amauricunha commited on
Commit
40352af
·
verified ·
1 Parent(s): 4566cb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -67
app.py CHANGED
@@ -6,6 +6,9 @@ SDK Gradio é usado apenas para compatibilidade com HF Spaces
6
  """
7
 
8
  import os
 
 
 
9
 
10
  def init_system():
11
  """Inicializar sistema completo"""
@@ -23,86 +26,59 @@ def init_system():
23
  print(f"❌ Erro na inicialização: {e}")
24
  return False
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # Executar aplicação
27
  if __name__ == "__main__":
28
  print("🎯 Iniciando English Helper no Hugging Face Spaces...")
29
- print("🔄 Modo: Flask como aplicação única")
30
 
31
  # Inicializar sistema
32
  if not init_system():
33
  print("❌ Falha na inicialização do sistema")
34
  exit(1)
35
 
36
- # Importar e executar Flask como aplicação principal
37
  try:
38
- from flask_app import app
 
 
 
 
 
 
 
 
 
39
 
40
- # Adicionar rota para dashboard simples (HTML puro)
41
- @app.route('/dashboard')
42
- def dashboard_simple():
43
- groq_status = "🟢 OK" if os.getenv('GROQ_API_KEY') else "🔴 Missing"
44
- gemini_status = "🟢 OK" if os.getenv('GEMINI_API_KEY') else "🔴 Missing"
45
- admin_status = "🟢 OK" if os.getenv('ADMIN_USERNAME') else "🔴 Missing"
46
- port = os.environ.get("PORT", "7860")
47
-
48
- return f'''<!DOCTYPE html>
49
- <html>
50
- <head>
51
- <title>English Helper - System Dashboard</title>
52
- <style>
53
- body {{ font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }}
54
- .card {{ background: white; padding: 20px; border-radius: 10px; margin: 20px 0; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }}
55
- h1 {{ color: #4f46e5; }}
56
- h2 {{ color: #374151; }}
57
- a {{ color: #4f46e5; text-decoration: none; font-weight: 500; }}
58
- a:hover {{ text-decoration: underline; }}
59
- .status {{ font-weight: bold; }}
60
- .container {{ max-width: 800px; margin: 0 auto; }}
61
- </style>
62
- </head>
63
- <body>
64
- <div class="container">
65
- <h1>🎓 English Helper - System Dashboard</h1>
66
-
67
- <div class="card">
68
- <h2>📊 System Status</h2>
69
- <p><strong>Groq API:</strong> <span class="status">{groq_status}</span></p>
70
- <p><strong>Gemini API:</strong> <span class="status">{gemini_status}</span></p>
71
- <p><strong>Admin Panel:</strong> <span class="status">{admin_status}</span></p>
72
- <p><strong>Mode:</strong> Single Flask Application</p>
73
- </div>
74
-
75
- <div class="card">
76
- <h2>🔗 Quick Links</h2>
77
- <p><a href="/">🎓 Main Application</a> - Complete English learning platform</p>
78
- <p><a href="/admin">👤 Admin Panel</a> - User management and analytics</p>
79
- <p><a href="/auth/check">⚕️ Health Check</a> - System status verification</p>
80
- </div>
81
-
82
- <div class="card">
83
- <h2>🏗️ Architecture Info</h2>
84
- <p><strong>Mode:</strong> Single Flask Application</p>
85
- <p><strong>Port:</strong> {port}</p>
86
- <p><strong>Database:</strong> SQLite with 10+ tables</p>
87
- <p><strong>APIs:</strong> Groq + Google Gemini</p>
88
- <p><strong>Features:</strong> AI Chat, Flashcards, Content Curation, Study Plans</p>
89
- </div>
90
- </div>
91
- </body>
92
- </html>'''
93
 
94
- # Executar Flask na porta principal do HF Spaces
95
- port = int(os.environ.get("PORT", 7860))
96
- print(f"🌐 Flask rodando na porta: {port}")
97
- print(f"🎓 Aplicação principal: http://localhost:{port}/")
98
- print(f"👤 Admin panel: http://localhost:{port}/admin")
99
- print(f"📊 Dashboard: http://localhost:{port}/dashboard")
100
 
101
- app.run(
102
- host="0.0.0.0",
103
- port=port,
104
- debug=False,
105
- threaded=True
 
106
  )
107
 
108
  except Exception as e:
 
6
  """
7
 
8
  import os
9
+ import gradio as gr
10
+ import threading
11
+ import time
12
 
13
  def init_system():
14
  """Inicializar sistema completo"""
 
26
  print(f"❌ Erro na inicialização: {e}")
27
  return False
28
 
29
+ def create_gradio_interface():
30
+ """Interface Gradio mínima para compatibilidade HF"""
31
+ with gr.Blocks(title="English Helper", theme=gr.themes.Soft()) as demo:
32
+ gr.HTML("""
33
+ <div style="text-align: center; padding: 40px;">
34
+ <h1>🎓 English Helper</h1>
35
+ <p>Sistema completo de aprendizado de inglês com IA</p>
36
+ <br>
37
+ <p><strong>⚠️ Redirecionamento automático em 3 segundos...</strong></p>
38
+ <script>
39
+ setTimeout(function() {
40
+ window.location.href = '/';
41
+ }, 3000);
42
+ </script>
43
+ </div>
44
+ """)
45
+ return demo
46
+
47
  # Executar aplicação
48
  if __name__ == "__main__":
49
  print("🎯 Iniciando English Helper no Hugging Face Spaces...")
 
50
 
51
  # Inicializar sistema
52
  if not init_system():
53
  print("❌ Falha na inicialização do sistema")
54
  exit(1)
55
 
56
+ # Modo HF Spaces: Gradio na porta principal, Flask em background
57
  try:
58
+ # Import limpo para evitar execução duplicada
59
+ import flask_app
60
+ app = flask_app.app
61
+
62
+ # Iniciar Flask em background na porta 5000
63
+ def start_flask():
64
+ app.run(host="0.0.0.0", port=5000, debug=False, threaded=True)
65
+
66
+ flask_thread = threading.Thread(target=start_flask, daemon=True)
67
+ flask_thread.start()
68
 
69
+ print("🌐 Flask iniciado na porta 5000")
70
+ time.sleep(2) # Aguardar Flask inicializar
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
+ # Criar interface Gradio na porta principal
73
+ demo = create_gradio_interface()
74
+ print("📊 Gradio na porta 7860 (HF Spaces)")
 
 
 
75
 
76
+ demo.launch(
77
+ server_name="0.0.0.0",
78
+ server_port=7860,
79
+ share=False,
80
+ debug=False,
81
+ show_error=True
82
  )
83
 
84
  except Exception as e: