Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,75 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import faiss
|
| 7 |
import numpy as np
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
allow_methods=["*"],
|
| 18 |
-
allow_headers=["*"],
|
| 19 |
-
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
chunked_answers = None
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
chunks = [] # The actual chunk text
|
| 45 |
-
chunked_answers = [] # Full answer for reference
|
| 46 |
-
for q, a in zip(questions, answers):
|
| 47 |
-
answer_chunks = chunk_text(a)
|
| 48 |
-
for chunk in answer_chunks:
|
| 49 |
-
chunked_questions.append(q)
|
| 50 |
-
chunks.append(chunk)
|
| 51 |
-
chunked_answers.append(a)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
chunk_index.add(chunk_embeddings)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
def root():
|
| 66 |
-
return {"message": "Bank FAQ Assistant is running. Use /search endpoint to query."}
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# cosine_sim = 1 - (L2_distance^2 / 2)
|
| 77 |
-
similarities = 1 - (D[0] / 2)
|
| 78 |
-
threshold = 0.6
|
| 79 |
-
results = []
|
| 80 |
-
for idx, sim in zip(I[0], similarities):
|
| 81 |
-
if sim >= threshold:
|
| 82 |
-
results.append({
|
| 83 |
-
"question": chunked_questions[idx],
|
| 84 |
-
"full_answer": chunked_answers[idx]
|
| 85 |
-
})
|
| 86 |
-
return {"results": results}
|
| 87 |
|
| 88 |
-
|
| 89 |
-
def health_check():
|
| 90 |
-
return {"status": "healthy", "message": "FAQ Assistant is ready"}
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# Load dataset
|
| 5 |
+
df = pd.read_csv("samsung_led_tv_faq_500.csv")
|
| 6 |
+
df.head()
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
+
|
| 9 |
+
# Load pretrained model
|
| 10 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 11 |
+
|
| 12 |
+
# Generate embeddings
|
| 13 |
+
question_embeddings = model.encode(df['Question'].tolist(), show_progress_bar=True)
|
| 14 |
+
question_embeddings
|
| 15 |
import faiss
|
| 16 |
import numpy as np
|
| 17 |
+
# Drop exact duplicate questions
|
| 18 |
+
# Clean duplicates
|
| 19 |
+
df = df.drop_duplicates(subset='Question').reset_index(drop=True)
|
| 20 |
+
print(f"Total unique questions: {len(df)}")
|
| 21 |
|
| 22 |
+
# Regenerate embeddings for cleaned DataFrame
|
| 23 |
+
from sentence_transformers import SentenceTransformer
|
| 24 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 25 |
+
question_embeddings = model.encode(df['Question'].tolist(), show_progress_bar=True)
|
| 26 |
+
question_embeddings = np.array(question_embeddings).astype("float32")
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Build FAISS index
|
| 29 |
+
import faiss
|
| 30 |
+
index = faiss.IndexFlatL2(question_embeddings.shape[1])
|
| 31 |
+
index.add(question_embeddings)
|
|
|
|
| 32 |
|
| 33 |
+
def search_faq(query, k=3):
|
| 34 |
+
query_embedding = model.encode([query]).astype("float32")
|
| 35 |
+
D, I = index.search(query_embedding, k)
|
| 36 |
+
results = []
|
| 37 |
+
for dist, i in zip(D[0], I[0]):
|
| 38 |
+
if i < len(df):
|
| 39 |
+
results.append((df.iloc[i]['Question'], df.iloc[i]['Answer'], dist))
|
| 40 |
+
return results
|
| 41 |
+
query = "Can I mount the TV on a wall? (model UA48TU7069)"
|
| 42 |
+
results = search_faq(query)
|
| 43 |
|
| 44 |
+
print(f"Query: {query}\n")
|
| 45 |
+
for q, a, d in results:
|
| 46 |
+
print(f"Matched Q: {q}\nAnswer: {a}\nDistance: {d:.4f}\n")
|
| 47 |
+
import gradio as gr
|
| 48 |
+
from gtts import gTTS
|
| 49 |
+
import os
|
| 50 |
|
| 51 |
+
def gradio_interface(query):
|
| 52 |
+
results = search_faq(query, k=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
if results:
|
| 55 |
+
top_q, top_a, dist = results[0]
|
| 56 |
+
answer = top_a
|
| 57 |
+
else:
|
| 58 |
+
answer = "Sorry, I couldn't find a match."
|
|
|
|
| 59 |
|
| 60 |
+
# Generate audio with gTTS
|
| 61 |
+
tts = gTTS(text=answer, lang='en')
|
| 62 |
+
tts.save("answer.mp3")
|
| 63 |
|
| 64 |
+
return "answer.mp3"
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
fn=gradio_interface,
|
| 68 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask a question about your Samsung LED TV..."),
|
| 69 |
+
outputs=gr.Audio(label=""),
|
| 70 |
+
title="Samsung LED TV FAQ Assistant",
|
| 71 |
+
description="Ask queries about your Samsung LED TV. The assistant will speak the answer.",
|
| 72 |
+
theme="soft"
|
| 73 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
demo.launch(share=True)
|
|
|
|
|
|