File size: 4,620 Bytes
7b974d5 597b488 7b974d5 597b488 7b974d5 597b488 7b974d5 597b488 7b974d5 597b488 7b974d5 597b488 7b974d5 597b488 7b974d5 597b488 7b974d5 597b488 7b974d5 597b488 7b974d5 597b488 7b974d5 |
1 2 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#!/usr/bin/env python3
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'
# Import Flask app
from main import app as flask_app, get_chat_response, init_db
# Global variables
current_user = None
chat_history = []
def start_flask_app():
"""Start Flask app in background thread"""
print("π Starting Flask app on port 5001...")
try:
with flask_app.app_context():
init_db()
flask_app.run(host='127.0.0.1', port=5001, debug=False, threaded=True, use_reloader=False)
except Exception as e:
print(f"β Error starting Flask app: {e}")
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}! Ready to chat."
def process_chat_message(message, history):
"""Process chat message using Flask backend"""
global current_user
if not current_user:
create_user_session("Guest")
if not message.strip():
return history, ""
history.append([message, None])
try:
with flask_app.app_context():
response = get_chat_response(message, current_user.get('session_id', 'default'))
if isinstance(response, dict):
ai_response = response.get('response', 'Sorry, I encountered an error.')
else:
ai_response = str(response)
history[-1][1] = ai_response
except Exception as e:
print(f"β Error processing chat: {e}")
history[-1][1] = "I'm having trouble processing your message. Please try again."
return history, ""
def create_interface():
"""Create the Gradio interface"""
with gr.Blocks(title="Mental Health AI Assistant") as interface:
gr.Markdown("# π§ Mental Health AI Assistant\n### Powered by Flask + FastAPI + Gradio on Hugging Face Spaces")
with gr.Tabs():
with gr.Tab("π¬ Chat"):
chatbot = gr.Chatbot(label="Mental Health Assistant", height=500, show_copy_button=True)
with gr.Row():
msg_input = gr.Textbox(placeholder="Type your message here...", show_label=False, scale=4)
send_btn = gr.Button("Send", variant="primary", scale=1)
clear_btn = gr.Button("Clear Chat")
session_info = gr.Textbox(value="Click 'Start Session' to begin", label="Session")
new_session_btn = gr.Button("Start New Session")
with gr.Tab("βΉοΈ About"):
gr.Markdown("""
### About This Application
This Mental Health AI Assistant is deployed on Hugging Face Spaces using Docker.
**Architecture:**
- Frontend: Gradio interface (port 7860)
- Flask Backend: Main application logic (port 5001)
- FastAPI Backend: AI services (port 8001)
- Database: SQLite for data persistence
""")
# 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 all services"""
print("π Starting Mental Health AI Assistant for Hugging Face Spaces Docker...")
# Start Flask app in background
flask_thread = threading.Thread(target=start_flask_app, daemon=True)
flask_thread.start()
# Wait for Flask to start
print("β³ Waiting for Flask to initialize...")
time.sleep(3)
# 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()
|