Spaces:
Paused
Paused
File size: 4,002 Bytes
4ca4b26 |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
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("""
<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;">
<div style="font-size: 15px; color: #e0e0e0; font-weight: 600; margin-bottom: 14px; position: relative; z-index: 2;">
📚 <em>You may refer to these example FAQ queries:</em>
</div>
<div class="scroll-wrapper">
<div class="scroll-content">
<div class="scroll-inner">
<div>What services does your healthcare facility offer?</div>
<div>How do I book an appointment?</div>
<div>Can I reschedule or cancel my appointment?</div>
<div>What are the modes of payment?</div>
<div>What should I do in a medical emergency?</div>
<div>How can I improve my mental health?</div>
<div>How do I get my lab test results?</div>
<div>What support do you offer for diabetes?</div>
<div>What services do you provide for women?</div>
<div>What is Glaucoma?</div>
<div>What causes High Blood Pressure?</div>
<div>How to treat Urinary Tract Infections?</div>
<div>What are the symptoms of Osteoporosis?</div>
<div>What causes Alzheimer’s Disease?</div>
<div>How can I get in touch with my doctor after hours?</div>
</div>
</div>
</div>
</div>
<style>
.scroll-wrapper {
height: 50px;
overflow: hidden;
position: relative;
}
.scroll-content {
height: auto;
width: 100%;
position: absolute;
animation: scroll-vertical 60s linear infinite;
will-change: transform;
}
.scroll-inner {
display: flex;
flex-direction: column;
gap: 20px;
color: #bbbbbb;
font-size: 13.8px;
line-height: 1.5;
}
@keyframes scroll-vertical {
0% {
transform: translateY(0);
}
100% {
transform: translateY(-75%);
}
}
</style>
""", elem_classes="centered-text") |