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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 4 |
+
|
| 5 |
+
# Load BlenderBot model
|
| 6 |
+
model_id = "facebook/blenderbot-1B-distill"
|
| 7 |
+
tokenizer = BlenderbotTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = BlenderbotForConditionalGeneration.from_pretrained(model_id)
|
| 9 |
+
|
| 10 |
+
chat_history = []
|
| 11 |
+
|
| 12 |
+
# Response function
|
| 13 |
+
def respond(user_message):
|
| 14 |
+
global chat_history
|
| 15 |
+
inputs = tokenizer([user_message], return_tensors="pt").to(model.device)
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
reply_ids = model.generate(**inputs, max_new_tokens=80, do_sample=False)
|
| 18 |
+
reply = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
| 19 |
+
chat_history.append((user_message, reply))
|
| 20 |
+
formatted_chat = "\n\n".join([f"👤 You: {u}\n\n🤖 Bot: {b}\n\n------" for u, b in chat_history])
|
| 21 |
+
return "", formatted_chat.strip()
|
| 22 |
+
|
| 23 |
+
# Clear function
|
| 24 |
+
def clear_chat():
|
| 25 |
+
global chat_history
|
| 26 |
+
chat_history = []
|
| 27 |
+
return "", ""
|
| 28 |
+
|
| 29 |
+
# Optional CSS
|
| 30 |
+
custom_css = """
|
| 31 |
+
body {
|
| 32 |
+
background-color: #121212;
|
| 33 |
+
color: white;
|
| 34 |
+
}
|
| 35 |
+
.centered-text {
|
| 36 |
+
text-align: center;
|
| 37 |
+
margin-left: auto;
|
| 38 |
+
margin-right: auto;
|
| 39 |
+
}
|
| 40 |
+
#submit-btn, #symptom-btn, #faq-btn, #process-btn {
|
| 41 |
+
background-color: #cc5500 !important;
|
| 42 |
+
color: white;
|
| 43 |
+
font-weight: bold;
|
| 44 |
+
border-radius: 8px;
|
| 45 |
+
padding: 10px 20px;
|
| 46 |
+
}
|
| 47 |
+
#explanation-box textarea {
|
| 48 |
+
background-color: #1c1c1c !important;
|
| 49 |
+
color: #f0f0f0 !important;
|
| 50 |
+
border-radius: 10px;
|
| 51 |
+
border: 1px solid #444 !important;
|
| 52 |
+
font-size: 15px;
|
| 53 |
+
padding: 12px;
|
| 54 |
+
box-shadow: 0 0 8px rgba(0,0,0,0.4);
|
| 55 |
+
}
|
| 56 |
+
.gr-markdown > div {
|
| 57 |
+
background: transparent !important;
|
| 58 |
+
}
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
# UI function
|
| 62 |
+
def therapy_chat_tab():
|
| 63 |
+
gr.Markdown("## 🧠 TherapyBot++", elem_classes="centered-text")
|
| 64 |
+
gr.Markdown("Thoughtfully designed to be your safe and supportive space — here for you when your mind needs it the most.", elem_classes="centered-text")
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column():
|
| 67 |
+
user_input = gr.Textbox(placeholder="How are you feeling?", label="🗣 Your Message", lines=8)
|
| 68 |
+
submit_btn = gr.Button("Submit", elem_id="submit-btn")
|
| 69 |
+
clear_btn = gr.Button("Clear")
|
| 70 |
+
with gr.Column():
|
| 71 |
+
with gr.Accordion("📜 Chat History", open=True):
|
| 72 |
+
chat_display = gr.Textbox(label=None, interactive=False, lines=13.8, container=False)
|
| 73 |
+
submit_btn.click(respond, user_input, outputs=[user_input, chat_display])
|
| 74 |
+
clear_btn.click(clear_chat, outputs=[user_input, chat_display])
|
| 75 |
+
gr.Markdown(
|
| 76 |
+
"""
|
| 77 |
+
<div style="
|
| 78 |
+
height: auto;
|
| 79 |
+
min-height: 100px;
|
| 80 |
+
width: 100%;
|
| 81 |
+
max-width: 65%;
|
| 82 |
+
margin: 15px auto;
|
| 83 |
+
overflow: hidden;
|
| 84 |
+
position: relative;
|
| 85 |
+
background: linear-gradient(to right, #1c2f3a 0%, #000000 50%, #1c2f3a 100%);
|
| 86 |
+
border-radius: 12px;
|
| 87 |
+
box-shadow: 0 2px 12px rgba(0,0,0,0.4);
|
| 88 |
+
text-align: center;
|
| 89 |
+
padding: 16px 24px;
|
| 90 |
+
box-sizing: border-box;
|
| 91 |
+
">
|
| 92 |
+
<div style="
|
| 93 |
+
font-size: 15px;
|
| 94 |
+
color: #e0e0e0;
|
| 95 |
+
font-weight: 600;
|
| 96 |
+
margin-bottom: 14px;
|
| 97 |
+
position: relative;
|
| 98 |
+
z-index: 2;
|
| 99 |
+
">
|
| 100 |
+
🧠 <em>Here are some surprising and helpful wellness facts:</em>
|
| 101 |
+
</div>
|
| 102 |
+
<div class="scroll-wrapper chatbot-scroll">
|
| 103 |
+
<div class="scroll-content chatbot-content">
|
| 104 |
+
<div class="scroll-inner">
|
| 105 |
+
<div>-----</div>
|
| 106 |
+
<div>🧠 Talking to someone about your emotions can significantly lower stress levels.</div>
|
| 107 |
+
<div>👃 The average human nose can identify over 50,000 different smells.</div>
|
| 108 |
+
<div>😶🌫️ Suppressing emotions doesn’t erase them — they return stronger later.</div>
|
| 109 |
+
<div>🧠 Your brain remains plastic at any age — emotional healing is always possible.</div>
|
| 110 |
+
<div>🍋 Lemons are among the healthiest fruits, rich in antioxidants and immune support.</div>
|
| 111 |
+
<div>🛏️ Sleep affects nearly every system in the body — it’s your natural mental reset.</div>
|
| 112 |
+
<div>🔁 Replaying negative thoughts rewires your brain for anxiety — called cognitive looping.</div>
|
| 113 |
+
<div>🍵 Drinking a warm beverage may help your body feel cooler and more relaxed.</div>
|
| 114 |
+
<div>👶 Newborns have the fastest heartbeats of any human age group.</div>
|
| 115 |
+
<div>🦶 Your feet can reveal early signs of deeper health issues like diabetes or stress.</div>
|
| 116 |
+
<div>👫 Companionship and bonding are great for both your heart and brain health.</div>
|
| 117 |
+
<div>👂 The brain releases calming chemicals when it feels truly listened to.</div>
|
| 118 |
+
<div>🌞 Optimistic thinking can add years to your life by reducing stress.</div>
|
| 119 |
+
<div>❄️ Cold weather can boost metabolism and strengthen your immune system.</div>
|
| 120 |
+
<div>👁️ You can physically see signs of high cholesterol in your eyes and skin.</div>
|
| 121 |
+
<div>🎨 Creative activities like drawing or music reduce anxiety and boost cognition.</div>
|
| 122 |
+
<div>🧬 Trauma can be inherited — emotions may echo generations before you.</div>
|
| 123 |
+
<div>😢 Humans are the only animals who cry due to emotional reasons.</div>
|
| 124 |
+
<div>📵 Limiting screen time improves focus, clarity, and emotional regulation.</div>
|
| 125 |
+
<div>💓 Your heart beats over 100,000 times a day to keep you alive and well.</div>
|
| 126 |
+
<div>☕ Drinking coffee may reduce symptoms of depression and lift your mood.</div>
|
| 127 |
+
<div>💧 You can lose 3% body weight in fluids before even feeling thirsty.</div>
|
| 128 |
+
<div>📆 Heart attacks are most frequent on Monday mornings due to work stress.</div>
|
| 129 |
+
<div>🐶 A dog’s companionship may lower blood pressure and protect your heart.</div>
|
| 130 |
+
<div>🩸 The heart pumps nearly 2,000 gallons of blood every day.</div>
|
| 131 |
+
<div>😣 Chronic stress increases your risk of type 2 diabetes.</div>
|
| 132 |
+
<div>🩻 Some capillaries are ten times thinner than a strand of hair.</div>
|
| 133 |
+
<div>🦴 More than half your bones are located in your hands and feet.</div>
|
| 134 |
+
<div>🧘 Just 10 minutes of mindful breathing can reset your brain chemistry.</div>
|
| 135 |
+
<div>❓ Most therapy breakthroughs come from better questions — not quick answers.</div>
|
| 136 |
+
<div>😂 Laughter releases feel-good hormones and even helps reduce pain.</div>
|
| 137 |
+
<div>🫀 Your heart weighs around 7 to 15 ounces depending on your body.</div>
|
| 138 |
+
<div>🏃 The heart works harder than a sprinter's leg muscles — nonstop effort.</div>
|
| 139 |
+
<div>🧠 Emotional pain activates the same brain region as physical pain.</div>
|
| 140 |
+
<div>🍌 Bananas can naturally boost your mood by increasing serotonin levels.</div>
|
| 141 |
+
</div>
|
| 142 |
+
</div>
|
| 143 |
+
</div>
|
| 144 |
+
</div>
|
| 145 |
+
<style>
|
| 146 |
+
.chatbot-scroll .chatbot-content {
|
| 147 |
+
position: absolute;
|
| 148 |
+
width: 100%;
|
| 149 |
+
animation: chatbot-scroll-vertical 130s linear infinite;
|
| 150 |
+
will-change: transform;
|
| 151 |
+
}
|
| 152 |
+
.scroll-wrapper {
|
| 153 |
+
height: 50px;
|
| 154 |
+
overflow: hidden;
|
| 155 |
+
position: relative;
|
| 156 |
+
}
|
| 157 |
+
.scroll-inner {
|
| 158 |
+
display: flex;
|
| 159 |
+
flex-direction: column;
|
| 160 |
+
gap: 20px;
|
| 161 |
+
color: #bbbbbb;
|
| 162 |
+
font-size: 13.8px;
|
| 163 |
+
line-height: 1.5;
|
| 164 |
+
}
|
| 165 |
+
@keyframes chatbot-scroll-vertical {
|
| 166 |
+
0% {
|
| 167 |
+
transform: translateY(0);
|
| 168 |
+
}
|
| 169 |
+
100% {
|
| 170 |
+
transform: translateY(-50%);
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
</style>
|
| 174 |
+
""",
|
| 175 |
+
elem_classes="centered-text"
|
| 176 |
+
)
|