|
import gradio as gr |
|
import os |
|
import logging |
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
logging.basicConfig( |
|
level=logging.INFO, |
|
format="%(asctime)s - %(levelname)s - %(message)s" |
|
) |
|
|
|
|
|
hf_token = os.getenv("HF_TOKEN2") |
|
|
|
if not hf_token: |
|
logging.error("El token de Hugging Face no est谩 configurado. Agrega 'HF_TOKEN' como variable de entorno.") |
|
raise ValueError("El token de Hugging Face no est谩 configurado. Agrega 'HF_TOKEN' como variable de entorno.") |
|
|
|
logging.info("Token de Hugging Face encontrado correctamente.") |
|
|
|
|
|
try: |
|
client = InferenceClient("BSC-LT/ALIA-40b", token=hf_token) |
|
logging.info("Cliente de Hugging Face inicializado correctamente.") |
|
except Exception as e: |
|
logging.error(f"Error al inicializar el cliente de Hugging Face: {e}") |
|
raise |
|
|
|
def respond( |
|
message, |
|
history: list[tuple[str, str]], |
|
system_message, |
|
max_tokens, |
|
temperature, |
|
top_p, |
|
): |
|
logging.info("Generando respuesta para el mensaje del usuario.") |
|
|
|
prompt = f"{system_message}\n" |
|
|
|
for user_input, bot_response in history: |
|
prompt += f"User: {user_input}\nAssistant: {bot_response}\n" |
|
|
|
prompt += f"User: {message}\nAssistant:" |
|
|
|
response = "" |
|
|
|
try: |
|
for message in client.text_generation( |
|
prompt=prompt, |
|
max_new_tokens=max_tokens, |
|
temperature=temperature, |
|
top_p=top_p, |
|
stream=True |
|
): |
|
response += message |
|
yield response |
|
|
|
logging.info("Respuesta generada correctamente.") |
|
|
|
except Exception as e: |
|
logging.error(f"Error durante la generaci贸n de texto: {e}", exc_info=True) |
|
yield f"Error: {str(e)}. Por favor, int茅ntalo nuevamente." |
|
|
|
""" |
|
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface |
|
""" |
|
demo = gr.Interface( |
|
fn=respond, |
|
inputs=[ |
|
gr.Textbox(value="Eres un chatbot muy amigable.", label="Mensaje del sistema"), |
|
gr.Chatbot(type="messages"), |
|
gr.Slider(minimum=1, maximum=1024, value=512, step=1, label="Max new tokens"), |
|
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"), |
|
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"), |
|
gr.Textbox(value="", label="Mensaje del usuario"), |
|
], |
|
outputs="text", |
|
title="ALIA-40b Generador de Texto", |
|
description="Introduce un texto y deja que el modelo ALIA-40b complete la respuesta.", |
|
) |
|
|
|
if __name__ == "__main__": |
|
logging.info("Lanzando la aplicaci贸n con Gradio...") |
|
demo.launch(share=True) |
|
|
|
|