|
|
|
|
|
""" |
|
|
Hugging Face Spaces Docker Entry Point for Mental Health Chatbot |
|
|
Simplified version without dependency conflicts |
|
|
""" |
|
|
|
|
|
import gradio as gr |
|
|
import os |
|
|
import sys |
|
|
import time |
|
|
import threading |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
project_root = Path(__file__).parent.absolute() |
|
|
sys.path.insert(0, str(project_root)) |
|
|
|
|
|
|
|
|
os.environ['HUGGINGFACE_SPACES'] = '1' |
|
|
os.environ['GRADIO_SERVER_NAME'] = '0.0.0.0' |
|
|
os.environ['GRADIO_SERVER_PORT'] = '7860' |
|
|
os.environ['PYTHONUNBUFFERED'] = '1' |
|
|
os.environ['FLASK_ENV'] = 'production' |
|
|
|
|
|
|
|
|
current_user = None |
|
|
chat_history = [] |
|
|
DEMO_MODE = True |
|
|
|
|
|
def create_user_session(name="Guest"): |
|
|
"""Create a user session""" |
|
|
global current_user |
|
|
current_user = { |
|
|
'id': f'gradio_user_{int(time.time())}', |
|
|
'name': name, |
|
|
'session_id': f'session_{int(time.time())}' |
|
|
} |
|
|
return f"Welcome {name}! You're now in a demo session." |
|
|
|
|
|
def process_chat_message(message, history): |
|
|
"""Process chat message - demo version""" |
|
|
global current_user |
|
|
|
|
|
if not current_user: |
|
|
create_user_session("Guest") |
|
|
|
|
|
if not message.strip(): |
|
|
return history, "" |
|
|
|
|
|
|
|
|
history.append([message, None]) |
|
|
|
|
|
|
|
|
demo_responses = { |
|
|
"hello": "Hello! I'm a mental health AI assistant. While this is currently a demo version, I'm here to provide support and information.", |
|
|
"help": "I can help you with mental health resources, coping strategies, and provide a supportive conversation. What would you like to talk about?", |
|
|
"anxiety": "Anxiety is very common and treatable. Some helpful techniques include deep breathing, mindfulness, and grounding exercises. Would you like me to guide you through a breathing exercise?", |
|
|
"depression": "I understand you may be dealing with difficult feelings. It's important to know that help is available and you're not alone. Consider reaching out to a mental health professional.", |
|
|
"crisis": "If you're in crisis, please reach out immediately: National Suicide Prevention Lifeline: 988, Crisis Text Line: Text HOME to 741741, or contact your local emergency services.", |
|
|
"default": "Thank you for sharing that with me. While this is currently a demo version, I want you to know that your mental health matters. Can you tell me more about how you're feeling?" |
|
|
} |
|
|
|
|
|
|
|
|
message_lower = message.lower() |
|
|
response = demo_responses["default"] |
|
|
|
|
|
for keyword, demo_response in demo_responses.items(): |
|
|
if keyword in message_lower: |
|
|
response = demo_response |
|
|
break |
|
|
|
|
|
|
|
|
history[-1][1] = response |
|
|
|
|
|
return history, "" |
|
|
|
|
|
def create_interface(): |
|
|
"""Create the Gradio interface""" |
|
|
|
|
|
css = """ |
|
|
.gradio-container { |
|
|
max-width: 1200px !important; |
|
|
margin: auto !important; |
|
|
} |
|
|
.demo-notice { |
|
|
background: #fff3cd !important; |
|
|
color: #856404 !important; |
|
|
padding: 15px !important; |
|
|
border-radius: 10px !important; |
|
|
margin: 10px 0 !important; |
|
|
border: 1px solid #ffeaa7 !important; |
|
|
} |
|
|
""" |
|
|
|
|
|
with gr.Blocks(css=css, title="Mental Health AI Assistant - Demo") as interface: |
|
|
|
|
|
gr.Markdown(""" |
|
|
# π§ Mental Health AI Assistant (Demo) |
|
|
### Docker Deployment Ready for Hugging Face Spaces |
|
|
|
|
|
**β οΈ Demo Mode**: This is a simplified demo version with basic responses while we resolve dependency conflicts. |
|
|
The full AI-powered version with CrewAI agents will be available once all dependencies are compatible. |
|
|
""", elem_classes="demo-notice") |
|
|
|
|
|
with gr.Tabs(): |
|
|
|
|
|
with gr.Tab("π¬ Chat Demo"): |
|
|
gr.Markdown(""" |
|
|
### Chat with the Mental Health Assistant |
|
|
This demo provides supportive responses and mental health resources. |
|
|
Try keywords like: *hello*, *help*, *anxiety*, *depression*, *crisis* |
|
|
""") |
|
|
|
|
|
chatbot = gr.Chatbot( |
|
|
label="Mental Health Assistant (Demo)", |
|
|
height=500, |
|
|
show_copy_button=True, |
|
|
bubble_full_width=False |
|
|
) |
|
|
|
|
|
with gr.Row(): |
|
|
msg_input = gr.Textbox( |
|
|
placeholder="Type your message here... (Demo mode with keyword responses)", |
|
|
show_label=False, |
|
|
scale=4 |
|
|
) |
|
|
send_btn = gr.Button("Send", variant="primary", scale=1) |
|
|
|
|
|
with gr.Row(): |
|
|
clear_btn = gr.Button("Clear Chat", variant="secondary") |
|
|
new_session_btn = gr.Button("Start New Session", variant="primary") |
|
|
|
|
|
session_info = gr.Textbox( |
|
|
value="Click 'Start New Session' to begin", |
|
|
label="Session Status", |
|
|
interactive=False |
|
|
) |
|
|
|
|
|
with gr.Tab("ποΈ Architecture"): |
|
|
gr.Markdown(""" |
|
|
### Deployment Architecture |
|
|
|
|
|
**Current Status**: Demo Mode β
(Dependencies being resolved) |
|
|
|
|
|
**Target Architecture**: |
|
|
``` |
|
|
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ |
|
|
β Gradio UI β β Flask App β β FastAPI App β |
|
|
β (Port 7860) βββββΊβ (Port 5001) βββββΊβ (Port 8001) β |
|
|
β app.py β β main.py β β fastapi_app.py β |
|
|
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ |
|
|
β β β |
|
|
βββββββββββββββββββββββββΌββββββββββββββββββββββββ |
|
|
β |
|
|
βββββββββββββββββββ |
|
|
β SQLite Databaseβ |
|
|
β Vector Store β |
|
|
β Session Data β |
|
|
βββββββββββββββββββ |
|
|
``` |
|
|
|
|
|
**Docker Features**: |
|
|
- β
Python 3.11 base image |
|
|
- β
System dependencies (FFmpeg, GCC) |
|
|
- β
Gradio interface on port 7860 |
|
|
- β
Health checks and monitoring |
|
|
- β
Multi-service architecture ready |
|
|
|
|
|
**What's Working**: |
|
|
- Docker container builds successfully |
|
|
- Gradio interface loads and functions |
|
|
- Basic chat functionality (demo responses) |
|
|
- Session management |
|
|
- Proper port configuration for Spaces |
|
|
|
|
|
**Next Steps**: |
|
|
1. Resolve LangChain/CrewAI dependency conflicts |
|
|
2. Enable full AI agent functionality |
|
|
3. Integrate Flask and FastAPI backends |
|
|
4. Add voice processing capabilities |
|
|
5. Enable mental health assessments |
|
|
""") |
|
|
|
|
|
with gr.Tab("π Deployment"): |
|
|
gr.Markdown(""" |
|
|
### Hugging Face Spaces Deployment Status |
|
|
|
|
|
**β
Ready to Deploy**: This application is ready for Hugging Face Spaces! |
|
|
|
|
|
**Files Configured**: |
|
|
- β
`Dockerfile` - Docker container configuration |
|
|
- β
`app.py` - Gradio entry point (this file) |
|
|
- β
`requirements.txt` - Minimal dependencies (no conflicts) |
|
|
- β
`README.md` - With proper Spaces header |
|
|
- β
`.dockerignore` - Build optimization |
|
|
|
|
|
**Deployment Steps**: |
|
|
1. Create a new Space on [Hugging Face Spaces](https://huggingface.co/spaces) |
|
|
2. Choose **Docker** as the SDK |
|
|
3. Upload your code to the Space repository |
|
|
4. Set environment variables in Space settings: |
|
|
``` |
|
|
GOOGLE_API_KEY=your_api_key |
|
|
SECRET_KEY=your_secret_key |
|
|
``` |
|
|
5. Watch the automatic build and deployment |
|
|
|
|
|
**What Happens During Deployment**: |
|
|
- Docker builds the container with Python 3.11 |
|
|
- Installs minimal, conflict-free dependencies |
|
|
- Starts Gradio on port 7860 |
|
|
- Application becomes available at your Space URL |
|
|
|
|
|
**Current Status**: Demo Mode |
|
|
- Basic chat functionality works |
|
|
- No dependency conflicts |
|
|
- Ready for incremental AI feature additions |
|
|
""") |
|
|
|
|
|
with gr.Tab("π Resources"): |
|
|
gr.Markdown(""" |
|
|
### Mental Health Resources |
|
|
|
|
|
**π Crisis Support** |
|
|
- **National Suicide Prevention Lifeline**: 988 |
|
|
- **Crisis Text Line**: Text HOME to 741741 |
|
|
- **International Crisis Lines**: https://findahelpline.com |
|
|
|
|
|
**π₯ Professional Help** |
|
|
- Psychology Today: Find a therapist directory |
|
|
- BetterHelp, Talkspace: Online therapy platforms |
|
|
- NAMI: National Alliance on Mental Illness |
|
|
|
|
|
**π± Mental Health Apps** |
|
|
- Headspace, Calm: Meditation and mindfulness |
|
|
- Sanvello: Anxiety and mood tracking |
|
|
- MindShift: CBT-based anxiety management |
|
|
|
|
|
**π Educational Resources** |
|
|
- Mental Health America: mhanational.org |
|
|
- NIMH: National Institute of Mental Health |
|
|
- CDC Mental Health: cdc.gov/mentalhealth |
|
|
|
|
|
**β οΈ Important Disclaimer** |
|
|
This is an AI assistant for support and information only. |
|
|
It is not a replacement for professional mental health care. |
|
|
Always consult healthcare providers for serious concerns. |
|
|
""") |
|
|
|
|
|
|
|
|
send_btn.click(process_chat_message, [msg_input, chatbot], [chatbot, msg_input]) |
|
|
msg_input.submit(process_chat_message, [msg_input, chatbot], [chatbot, msg_input]) |
|
|
clear_btn.click(lambda: [], outputs=[chatbot]) |
|
|
new_session_btn.click(create_user_session, outputs=[session_info]) |
|
|
|
|
|
return interface |
|
|
|
|
|
def main(): |
|
|
"""Main function to start the application""" |
|
|
print("π Starting Mental Health AI Assistant (Demo Mode) for Hugging Face Spaces...") |
|
|
print("π Status: Dependency conflicts resolved, basic functionality working") |
|
|
print("π― Mode: Demo with keyword-based responses") |
|
|
|
|
|
|
|
|
print("π¨ Creating Gradio interface...") |
|
|
interface = create_interface() |
|
|
|
|
|
print("π Launching on Hugging Face Spaces (Docker)...") |
|
|
interface.launch( |
|
|
server_name="0.0.0.0", |
|
|
server_port=7860, |
|
|
share=False, |
|
|
debug=False, |
|
|
show_error=True, |
|
|
enable_queue=True |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|