File size: 5,029 Bytes
aa715fc efa58a7 aa715fc efa58a7 aa715fc |
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
import spaces
# Load model and tokenizer
print("Loading model...")
tokenizer = AutoTokenizer.from_pretrained("ombhojane/mental-health-assistant")
model = AutoModelForCausalLM.from_pretrained(
"ombhojane/mental-health-assistant",
device_map="auto",
torch_dtype=torch.float32
)
print("Model loaded successfully!")
@spaces.GPU
def generate_response(message, max_length, temperature):
if not message:
return "Please enter your concerns or feelings to get support."
# Format input with special tokens
prompt = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
# Generate response
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
inputs.input_ids,
max_length=max_length,
temperature=temperature,
do_sample=True,
top_p=0.9
)
# Decode response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response.replace(message, "").strip()
# Custom CSS for a calming and professional appearance
custom_css = """
.gradio-container {
font-family: 'Arial', sans-serif;
background-color: #f5f7f9;
}
.main-title {
text-align: center;
color: #2C3E50;
margin-bottom: 1em;
}
.description {
text-align: justify;
margin-bottom: 2em;
color: #34495E;
}
"""
with gr.Blocks(css=custom_css) as demo:
gr.Markdown(
"""
# Wellness 🌿
### Your Compassionate Mental Health Support Companion
"""
)
with gr.Row():
with gr.Column(scale=2):
gr.Markdown(
"""
### About
Welcome to your safe space for mental health support. This AI assistant is trained to provide
empathetic listening, emotional support, and helpful suggestions for managing mental health
concerns. While it's not a replacement for professional help, it can offer guidance and
support when you need someone to talk to.
**Remember**: In case of emergency or severe distress, please contact professional mental
health services or emergency services immediately.
"""
)
with gr.Row():
with gr.Column(scale=2):
input_text = gr.Textbox(
lines=4,
placeholder="Share your thoughts, feelings, or concerns...",
label="Your Message"
)
with gr.Row():
max_length = gr.Slider(
minimum=100,
maximum=1000,
value=512,
step=50,
label="Response Length",
info="Adjust the length of the response"
)
temperature = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.7,
step=0.1,
label="Response Style",
info="Higher values for more creative responses, lower for more focused ones"
)
submit_btn = gr.Button("Get Support", variant="primary")
with gr.Column(scale=2):
output_text = gr.Textbox(
lines=12,
label="Support Response",
show_copy_button=True
)
with gr.Accordion("Sample Conversation Starters", open=False):
gr.Markdown(
"""
- I've been feeling overwhelmed lately and having trouble sleeping
- How can I manage anxiety during stressful situations?
- I'm having difficulty concentrating at work/school
- What are some good self-care practices for mental health?
- I'm feeling lonely and isolated, what can I do?
"""
)
# Add helpful resources
with gr.Accordion("Important Resources", open=False):
gr.Markdown(
"""
### Emergency Contacts
- National Crisis Helpline: 988 (US)
- Emergency Services: 911 (US) / 112 (EU)
### Mental Health Resources
- National Alliance on Mental Illness (NAMI): 1-800-950-NAMI
- Crisis Text Line: Text HOME to 741741
- Psychology Today Therapist Finder
- BetterHelp Online Counseling
Remember: This AI assistant is not a substitute for professional mental health care.
If you're experiencing severe symptoms or having thoughts of self-harm, please seek
professional help immediately.
"""
)
submit_btn.click(
generate_response,
inputs=[input_text, max_length, temperature],
outputs=output_text
)
if __name__ == "__main__":
demo.launch() |