faq-chatbot / app.py
Kameswaran's picture
Create app.py
f1f6d31 verified
# 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()