Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import torch
|
| 4 |
+
from sentence_transformers import SentenceTransformer, util
|
| 5 |
+
|
| 6 |
+
# Load dataset
|
| 7 |
+
df = pd.read_csv("Chatbot.csv")
|
| 8 |
+
questions = df[df["name"] == "User"]["line"].tolist()
|
| 9 |
+
answers = df[df["name"] == "ECO"]["line"].tolist()
|
| 10 |
+
|
| 11 |
+
# Load embedding model
|
| 12 |
+
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 13 |
+
question_embeddings = embedding_model.encode(questions, convert_to_tensor=True)
|
| 14 |
+
|
| 15 |
+
def get_best_response(user_input):
|
| 16 |
+
input_embedding = embedding_model.encode(user_input, convert_to_tensor=True)
|
| 17 |
+
similarities = util.pytorch_cos_sim(input_embedding, question_embeddings)[0]
|
| 18 |
+
best_match_idx = torch.argmax(similarities).item()
|
| 19 |
+
best_match_score = similarities[best_match_idx].item()
|
| 20 |
+
|
| 21 |
+
if best_match_score > 0.7:
|
| 22 |
+
return answers[best_match_idx]
|
| 23 |
+
return "I'm sorry, I don't have an answer for that."
|
| 24 |
+
|
| 25 |
+
# Gradio UI
|
| 26 |
+
iface = gr.Interface(fn=get_best_response, inputs="text", outputs="text", title="🤖 AI Chatbot")
|
| 27 |
+
iface.launch()
|