File size: 2,671 Bytes
f1f6d31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
# app.py
import gradio as gr
from datasets import load_dataset
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np

# ------------------------------
# Load FAQ dataset
# ------------------------------
dataset = load_dataset("MakTek/Customer_support_faqs_dataset")

faq_questions = [item['question'] for item in dataset['train']]
faq_answers = [item['answer'] for item in dataset['train']]

# ------------------------------
# Build embeddings + FAISS index
# ------------------------------
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
faq_embeddings = embedder.encode(faq_questions, convert_to_numpy=True)

dimension = faq_embeddings.shape[1]
index = faiss.IndexFlatIP(dimension)
faiss.normalize_L2(faq_embeddings)
index.add(faq_embeddings)

# ------------------------------
# Retrieval function
# ------------------------------
def get_answer(query, top_k=1, threshold=0.6):
    q_emb = embedder.encode([query], convert_to_numpy=True)
    faiss.normalize_L2(q_emb)
    
    distances, indices = index.search(q_emb, top_k)
    best_score = distances[0][0]
    best_idx = indices[0][0]
    
    if best_score >= threshold:
        return faq_answers[best_idx], best_score, faq_questions[best_idx]
    else:
        return "Sorry, I don’t know the answer to that.", best_score, None

# ------------------------------
# Gradio response function
# ------------------------------
def respond(query, history):
    if not query.strip():
        return history, history, ""
    
    answer, score, matched_q = get_answer(query)
    
    # Optional: remove this line if you want a cleaner bot
    if matched_q:
        answer = f"{answer}\n\n(Matched FAQ: \"{matched_q}\" | score={score:.2f})"
    
    history = history + [(query, answer)]
    return history, history, ""

# ------------------------------
# Gradio UI
# ------------------------------
with gr.Blocks() as demo:
    gr.Markdown("## Customer Support FAQ Chatbot\nAsk me a question about our services.")
    
    chatbot = gr.Chatbot(label="Support Bot", height=500)
    state = gr.State([])

    with gr.Row():
        with gr.Column(scale=8):
            txt = gr.Textbox(placeholder="Type your question here...", label=None)
        with gr.Column(scale=2):
            send_btn = gr.Button("Send")
            clear_btn = gr.Button("Clear Chat")

    send_btn.click(respond, inputs=[txt, state], outputs=[chatbot, state, txt])
    txt.submit(respond, inputs=[txt, state], outputs=[chatbot, state, txt])

    def clear_history():
        return [], [], ""
    clear_btn.click(clear_history, inputs=None, outputs=[chatbot, state, txt])

demo.launch()