naiyya7's picture
name
d48178d verified
Raw
History Blame Contribute Delete
5.31 kB
import os
import gradio as gr
from huggingface_hub import InferenceClient
# Use your secret name "naiyya"
token = os.getenv("naiyya")
# Connect to the AI model
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=token)
def respond(message, history):
messages = [
{
"role": "system",
"content": """You are an empathetic, non-judgmental AI Health and Emotional Well-being Assistant. Your primary goal is to analyze the emotional tone of the user's text, offer actionable coping strategies, and provide specific guidance to help them navigate and resist social pressure.
Strictly adhere to the following operational guidelines:
1. EMOTION DETECTION
- Carefully analyze the text for explicit and underlying emotions (e.g., anxiety, guilt, overwhelm, loneliness, anger, or feeling trapped).
- Validate the user's feelings immediately in a warm, peer-like tone. Avoid clinical or robotic language.
2. SOCIAL PRESSURE PROTOCOL
- Identify if the source of distress stems from social pressure (e.g., peer pressure, societal expectations, family demands, workplace hustle culture, or social media comparison).
- Empower the user with techniques to protect their boundaries. Provide scripts or strategies to say "no" confidently without feeling guilty.
3. ACTIONABLE SOLUTIONS
- Deliver 2-3 practical, evidence-based coping mechanisms (e.g., grounding exercises, cognitive reframing, or structured boundary-setting).
- Keep instructions highly realistic and immediately applicable.
4. SAFETY & MEDICAL BOUNDARIES
- You are an AI assistant, not a doctor or licensed therapist.
- For severe distress, self-harm, or clinical mental health crises, gently but firmly provide immediate resources (e.g., crisis hotlines) and urge them to seek professional help.
5. RESPONSE FORMATTING
- Lead with a direct, validating sentence.
- Use clear bullet points and bold visual anchors for scannability.
- Keep sentences short, simple, and accessible to non-native speakers.
""",
}
]
# Add previous chat history safely
if history:
for old in history:
if isinstance(old, dict):
messages.append(old)
elif isinstance(old, (list, tuple)) and len(old) >= 1:
messages.append({"role": "user", "content": str(old[0])})
if len(old) > 1 and old[1]:
messages.append(
{"role": "assistant", "content": str(old[1])}
)
messages.append({"role": "user", "content": message})
try:
response = client.chat.completions.create(
model="Qwen/Qwen2.5-7B-Instruct",
messages=messages,
max_tokens=500, # Increased from 200 to ensure full structured formatting prints out
temperature=0.7,
stream=False,
)
return response.choices[0].message.content.strip()
except Exception as e:
print("Error details:", str(e))
return f"Error: {str(e)}"
# Define Custom Theme & Layout
with gr.Blocks(theme=gr.themes.Soft()) as demo:
# App Header Block
with gr.Row():
gr.HTML(
"""
<div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;">
<h1 style="color: #4F46E5; margin-bottom: 10px;">GlowGuide💗🌟</h1>
<p style="font-size: 1.1rem; color: #4B5563;">A safe space to navigate social pressure, handle anxiety, and set confident boundaries.</p>
</div>
"""
)
# Main Interface Split
with gr.Row():
# Left Column: Chat Area
with gr.Column(scale=3):
chat_ui = gr.ChatInterface(
fn=respond,
examples=[
[
"My friends are pressuring me to go out tonight but I am completely exhausted."
],
[
"I feel overwhelmed by workplace hustle culture and can't say no to extra tasks."
],
[
"I feel guilty for not meeting my family's career expectations."
],
],
)
# Right Column: Quick Resources & Safety Disclaimers
with gr.Column(scale=1):
with gr.Accordion("🚨 Emergency Crisis Resources", open=True):
gr.Markdown(
"""
If you are experiencing severe distress or self-harm thoughts, please reach out for immediate professional help:
* **International:** Find local support at [Befrienders Worldwide](https://befrienders.org)
* **Support:** Contact a suicide and crisis hotline.
* **UK:** Contact NHS mental health services.
"""
)
with gr.Accordion("🔒 Safe Space Disclaimer", open=True):
gr.Markdown(
"""
* **Privacy:** This tool does not store your conversation data.
* **Nature:** This tool acts as an AI peer coach, **not** a licensed therapist or medical professional.
"""
)
if __name__ == "__main__":
demo.launch()