Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,11 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
English Helper - Hugging Face Spaces
|
| 4 |
-
Sistema de aprendizado de inglês com IA - Flask como aplicação
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
-
import gradio as gr
|
| 9 |
-
import threading
|
| 10 |
-
import time
|
| 11 |
-
|
| 12 |
-
def start_gradio_dashboard():
|
| 13 |
-
"""Iniciar dashboard Gradio em background"""
|
| 14 |
-
try:
|
| 15 |
-
# Criar interface Gradio
|
| 16 |
-
demo = create_gradio_interface()
|
| 17 |
-
|
| 18 |
-
# Executar Gradio em background
|
| 19 |
-
port = int(os.environ.get("GRADIO_PORT", 7861))
|
| 20 |
-
print(f"📊 Iniciando Gradio Dashboard na porta {port}")
|
| 21 |
-
demo.launch(
|
| 22 |
-
server_name="0.0.0.0",
|
| 23 |
-
server_port=port,
|
| 24 |
-
share=False,
|
| 25 |
-
debug=False,
|
| 26 |
-
show_error=True,
|
| 27 |
-
quiet=True
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
except Exception as e:
|
| 31 |
-
print(f"❌ Erro no Gradio: {e}")
|
| 32 |
|
| 33 |
def init_system():
|
| 34 |
"""Inicializar sistema completo"""
|
|
@@ -46,128 +23,80 @@ def init_system():
|
|
| 46 |
print(f"❌ Erro na inicialização: {e}")
|
| 47 |
return False
|
| 48 |
|
| 49 |
-
def get_system_status():
|
| 50 |
-
"""Status do sistema"""
|
| 51 |
-
|
| 52 |
-
# Verificar APIs
|
| 53 |
-
groq_status = "🟢 OK" if os.getenv('GROQ_API_KEY') else "🔴 Missing"
|
| 54 |
-
gemini_status = "🟢 OK" if os.getenv('GEMINI_API_KEY') else "🔴 Missing"
|
| 55 |
-
admin_status = "🟢 OK" if os.getenv('ADMIN_USERNAME') else "🔴 Missing"
|
| 56 |
-
|
| 57 |
-
# Verificar modo de operação
|
| 58 |
-
port = os.environ.get("PORT", "7860")
|
| 59 |
-
|
| 60 |
-
status = f"""
|
| 61 |
-
## 🎓 English Helper - System Status
|
| 62 |
-
|
| 63 |
-
### 🏗️ **Arquitetura**
|
| 64 |
-
- **Modo**: Flask Principal + Gradio Dashboard
|
| 65 |
-
- **Porta Principal**: {port} (Flask App)
|
| 66 |
-
- **Dashboard**: 7861 (Gradio)
|
| 67 |
-
|
| 68 |
-
### 📊 **APIs**
|
| 69 |
-
- **Groq API**: {groq_status}
|
| 70 |
-
- **Gemini API**: {gemini_status}
|
| 71 |
-
- **Admin Panel**: {admin_status}
|
| 72 |
-
|
| 73 |
-
### 🌐 **Acesso Direto**
|
| 74 |
-
- **[🎓 Aplicação Principal →](../)**
|
| 75 |
-
- **[👤 Admin Panel →](../admin)**
|
| 76 |
-
- **[⚕️ Health Check →](../auth/check)**
|
| 77 |
-
|
| 78 |
-
### ✅ **Sistema Funcionando**
|
| 79 |
-
✓ Flask rodando na porta principal ({port})
|
| 80 |
-
✓ Database SQLite inicializado
|
| 81 |
-
✓ Dashboard Gradio na porta 7861
|
| 82 |
-
✓ Rotas e templates carregados
|
| 83 |
-
|
| 84 |
-
### 🔧 **Secrets Necessários**
|
| 85 |
-
- GROQ_API_KEY
|
| 86 |
-
- GEMINI_API_KEY
|
| 87 |
-
- SECRET_KEY
|
| 88 |
-
- ADMIN_USERNAME
|
| 89 |
-
- ADMIN_PASSWORD
|
| 90 |
-
"""
|
| 91 |
-
|
| 92 |
-
return status
|
| 93 |
-
|
| 94 |
-
def create_gradio_interface():
|
| 95 |
-
"""Criar interface Gradio Dashboard"""
|
| 96 |
-
|
| 97 |
-
with gr.Blocks(
|
| 98 |
-
title="English Helper - System Dashboard",
|
| 99 |
-
theme=gr.themes.Soft()
|
| 100 |
-
) as demo:
|
| 101 |
-
|
| 102 |
-
gr.HTML("""
|
| 103 |
-
<div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
| 104 |
-
<h1>📊 English Helper - Dashboard</h1>
|
| 105 |
-
<p>Sistema de monitoramento e status</p>
|
| 106 |
-
</div>
|
| 107 |
-
""")
|
| 108 |
-
|
| 109 |
-
with gr.Tab("📊 System Status"):
|
| 110 |
-
status_display = gr.Markdown(get_system_status())
|
| 111 |
-
refresh_btn = gr.Button("🔄 Refresh Status", variant="primary")
|
| 112 |
-
refresh_btn.click(fn=get_system_status, outputs=status_display)
|
| 113 |
-
|
| 114 |
-
with gr.Tab("� Quick Access"):
|
| 115 |
-
gr.Markdown("""
|
| 116 |
-
## 🎯 Acesso Rápido:
|
| 117 |
-
|
| 118 |
-
### 🎓 **[ACESSAR APLICAÇÃO PRINCIPAL →](../)**
|
| 119 |
-
Acesse a aplicação completa de aprendizado de inglês
|
| 120 |
-
|
| 121 |
-
### 👤 **[PAINEL ADMINISTRATIVO →](../admin)**
|
| 122 |
-
Acesso ao painel de administração do sistema
|
| 123 |
-
|
| 124 |
-
### ⚕️ **[VERIFICAR STATUS →](../auth/check)**
|
| 125 |
-
Verificação de saúde e status dos serviços
|
| 126 |
-
""")
|
| 127 |
-
|
| 128 |
-
gr.Markdown("""
|
| 129 |
-
## 🔧 Funcionalidades Principais:
|
| 130 |
-
- 🤖 **Chat IA**: Conversação inteligente com IA
|
| 131 |
-
- 📚 **Flashcards**: Sistema de vocabulário personalizado
|
| 132 |
-
- 📰 **Curadoria**: Artigos organizados por nível
|
| 133 |
-
- 📅 **Planos**: Cronograma de estudos personalizado
|
| 134 |
-
- 📊 **Analytics**: Dashboard completo de progresso
|
| 135 |
-
- � **Usuários**: Sistema de autenticação e progresso
|
| 136 |
-
""")
|
| 137 |
-
|
| 138 |
-
return demo
|
| 139 |
-
|
| 140 |
# Executar aplicação
|
| 141 |
if __name__ == "__main__":
|
| 142 |
print("🎯 Iniciando English Helper no Hugging Face Spaces...")
|
| 143 |
-
print("🔄 Modo: Flask como aplicação
|
| 144 |
|
| 145 |
# Inicializar sistema
|
| 146 |
if not init_system():
|
| 147 |
print("❌ Falha na inicialização do sistema")
|
| 148 |
exit(1)
|
| 149 |
|
| 150 |
-
# Iniciar Gradio Dashboard em background
|
| 151 |
-
gradio_thread = threading.Thread(target=start_gradio_dashboard, daemon=True)
|
| 152 |
-
gradio_thread.start()
|
| 153 |
-
|
| 154 |
-
# Aguardar inicialização
|
| 155 |
-
time.sleep(2)
|
| 156 |
-
|
| 157 |
# Importar e executar Flask como aplicação principal
|
| 158 |
try:
|
| 159 |
from flask_app import app
|
| 160 |
|
| 161 |
-
# Adicionar rota para dashboard
|
| 162 |
@app.route('/dashboard')
|
| 163 |
-
def
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
# Executar Flask na porta principal do HF Spaces
|
| 167 |
port = int(os.environ.get("PORT", 7860))
|
| 168 |
-
print(f"🌐 Flask rodando na porta
|
| 169 |
-
print(f"📊 Dashboard Gradio: porta 7861")
|
| 170 |
print(f"🎓 Aplicação principal: http://localhost:{port}/")
|
|
|
|
|
|
|
| 171 |
|
| 172 |
app.run(
|
| 173 |
host="0.0.0.0",
|
|
@@ -178,4 +107,4 @@ if __name__ == "__main__":
|
|
| 178 |
|
| 179 |
except Exception as e:
|
| 180 |
print(f"❌ Erro no Flask: {e}")
|
| 181 |
-
exit(1)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
English Helper - Hugging Face Spaces
|
| 4 |
+
Sistema de aprendizado de inglês com IA - Flask como aplicação única
|
| 5 |
+
SDK Gradio é usado apenas para compatibilidade com HF Spaces
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def init_system():
|
| 11 |
"""Inicializar sistema completo"""
|
|
|
|
| 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",
|
|
|
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
print(f"❌ Erro no Flask: {e}")
|
| 110 |
+
exit(1)
|