File size: 2,216 Bytes
cc0bbc0
4b9b98e
 
 
 
 
cc0bbc0
4b9b98e
 
 
 
 
 
 
cc0bbc0
 
4b9b98e
 
 
 
cc0bbc0
4b9b98e
 
 
 
 
cc0bbc0
4b9b98e
 
 
 
cc0bbc0
4b9b98e
 
 
 
 
 
 
 
 
 
cc0bbc0
4b9b98e
 
 
 
 
 
cc0bbc0
4b9b98e
 
735829b
4b9b98e
 
 
 
 
cc0bbc0
4b9b98e
 
 
cc0bbc0
4b9b98e
937c1b4
4b9b98e
 
 
 
 
 
 
 
cc0bbc0
4b9b98e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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)