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)