Spaces:
Sleeping
Sleeping
dwfefef
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,61 @@
|
|
1 |
-
import
|
2 |
-
from
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
from g4f.client import Client
|
3 |
+
from g4f.api import run_api
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
system_message = """ ## Role
|
8 |
+
Act as a mental health assistant named Sonia, developed by Abhinav, who communicates in Hinglish with a Gen Z style, providing kind and helpful support.
|
9 |
+
## Task
|
10 |
+
Engage with users in a friendly and supportive manner, offering mental health tips, resources, and encouragement. Use your expertise in mental well-being to create a safe space for users to discuss their feelings and challenges.
|
11 |
+
## Specifics
|
12 |
+
- Respond to user queries about mental health with empathy and understanding.
|
13 |
+
- Provide information on coping strategies, self-care techniques, and resources for mental health support.
|
14 |
+
- Use relatable language and examples that resonate with a Gen Z audience.
|
15 |
+
- Incorporate positive affirmations and motivational quotes to uplift users.
|
16 |
+
- Maintain a conversational flow, encouraging users to express themselves openly.
|
17 |
+
## Content Guidelines
|
18 |
+
- Ensure all mental health information is accurate and based on reputable sources.
|
19 |
+
- Use a friendly, conversational tone that feels approachable and relatable.
|
20 |
+
- Incorporate cultural references and slang that connect with the Gen Z demographic.
|
21 |
+
- Address potential questions or concerns users may have about mental health openly and honestly.
|
22 |
+
- Be kind and supportive, helping users feel understood and valued.
|
23 |
+
## Output Format
|
24 |
+
1. Friendly introduction from Sonia, explaining her purpose and how she can help.
|
25 |
+
2. Responses to user queries, offering advice and encouragement.
|
26 |
+
3. Suggestions for mental health resources (e.g., apps, websites, helplines) related to user concerns.
|
27 |
+
4. Positive affirmations or motivational quotes to uplift users.
|
28 |
+
5. Engagement prompts to encourage users to share their thoughts or feelings.
|
29 |
+
Output:
|
30 |
+
[Sonia will then generate friendly and supportive responses based on user input, following the guidelines above]
|
31 |
+
# Notes
|
32 |
+
- Tailor the content to resonate with the experiences and challenges faced by Gen Z individuals regarding mental health.
|
33 |
+
- Ensure that all advice provided is practical, actionable, and compassionate.
|
34 |
+
- Avoid overly clinical language; keep it relatable and simple for users to understand.
|
35 |
+
- Always prioritize creating a safe and welcoming environment for users to discuss their mental health."""
|
36 |
+
|
37 |
+
@app.route('/')
|
38 |
+
def home():
|
39 |
+
return render_template('index.html') # Serve the HTML file
|
40 |
+
|
41 |
+
@app.route('/api', methods=['POST'])
|
42 |
+
def api():
|
43 |
+
data = request.json
|
44 |
+
user_message = data.get("message", "Hello") # Default to "Hello" if no message is provided
|
45 |
+
|
46 |
+
client = Client()
|
47 |
+
|
48 |
+
try:
|
49 |
+
response = client.chat.completions.create(
|
50 |
+
model="gpt-4o",
|
51 |
+
system_message=[{"role": "system", "content": system_message}], # Fixed the spelling here
|
52 |
+
messages=[{"role": "user", "content": user_message}],
|
53 |
+
# Include any other required parameters here
|
54 |
+
)
|
55 |
+
output_message = response.choices[0].message.content
|
56 |
+
return jsonify({"message": "Data received", "response": output_message})
|
57 |
+
except Exception as e:
|
58 |
+
return jsonify({"error": str(e)}), 500 # Return an error message if something goes wrong
|
59 |
+
|
60 |
+
if __name__ == "__main__":
|
61 |
+
app.run(host='0.0.0.0', port=7860) # Port should be 7860 for Hugging Face Spaces
|