Vizznu19's picture
Update app.py
4b9b98e verified
import pandas as pd
# Load dataset
df = pd.read_csv("samsung_led_tv_faq_500.csv")
df.head()
from sentence_transformers import SentenceTransformer
# Load pretrained model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Generate embeddings
question_embeddings = model.encode(df['Question'].tolist(), show_progress_bar=True)
question_embeddings
import faiss
import numpy as np
# Drop exact duplicate questions
# Clean duplicates
df = df.drop_duplicates(subset='Question').reset_index(drop=True)
print(f"Total unique questions: {len(df)}")
# Regenerate embeddings for cleaned DataFrame
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
question_embeddings = model.encode(df['Question'].tolist(), show_progress_bar=True)
question_embeddings = np.array(question_embeddings).astype("float32")
# Build FAISS index
import faiss
index = faiss.IndexFlatL2(question_embeddings.shape[1])
index.add(question_embeddings)
def search_faq(query, k=3):
query_embedding = model.encode([query]).astype("float32")
D, I = index.search(query_embedding, k)
results = []
for dist, i in zip(D[0], I[0]):
if i < len(df):
results.append((df.iloc[i]['Question'], df.iloc[i]['Answer'], dist))
return results
query = "Can I mount the TV on a wall? (model UA48TU7069)"
results = search_faq(query)
print(f"Query: {query}\n")
for q, a, d in results:
print(f"Matched Q: {q}\nAnswer: {a}\nDistance: {d:.4f}\n")
import gradio as gr
from gtts import gTTS
import os
def gradio_interface(query):
results = search_faq(query, k=1)
if results:
top_q, top_a, dist = results[0]
answer = top_a
else:
answer = "Sorry, I couldn't find a match."
# Generate audio with gTTS
tts = gTTS(text=answer, lang='en')
tts.save("answer.mp3")
return "answer.mp3"
demo = gr.Interface(
fn=gradio_interface,
inputs=gr.Textbox(lines=2, placeholder="Ask a question about your Samsung LED TV..."),
outputs=gr.Audio(label=""),
title="Samsung LED TV FAQ Assistant",
description="Ask queries about your Samsung LED TV. The assistant will speak the answer.",
theme="soft"
)
demo.launch(share=True)