ombhojane commited on
Commit
5b266ef
·
verified ·
1 Parent(s): 17ef516

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # Load model and tokenizer
6
+ model_name = "ombhojane/mental-health-assistant"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
10
+
11
+ def generate_response(message, history):
12
+ # Format the input with chat history
13
+ prompt = ""
14
+ for user_msg, bot_msg in history:
15
+ prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n"
16
+ prompt += f"User: {message}\nAssistant:"
17
+
18
+ # Generate response
19
+ response = pipe(
20
+ prompt,
21
+ max_length=256,
22
+ num_return_sequences=1,
23
+ temperature=0.7,
24
+ top_p=0.9,
25
+ do_sample=True,
26
+ pad_token_id=tokenizer.eos_token_id
27
+ )[0]["generated_text"]
28
+
29
+ # Extract just the assistant's response
30
+ try:
31
+ assistant_response = response.split("Assistant:")[-1].strip()
32
+ except:
33
+ assistant_response = response
34
+
35
+ return assistant_response
36
+
37
+ # Create the Gradio interface
38
+ with gr.Blocks(css="footer {visibility: hidden}") as demo:
39
+ gr.Markdown(
40
+ """
41
+ # 🧠 Mental Health Assistant
42
+ Welcome! I'm here to provide support and guidance for your mental health concerns.
43
+ While I can offer helpful insights, please remember I'm not a replacement for professional medical advice.
44
+ """
45
+ )
46
+
47
+ chatbot = gr.Chatbot(
48
+ height=400,
49
+ show_label=False,
50
+ container=True,
51
+ bubble_full_width=False,
52
+ )
53
+
54
+ with gr.Row():
55
+ msg = gr.Textbox(
56
+ scale=4,
57
+ show_label=False,
58
+ placeholder="Type your message here...",
59
+ container=False
60
+ )
61
+ submit = gr.Button("Send", scale=1, variant="primary")
62
+
63
+ gr.Examples(
64
+ examples=[
65
+ "I've been feeling really anxious lately",
66
+ "How can I improve my sleep habits?",
67
+ "I'm having trouble focusing at work",
68
+ ],
69
+ inputs=msg
70
+ )
71
+
72
+ gr.Markdown(
73
+ """
74
+ ### Tips for best results:
75
+ - Be specific about how you're feeling
76
+ - Ask direct questions
77
+ - Share relevant context
78
+ - Take your time to explain your situation
79
+ """
80
+ )
81
+
82
+ # Set up event handlers
83
+ submit_click = submit.click(
84
+ generate_response,
85
+ inputs=[msg, chatbot],
86
+ outputs=[chatbot],
87
+ queue=True
88
+ )
89
+ submit_click.then(lambda: "", None, msg, queue=False)
90
+
91
+ msg.submit(
92
+ generate_response,
93
+ inputs=[msg, chatbot],
94
+ outputs=[chatbot],
95
+ queue=True
96
+ ).then(lambda: "", None, msg, queue=False)
97
+
98
+ demo.launch()