Update app.py
Browse files
app.py
CHANGED
@@ -5,20 +5,22 @@ import gradio as gr
|
|
5 |
import faiss
|
6 |
from datasets import load_dataset
|
7 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
8 |
|
9 |
-
# Optional: Use token
|
10 |
auth_token = os.environ.get("HF_TOKEN")
|
11 |
|
12 |
# Load dataset
|
13 |
ds = load_dataset("RomainPct/steve-jobs-question-and-answers", split="train", use_auth_token=auth_token)
|
14 |
|
15 |
-
#
|
16 |
data = pd.DataFrame({
|
17 |
'question': ds['instruction'],
|
18 |
'answer': ds['output']
|
19 |
})
|
20 |
|
21 |
-
# Load
|
22 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
23 |
|
24 |
# Encode all questions
|
@@ -26,23 +28,29 @@ faq_embeddings = model.encode(data['question'].tolist())
|
|
26 |
faq_index = faiss.IndexFlatL2(faq_embeddings.shape[1])
|
27 |
faq_index.add(faq_embeddings)
|
28 |
|
29 |
-
#
|
30 |
def get_answer_with_audio(query):
|
31 |
query_embedding = model.encode([query])
|
32 |
D, I = faq_index.search(np.array(query_embedding), k=1)
|
33 |
answer = data.iloc[I[0][0]]['answer']
|
34 |
-
return answer, answer # returns both text and audio
|
35 |
|
36 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
iface = gr.Interface(
|
38 |
fn=get_answer_with_audio,
|
39 |
inputs=gr.Textbox(placeholder="Ask a question about Steve Jobs...", label="Your Question"),
|
40 |
outputs=[
|
41 |
gr.Text(label="Answer"),
|
42 |
-
gr.Audio(label="Spoken Answer", type="
|
43 |
],
|
44 |
title="🧠 Steve Jobs FAQ Chatbot",
|
45 |
-
description="Ask anything about Steve Jobs. This chatbot answers
|
46 |
)
|
47 |
|
48 |
iface.launch()
|
|
|
5 |
import faiss
|
6 |
from datasets import load_dataset
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
+
from gtts import gTTS # Google Text-to-Speech
|
9 |
+
import tempfile
|
10 |
|
11 |
+
# Optional: Use token for private datasets
|
12 |
auth_token = os.environ.get("HF_TOKEN")
|
13 |
|
14 |
# Load dataset
|
15 |
ds = load_dataset("RomainPct/steve-jobs-question-and-answers", split="train", use_auth_token=auth_token)
|
16 |
|
17 |
+
# Convert to DataFrame
|
18 |
data = pd.DataFrame({
|
19 |
'question': ds['instruction'],
|
20 |
'answer': ds['output']
|
21 |
})
|
22 |
|
23 |
+
# Load embedding model
|
24 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
25 |
|
26 |
# Encode all questions
|
|
|
28 |
faq_index = faiss.IndexFlatL2(faq_embeddings.shape[1])
|
29 |
faq_index.add(faq_embeddings)
|
30 |
|
31 |
+
# Answer + Audio generation
|
32 |
def get_answer_with_audio(query):
|
33 |
query_embedding = model.encode([query])
|
34 |
D, I = faq_index.search(np.array(query_embedding), k=1)
|
35 |
answer = data.iloc[I[0][0]]['answer']
|
|
|
36 |
|
37 |
+
# Generate speech with gTTS
|
38 |
+
tts = gTTS(text=answer)
|
39 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
40 |
+
tts.save(temp_file.name)
|
41 |
+
|
42 |
+
return answer, temp_file.name
|
43 |
+
|
44 |
+
# Gradio interface
|
45 |
iface = gr.Interface(
|
46 |
fn=get_answer_with_audio,
|
47 |
inputs=gr.Textbox(placeholder="Ask a question about Steve Jobs...", label="Your Question"),
|
48 |
outputs=[
|
49 |
gr.Text(label="Answer"),
|
50 |
+
gr.Audio(label="Spoken Answer", type="filepath")
|
51 |
],
|
52 |
title="🧠 Steve Jobs FAQ Chatbot",
|
53 |
+
description="Ask anything about Steve Jobs. This chatbot answers in text and speech."
|
54 |
)
|
55 |
|
56 |
iface.launch()
|