File size: 10,267 Bytes
a308962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import gradio as gr
import os
import json
from datetime import datetime
import logging

# Configuración de logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Importar módulos personalizados
from model_manager import ModelManager
from api_agent import APIAgent
from prompt_generator import PromptGenerator

# Inicializar componentes
model_manager = ModelManager()
api_agent = APIAgent()
prompt_generator = PromptGenerator()

class BATUTOChatbot:
    def __init__(self):
        self.conversation_history = []
        self.config = {
            "deepseek_api_key": "",
            "openai_api_key": "", 
            "max_tokens": 400,
            "temperature": 0.7
        }
    
    def update_config(self, deepseek_key, openai_key, max_tokens, temperature):
        """Actualiza la configuración desde la UI"""
        updated = False
        
        if deepseek_key:
            self.config["deepseek_api_key"] = deepseek_key
            updated = True
        if openai_key:
            self.config["openai_api_key"] = openai_key
            updated = True
        if max_tokens:
            self.config["max_tokens"] = int(max_tokens)
            updated = True
        if temperature:
            self.config["temperature"] = float(temperature)
            updated = True
            
        # Actualizar agentes
        model_manager.set_config(self.config)
        api_agent.set_config(self.config)
        
        return "✅ Configuración actualizada" if updated else "ℹ️ Sin cambios"
    
    def get_system_status(self):
        """Obtiene el estado del sistema"""
        has_deepseek = bool(self.config.get("deepseek_api_key"))
        has_openai = bool(self.config.get("openai_api_key"))
        models_loaded = model_manager.loaded
        
        status_html = f"""
        <div style='padding: 15px; border-radius: 10px; background: #f8f9fa; border: 2px solid #e9ecef;'>
            <h4 style='margin-top: 0;'>🔧 Estado del Sistema</h4>
            <p><strong>Modelos locales:</strong> {'✅ Cargados' if models_loaded else '🔄 Cargando...'}</p>
            <p><strong>DeepSeek API:</strong> {'✅ Configurada' if has_deepseek else '❌ No configurada'}</p>
            <p><strong>OpenAI API:</strong> {'✅ Configurada' if has_openai else '❌ No configurada'}</p>
            <p><strong>Mensajes en sesión:</strong> {len(self.conversation_history)}</p>
        </div>
        """
        return status_html
    
    def chat_response(self, message, history):
        """Genera respuesta del chatbot optimizado para HF"""
        if not message.strip():
            return ""
        
        # Mostrar indicador de typing
        yield "🔄 Procesando..."
        
        try:
            # Detectar intención y mejorar prompt
            intent = prompt_generator.detect_intent(message)
            enhanced_prompt = prompt_generator.enhance_prompt(message, intent)
            
            # Intentar usar APIs primero
            api_result = api_agent.generate_response(enhanced_prompt, intent["is_code"])
            
            if api_result["response"]:
                # Usar respuesta de API
                response_text = api_result["response"]
                source = api_result["source"]
            else:
                # Usar modelo local como fallback
                response_text = model_manager.generate_local_response(
                    enhanced_prompt, 
                    intent["is_code"],
                    max_length=200  # Reducido para HF
                )
                source = "local"
            
            # Agregar metadata a la respuesta
            metadata = f"\n\n---\n*🔧 Fuente: {source.upper()}*"
            if intent["is_code"]:
                metadata += f" | 💻 *Tipo: Código*"
            else:
                metadata += f" | 💬 *Tipo: Conversación*"
            
            full_response = response_text + metadata
            
            # Guardar en historial
            self.conversation_history.append({
                "timestamp": datetime.now().isoformat(),
                "user": message,
                "bot": response_text,
                "source": source,
                "intent": intent
            })
            
            yield full_response
            
        except Exception as e:
            error_msg = f"❌ Error: {str(e)}"
            logger.error(f"Error en chat_response: {e}")
            yield error_msg
    
    def clear_conversation(self):
        """Limpia la conversación"""
        self.conversation_history.clear()
        return None, []

# Crear instancia del chatbot
chatbot = BATUTOChatbot()

# Cargar modelos al inicio (async)
def load_models_async():
    logger.info("🔄 Cargando modelos en segundo plano...")
    model_manager.load_models()
    logger.info("✅ Modelos cargados exitosamente")

# Iniciar carga de modelos
import threading
model_loader = threading.Thread(target=load_models_async, daemon=True)
model_loader.start()

# Configuración de la interfaz Gradio para HF
with gr.Blocks(
    title="BATUTO Chatbot - Asistente Educativo",
    theme=gr.themes.Soft(),
    css="""
    .gradio-container {
        max-width: 1000px !important;
        margin: auto;
    }
    .chat-container {
        height: 500px;
    }
    .status-panel {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        padding: 20px;
        border-radius: 10px;
        color: white;
    }
    """
) as demo:
    
    gr.Markdown("""
    # 🤖 BATUTO Chatbot - Asistente Educativo
    **Sistema inteligente con modelos locales y APIs externas**  
    *Desplegado en Hugging Face Spaces - Versión Optimizada*
    """)
    
    with gr.Row():
        with gr.Column(scale=2):
            # Área de chat
            gr.Markdown("### 💬 Conversación")
            chatbot_interface = gr.Chatbot(
                label="Chat con BATUTO",
                height=400,
                show_copy_button=True,
                container=True
            )
            msg = gr.Textbox(
                label="Escribe tu mensaje",
                placeholder="Pregunta sobre programación, explica conceptos, pide ejemplos...",
                lines=2,
                max_lines=4
            )
            
            with gr.Row():
                submit_btn = gr.Button("🚀 Enviar", variant="primary")
                clear_btn = gr.Button("🧹 Limpiar", variant="secondary")
        
        with gr.Column(scale=1):
            # Panel de estado
            gr.Markdown("### 📊 Estado del Sistema")
            status_display = gr.HTML()
            
            # Configuración rápida
            with gr.Accordion("⚙️ Configuración Rápida", open=False):
                with gr.Group():
                    deepseek_key = gr.Textbox(
                        label="DeepSeek API Key",
                        type="password",
                        placeholder="sk-...",
                        info="Opcional - para respuestas mejoradas"
                    )
                    openai_key = gr.Textbox(
                        label="OpenAI API Key", 
                        type="password",
                        placeholder="sk-...",
                        info="Opcional - alternativa"
                    )
                    
                    with gr.Row():
                        max_tokens = gr.Slider(
                            label="Tokens máx",
                            minimum=100,
                            maximum=800,
                            value=400,
                            step=50
                        )
                        temperature = gr.Slider(
                            label="Temperatura",
                            minimum=0.1,
                            maximum=1.0,
                            value=0.7,
                            step=0.1
                        )
                    
                    save_config_btn = gr.Button("💾 Guardar Config", size="sm")
                    config_output = gr.Textbox(label="Estado", interactive=False)
            
            # Información
            with gr.Accordion("ℹ️ Cómo usar", open=True):
                gr.Markdown("""
                **Ejemplos:**
                - 💻 *"Muéstrame una función Python para ordenar listas"*
                - 💬 *"Explica qué es machine learning"*  
                - 🔧 *"Corrige este código: [tu código]"*
                
                **Fuentes:**
                1. 🚀 DeepSeek API (si se configura)
                2. ⚡ OpenAI API (si se configura)  
                3. 🤖 Modelos locales (fallback)
                """)
    
    # Event handlers
    def handle_submit(message, history):
        if not message.strip():
            return "", history
        return "", history + [[message, None]]
    
    # Conectar el botón de enviar
    submit_btn.click(
        handle_submit,
        inputs=[msg, chatbot_interface],
        outputs=[msg, chatbot_interface]
    ).then(
        chatbot.chat_response,
        inputs=[msg, chatbot_interface],
        outputs=[chatbot_interface]
    )
    
    # Enter también envía
    msg.submit(
        handle_submit,
        inputs=[msg, chatbot_interface],
        outputs=[msg, chatbot_interface]
    ).then(
        chatbot.chat_response,
        inputs=[msg, chatbot_interface],
        outputs=[chatbot_interface]
    )
    
    # Limpiar chat
    clear_btn.click(
        chatbot.clear_conversation,
        outputs=[msg, chatbot_interface]
    )
    
    # Configuración
    save_config_btn.click(
        chatbot.update_config,
        inputs=[deepseek_key, openai_key, max_tokens, temperature],
        outputs=[config_output]
    ).then(
        chatbot.get_system_status,
        outputs=[status_display]
    )
    
    # Actualizar estado al cargar
    demo.load(
        chatbot.get_system_status,
        outputs=[status_display]
    )

# Configuración específica para Hugging Face Spaces
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True,
        show_error=True,
        debug=False,
        favicon_path=None
    )