#!/usr/bin/env python3 """ 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 # Add project root to Python path project_root = Path(__file__).parent.absolute() sys.path.insert(0, str(project_root)) # Set environment variables for Hugging Face Spaces 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' # Global variables current_user = None chat_history = [] DEMO_MODE = True # Set to demo mode due to dependency issues 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, "" # Add user message to history history.append([message, None]) # Demo responses 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?" } # Simple keyword matching for demo 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 # Add response to history 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. """) # Event handlers 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") # Create and launch Gradio interface 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()