import gradio as gr import pickle import torch from sentence_transformers import SentenceTransformer, util # Load FAQ embeddings with open("models/faq_embeddings.pkl", "rb") as f: faq_data = pickle.load(f) # Function to get answer from most similar FAQ def answer_faq(user_query): embedding_model = SentenceTransformer('all-MiniLM-L6-v2') query_embedding = embedding_model.encode(user_query, convert_to_tensor=True) similarities = util.pytorch_cos_sim(query_embedding, faq_data['embeddings'])[0] idx = similarities.argmax().item() return faq_data['answers'][idx] # Clear input and output def clear_faq(): return "", "" # UI layout function for FAQ Support tab def faq_assistant_tab(): gr.Markdown("## 🧠 TherapyBot++", elem_classes="centered-text") gr.Markdown("Ask your health-related questions to get instant answers.", elem_classes="centered-text") with gr.Row(): with gr.Column(scale=1): faq_input = gr.Textbox(placeholder="e.g., How do I book an appointment?", label="Ask a Question") faq_btn = gr.Button("Get Answer", elem_id="faq-btn") faq_clear = gr.Button("Clear") with gr.Column(scale=1): faq_output = gr.Textbox(label="Answer", interactive=False, lines=6.9) faq_btn.click(answer_faq, faq_input, outputs=faq_output) faq_clear.click(clear_faq, outputs=[faq_input, faq_output]) gr.Markdown("""
📚 You may refer to these example FAQ queries:
What services does your healthcare facility offer?
How do I book an appointment?
Can I reschedule or cancel my appointment?
What are the modes of payment?
What should I do in a medical emergency?
How can I improve my mental health?
How do I get my lab test results?
What support do you offer for diabetes?
What services do you provide for women?
What is Glaucoma?
What causes High Blood Pressure?
How to treat Urinary Tract Infections?
What are the symptoms of Osteoporosis?
What causes Alzheimer’s Disease?
How can I get in touch with my doctor after hours?
""", elem_classes="centered-text")