Spaces:
Paused
Paused
Update tabs/faq_assistant.py
Browse files- tabs/faq_assistant.py +95 -0
tabs/faq_assistant.py
CHANGED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import torch
|
| 4 |
+
from sentence_transformers import SentenceTransformer, util
|
| 5 |
+
|
| 6 |
+
# Load FAQ embeddings
|
| 7 |
+
with open("models/faq_embeddings.pkl", "rb") as f:
|
| 8 |
+
faq_data = pickle.load(f)
|
| 9 |
+
|
| 10 |
+
# Function to get answer from most similar FAQ
|
| 11 |
+
def answer_faq(user_query):
|
| 12 |
+
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 13 |
+
query_embedding = embedding_model.encode(user_query, convert_to_tensor=True)
|
| 14 |
+
similarities = util.pytorch_cos_sim(query_embedding, faq_data['embeddings'])[0]
|
| 15 |
+
idx = similarities.argmax().item()
|
| 16 |
+
return faq_data['answers'][idx]
|
| 17 |
+
|
| 18 |
+
# Clear input and output
|
| 19 |
+
def clear_faq():
|
| 20 |
+
return "", ""
|
| 21 |
+
|
| 22 |
+
# UI layout function for FAQ Support tab
|
| 23 |
+
def faq_assistant_tab():
|
| 24 |
+
gr.Markdown("## 🧠 TherapyBot++", elem_classes="centered-text")
|
| 25 |
+
gr.Markdown("Ask your health-related questions to get instant answers.", elem_classes="centered-text")
|
| 26 |
+
|
| 27 |
+
with gr.Row():
|
| 28 |
+
with gr.Column(scale=1):
|
| 29 |
+
faq_input = gr.Textbox(placeholder="e.g., How do I book an appointment?", label="Ask a Question")
|
| 30 |
+
faq_btn = gr.Button("Get Answer", elem_id="faq-btn")
|
| 31 |
+
faq_clear = gr.Button("Clear")
|
| 32 |
+
with gr.Column(scale=1):
|
| 33 |
+
faq_output = gr.Textbox(label="Answer", interactive=False, lines=6.9)
|
| 34 |
+
|
| 35 |
+
faq_btn.click(answer_faq, faq_input, outputs=faq_output)
|
| 36 |
+
faq_clear.click(clear_faq, outputs=[faq_input, faq_output])
|
| 37 |
+
|
| 38 |
+
gr.Markdown("""
|
| 39 |
+
<div style="height: auto; min-height: 100px; width: 100%; max-width: 65%; margin: 15px auto; overflow: hidden; position: relative; background: linear-gradient(to right, #1c2f3a 0%, #000000 50%, #1c2f3a 100%); border-radius: 12px; box-shadow: 0 2px 12px rgba(0,0,0,0.4); text-align: center; padding: 16px 24px; box-sizing: border-box;">
|
| 40 |
+
<div style="font-size: 15px; color: #e0e0e0; font-weight: 600; margin-bottom: 14px; position: relative; z-index: 2;">
|
| 41 |
+
📚 <em>You may refer to these example FAQ queries:</em>
|
| 42 |
+
</div>
|
| 43 |
+
<div class="scroll-wrapper">
|
| 44 |
+
<div class="scroll-content">
|
| 45 |
+
<div class="scroll-inner">
|
| 46 |
+
<div>What services does your healthcare facility offer?</div>
|
| 47 |
+
<div>How do I book an appointment?</div>
|
| 48 |
+
<div>Can I reschedule or cancel my appointment?</div>
|
| 49 |
+
<div>What are the modes of payment?</div>
|
| 50 |
+
<div>What should I do in a medical emergency?</div>
|
| 51 |
+
<div>How can I improve my mental health?</div>
|
| 52 |
+
<div>How do I get my lab test results?</div>
|
| 53 |
+
<div>What support do you offer for diabetes?</div>
|
| 54 |
+
<div>What services do you provide for women?</div>
|
| 55 |
+
<div>What is Glaucoma?</div>
|
| 56 |
+
<div>What causes High Blood Pressure?</div>
|
| 57 |
+
<div>How to treat Urinary Tract Infections?</div>
|
| 58 |
+
<div>What are the symptoms of Osteoporosis?</div>
|
| 59 |
+
<div>What causes Alzheimer’s Disease?</div>
|
| 60 |
+
<div>How can I get in touch with my doctor after hours?</div>
|
| 61 |
+
</div>
|
| 62 |
+
</div>
|
| 63 |
+
</div>
|
| 64 |
+
</div>
|
| 65 |
+
<style>
|
| 66 |
+
.scroll-wrapper {
|
| 67 |
+
height: 50px;
|
| 68 |
+
overflow: hidden;
|
| 69 |
+
position: relative;
|
| 70 |
+
}
|
| 71 |
+
.scroll-content {
|
| 72 |
+
height: auto;
|
| 73 |
+
width: 100%;
|
| 74 |
+
position: absolute;
|
| 75 |
+
animation: scroll-vertical 60s linear infinite;
|
| 76 |
+
will-change: transform;
|
| 77 |
+
}
|
| 78 |
+
.scroll-inner {
|
| 79 |
+
display: flex;
|
| 80 |
+
flex-direction: column;
|
| 81 |
+
gap: 20px;
|
| 82 |
+
color: #bbbbbb;
|
| 83 |
+
font-size: 13.8px;
|
| 84 |
+
line-height: 1.5;
|
| 85 |
+
}
|
| 86 |
+
@keyframes scroll-vertical {
|
| 87 |
+
0% {
|
| 88 |
+
transform: translateY(0);
|
| 89 |
+
}
|
| 90 |
+
100% {
|
| 91 |
+
transform: translateY(-75%);
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
</style>
|
| 95 |
+
""", elem_classes="centered-text")
|