| import gradio as gr |
| from huggingface_hub import InferenceClient |
|
|
| |
| client = InferenceClient("Qwen/Qwen2.5-7B-Instruct") |
|
|
| def responder(mensaje, historial, sistema, temperatura): |
| |
| mensajes_api = [{"role": "system", "content": sistema}] |
| |
| |
| for msg in historial: |
| mensajes_api.append({"role": "user", "content": msg[0]}) |
| if msg[1] is not None: |
| mensajes_api.append({"role": "assistant", "content": msg[1]}) |
| |
| |
| mensajes_api.append({"role": "user", "content": mensaje}) |
| |
| respuesta_generada = "" |
| try: |
| |
| stream = client.chat_completion( |
| mensajes_api, |
| max_tokens=1024, |
| temperature=temperatura, |
| stream=True |
| ) |
| for chunk in stream: |
| texto = chunk.choices[0].delta.content |
| if texto is not None: |
| respuesta_generada += texto |
| yield respuesta_generada |
| except Exception as e: |
| yield f"⚠️ Error de conexión con el servidor: {str(e)}" |
|
|
| |
| with gr.Blocks(theme=gr.themes.Ocean()) as demo: |
| gr.Markdown("<h1 style='text-align: center;'>🤖 Mi Inteligencia Artificial Privada</h1>") |
| |
| with gr.Row(): |
| |
| with gr.Column(scale=1, variant="panel"): |
| gr.Markdown("### ⚙️ Ajustes del Chat") |
| |
| sistema_input = gr.Textbox( |
| value="Eres un asistente ultra-inteligente, amigable y experto. Respondes siempre en español.", |
| label="Instrucciones de la IA (Personalidad)", |
| lines=3 |
| ) |
| |
| temp_input = gr.Slider(minimum=0.1, maximum=1.5, value=0.7, step=0.1, label="Nivel de Creatividad") |
| |
| gr.Markdown("---") |
| gr.Markdown("### 🛠️ Herramientas") |
| |
| |
| btn_limpiar = gr.Button("🗑️ Limpiar Conversación", variant="secondary") |
| |
| |
| with gr.Accordion("ℹ️ Información del Servidor", open=False): |
| gr.Markdown("**Host:** Hugging Face Spaces\n**Motor IA:** Qwen 2.5 (7B)\n**Estado:** Online") |
| |
| |
| with gr.Column(scale=3): |
| chatbot = gr.Chatbot(height=500, label="Chat en vivo", bubble_full_width=False) |
| |
| with gr.Row(): |
| mensaje_input = gr.Textbox(placeholder="Escribe tu mensaje aquí y presiona Enter...", show_label=False, scale=4) |
| btn_enviar = gr.Button("Enviar 🚀", variant="primary", scale=1) |
|
|
| |
| |
| def procesar_mensaje(mensaje_usuario, historial): |
| |
| if not mensaje_usuario.strip(): |
| return "", historial |
| return "", historial + [[mensaje_usuario, None]] |
|
|
| def generar_respuesta(historial, sistema, temperatura): |
| |
| if not historial or historial[-1][0] is None: |
| yield historial |
| return |
| |
| mensaje_usuario = historial[-1][0] |
| historial_previo = historial[:-1] |
| |
| generador = responder(mensaje_usuario, historial_previo, sistema, temperatura) |
| |
| for texto_parcial in generador: |
| historial[-1][1] = texto_parcial |
| yield historial |
|
|
| |
| mensaje_input.submit( |
| procesar_mensaje, [mensaje_input, chatbot], [mensaje_input, chatbot], queue=False |
| ).then( |
| generar_respuesta, [chatbot, sistema_input, temp_input], chatbot |
| ) |
| |
| |
| btn_enviar.click( |
| procesar_mensaje, [mensaje_input, chatbot], [mensaje_input, chatbot], queue=False |
| ).then( |
| generar_respuesta, [chatbot, sistema_input, temp_input], chatbot |
| ) |
| |
| |
| btn_limpiar.click(lambda: None, None, chatbot, queue=False) |
|
|
| if __name__ == "__main__": |
| demo.launch() |