#!/usr/bin/env python3 """ HuggingChat Client - Interfaz web para Hugging Face Spaces Esta aplicación proporciona una interfaz web usando Gradio para interactuar con el cliente de HuggingChat desarrollado en este proyecto. Autor: HuggingChat Client Team Fecha: Noviembre 2024 Licencia: MIT """ import os import sys from pathlib import Path import gradio as gr # Agregamos el directorio src al path para importar nuestro módulo project_root = Path(__file__).parent src_path = project_root / "src" sys.path.insert(0, str(src_path)) try: from huggingchat_client import HuggingChatClient except ImportError as e: print(f"❌ Error importando el cliente de HuggingChat: {e}") sys.exit(1) # Variable global para mantener el cliente client = None def initialize_client(): """Inicializa el cliente de HuggingChat""" global client try: client = HuggingChatClient() return "✅ Cliente inicializado correctamente" except Exception as e: # En Spaces, a veces la validación inicial falla pero el cliente funciona # Creamos el cliente de forma más básica para Spaces client = None # Lo inicializaremos en el primer mensaje return "⚠️ Cliente listo (se inicializará en el primer mensaje)" def chat_with_huggingchat(message, history): """ Función principal para el chat con HuggingChat Args: message (str): Mensaje del usuario history (list): Historial de la conversación Returns: tuple: (mensaje_vacío, historial_actualizado) """ global client if not message.strip(): return "", history try: # Inicializar cliente si no existe (inicialización diferida) if client is None: try: client = HuggingChatClient() except Exception as init_error: error_response = f"❌ Error de conexión: {str(init_error)[:100]}... Verifica tu token HF_TOKEN" history.append([message, error_response]) return "", history # Obtener respuesta del modelo response = client.send_message(message.strip()) # Actualizar historial history.append([message, response]) return "", history except Exception as e: error_response = f"❌ Error: {str(e)}" history.append([message, error_response]) return "", history def clear_conversation(): """Limpia el historial de conversación""" global client if client: client.clear_history() return [] def get_model_info(): """Obtiene información del modelo actual""" global client if client: return f"Modelo actual: {client.model}" return "❌ Cliente no inicializado" # Configuración de la interfaz Gradio def create_interface(): """Crea la interfaz web de Gradio""" with gr.Blocks(title="HuggingChat Client") as demo: gr.HTML("""
Explora las posibilidades conversacionales de HuggingChat,
Busca diferentes formas de comunicarte, investiga sobre ingeniería de prompting y experimenta con este chatbot.
Investiga acerca de hugchat (hugging chat API) y desarrolla un código de prueba.
Cliente oficial para la API de HuggingChat de Hugging Face
Modelo: Llama-3.2-3B-Instruct
API: Hugging Face oficial
Tipo: Chat conversacional
HuggingChat Client v1.0.0 | Documentación API | Configurar Token