Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import faiss
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# ------------------------------
|
| 9 |
+
# Load FAQ dataset
|
| 10 |
+
# ------------------------------
|
| 11 |
+
dataset = load_dataset("MakTek/Customer_support_faqs_dataset")
|
| 12 |
+
|
| 13 |
+
faq_questions = [item['question'] for item in dataset['train']]
|
| 14 |
+
faq_answers = [item['answer'] for item in dataset['train']]
|
| 15 |
+
|
| 16 |
+
# ------------------------------
|
| 17 |
+
# Build embeddings + FAISS index
|
| 18 |
+
# ------------------------------
|
| 19 |
+
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 20 |
+
faq_embeddings = embedder.encode(faq_questions, convert_to_numpy=True)
|
| 21 |
+
|
| 22 |
+
dimension = faq_embeddings.shape[1]
|
| 23 |
+
index = faiss.IndexFlatIP(dimension)
|
| 24 |
+
faiss.normalize_L2(faq_embeddings)
|
| 25 |
+
index.add(faq_embeddings)
|
| 26 |
+
|
| 27 |
+
# ------------------------------
|
| 28 |
+
# Retrieval function
|
| 29 |
+
# ------------------------------
|
| 30 |
+
def get_answer(query, top_k=1, threshold=0.6):
|
| 31 |
+
q_emb = embedder.encode([query], convert_to_numpy=True)
|
| 32 |
+
faiss.normalize_L2(q_emb)
|
| 33 |
+
|
| 34 |
+
distances, indices = index.search(q_emb, top_k)
|
| 35 |
+
best_score = distances[0][0]
|
| 36 |
+
best_idx = indices[0][0]
|
| 37 |
+
|
| 38 |
+
if best_score >= threshold:
|
| 39 |
+
return faq_answers[best_idx], best_score, faq_questions[best_idx]
|
| 40 |
+
else:
|
| 41 |
+
return "Sorry, I don’t know the answer to that.", best_score, None
|
| 42 |
+
|
| 43 |
+
# ------------------------------
|
| 44 |
+
# Gradio response function
|
| 45 |
+
# ------------------------------
|
| 46 |
+
def respond(query, history):
|
| 47 |
+
if not query.strip():
|
| 48 |
+
return history, history, ""
|
| 49 |
+
|
| 50 |
+
answer, score, matched_q = get_answer(query)
|
| 51 |
+
|
| 52 |
+
# Optional: remove this line if you want a cleaner bot
|
| 53 |
+
if matched_q:
|
| 54 |
+
answer = f"{answer}\n\n(Matched FAQ: \"{matched_q}\" | score={score:.2f})"
|
| 55 |
+
|
| 56 |
+
history = history + [(query, answer)]
|
| 57 |
+
return history, history, ""
|
| 58 |
+
|
| 59 |
+
# ------------------------------
|
| 60 |
+
# Gradio UI
|
| 61 |
+
# ------------------------------
|
| 62 |
+
with gr.Blocks() as demo:
|
| 63 |
+
gr.Markdown("## Customer Support FAQ Chatbot\nAsk me a question about our services.")
|
| 64 |
+
|
| 65 |
+
chatbot = gr.Chatbot(label="Support Bot", height=500)
|
| 66 |
+
state = gr.State([])
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column(scale=8):
|
| 70 |
+
txt = gr.Textbox(placeholder="Type your question here...", label=None)
|
| 71 |
+
with gr.Column(scale=2):
|
| 72 |
+
send_btn = gr.Button("Send")
|
| 73 |
+
clear_btn = gr.Button("Clear Chat")
|
| 74 |
+
|
| 75 |
+
send_btn.click(respond, inputs=[txt, state], outputs=[chatbot, state, txt])
|
| 76 |
+
txt.submit(respond, inputs=[txt, state], outputs=[chatbot, state, txt])
|
| 77 |
+
|
| 78 |
+
def clear_history():
|
| 79 |
+
return [], [], ""
|
| 80 |
+
clear_btn.click(clear_history, inputs=None, outputs=[chatbot, state, txt])
|
| 81 |
+
|
| 82 |
+
demo.launch()
|