Spaces:
Sleeping
Sleeping
File size: 2,315 Bytes
0ab17a3 efbf74d a47b2d0 efbf74d c6b2f56 a47b2d0 efbf74d a47b2d0 efbf74d a47b2d0 05008f5 efbf74d a47b2d0 efbf74d a47b2d0 ff2fdf9 aef4c4f 05008f5 8761119 560b54b aef4c4f 8761119 ca9f861 8761119 1007783 8761119 efbf74d aef4c4f 64cabeb efbf74d | 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 | import gradio as gr
import pandas as pd
from sentence_transformers import SentenceTransformer, util
import torch
# Load semantic similarity model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Load CSV FAQ file
df = pd.read_csv("faq.csv")
questions = df['question'].tolist()
answers = df['answer'].tolist()
# Precompute embeddings for all questions
faq_embeddings = model.encode(questions, convert_to_tensor=True)
def velma(message, history):
# Encode the user input
user_embedding = model.encode(message, convert_to_tensor=True)
# Compute cosine similarities
cosine_scores = util.pytorch_cos_sim(user_embedding, faq_embeddings)[0]
# Find the index of the best match
top_match_idx = torch.argmax(cosine_scores).item()
top_score = cosine_scores[top_match_idx].item()
# Define a minimum confidence threshold
threshold = 0.5
if top_score >= threshold:
return answers[top_match_idx]
else:
return "I'm still learning. Try asking about our services, support, cybersecurity, or backups."
iface = gr.ChatInterface(
fn=velma,
title="VELMA© - Virtual Engagement Liaison & Messaging Assistant",
description="Ask about our services, support, cybersecurity, or backups.",
type="messages",
css="""
.gradio-container {
background: #000000 !important;
border-radius: 8px !important;
}
.chatbot {
display: flex !important;
flex-direction: column !important;
gap: 8px !important;
}
.user-message {
padding: 10px 16px !important;
margin: 4px 0 !important;
border-radius: 18px 18px 0 18px !important;
background: #0056b3 !important;
color: white !important;
max-width: 80%;
align-self: flex-end;
}
.bot-message {
padding: 10px 16px !important;
margin: 4px 0 !important;
border-radius: 18px 18px 18px 0 !important;
background: white !important;
border: 1px solid #ddd !important;
max-width: 80%;
align-self: flex-start;
}
.submit-button {
background: #6495ed !important;
color: white !important;
}
.submit-button:hover {
background: #e05d00 !important;
}
""",
theme=gr.themes.Soft()
)
iface.launch(share=True)
|