Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Obt茅n el token de manera segura desde el entorno
|
| 6 |
+
hf_token = os.getenv("HF_API_TOKEN")
|
| 7 |
+
|
| 8 |
+
# Clase para manejar m煤ltiples modelos
|
| 9 |
+
class ModelHandler:
|
| 10 |
+
def __init__(self, model_names, token):
|
| 11 |
+
"""
|
| 12 |
+
Inicializa el manejador de modelos con los nombres de los modelos y el token de API.
|
| 13 |
+
"""
|
| 14 |
+
self.clients = {
|
| 15 |
+
model_name: InferenceClient(model_name, token=token)
|
| 16 |
+
for model_name in model_names
|
| 17 |
+
}
|
| 18 |
+
self.current_model = model_names[0]
|
| 19 |
+
|
| 20 |
+
def switch_model(self, model_name):
|
| 21 |
+
"""
|
| 22 |
+
Cambia el modelo actual.
|
| 23 |
+
"""
|
| 24 |
+
if model_name in self.clients:
|
| 25 |
+
self.current_model = model_name
|
| 26 |
+
else:
|
| 27 |
+
raise ValueError(f"Modelo {model_name} no est谩 disponible.")
|
| 28 |
+
|
| 29 |
+
def generate_response(self, input_text):
|
| 30 |
+
"""
|
| 31 |
+
Genera una respuesta utilizando el modelo actual.
|
| 32 |
+
"""
|
| 33 |
+
prompt = f"Debes de responder a cualquier pregunta:\nPregunta: {input_text}"
|
| 34 |
+
try:
|
| 35 |
+
messages = [{"role": "user", "content": prompt}]
|
| 36 |
+
client = self.clients[self.current_model]
|
| 37 |
+
response = client.chat_completion(messages=messages, max_tokens=500)
|
| 38 |
+
if hasattr(response, 'choices') and response.choices:
|
| 39 |
+
return response.choices[0].message.content
|
| 40 |
+
else:
|
| 41 |
+
return str(response)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error al realizar la inferencia: {e}"
|
| 44 |
+
|
| 45 |
+
# Lista de modelos disponibles
|
| 46 |
+
model_names = [
|
| 47 |
+
"microsoft/Phi-3-mini-4k-instruct",
|
| 48 |
+
"bigscience/bloomz-560m",
|
| 49 |
+
"tiiuae/falcon-7b-instruct"
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
# Inicializa el manejador de modelos
|
| 53 |
+
model_handler = ModelHandler(model_names, hf_token)
|
| 54 |
+
|
| 55 |
+
# Configura la interfaz en Gradio con selecci贸n de modelos
|
| 56 |
+
with gr.Blocks(title="Multi-Model LLM Chatbot") as demo:
|
| 57 |
+
gr.Markdown(
|
| 58 |
+
"""
|
| 59 |
+
## Chatbot Multi-Modelo LLM
|
| 60 |
+
Este chatbot permite elegir entre m煤ltiples modelos de lenguaje para responder preguntas.
|
| 61 |
+
Selecciona un modelo, escribe tu consulta, y presiona el bot贸n de enviar para obtener una respuesta.
|
| 62 |
+
"""
|
| 63 |
+
)
|
| 64 |
+
with gr.Row():
|
| 65 |
+
model_dropdown = gr.Dropdown(
|
| 66 |
+
choices=model_names,
|
| 67 |
+
value=model_names[0],
|
| 68 |
+
label="Seleccionar Modelo",
|
| 69 |
+
interactive=True
|
| 70 |
+
)
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column():
|
| 73 |
+
input_text = gr.Textbox(
|
| 74 |
+
lines=5,
|
| 75 |
+
placeholder="Escribe tu pregunta aqu铆...",
|
| 76 |
+
label="Pregunta"
|
| 77 |
+
)
|
| 78 |
+
with gr.Column():
|
| 79 |
+
output_text = gr.Textbox(
|
| 80 |
+
lines=5,
|
| 81 |
+
label="Respuesta",
|
| 82 |
+
interactive=False
|
| 83 |
+
)
|
| 84 |
+
submit_button = gr.Button("Enviar")
|
| 85 |
+
|
| 86 |
+
# Define la funci贸n de actualizaci贸n
|
| 87 |
+
def process_input(selected_model, user_input):
|
| 88 |
+
model_handler.switch_model(selected_model)
|
| 89 |
+
return model_handler.generate_response(user_input)
|
| 90 |
+
|
| 91 |
+
# Conecta la funci贸n a los componentes
|
| 92 |
+
submit_button.click(
|
| 93 |
+
fn=process_input,
|
| 94 |
+
inputs=[model_dropdown, input_text],
|
| 95 |
+
outputs=output_text
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Lanza la interfaz
|
| 99 |
+
demo.launch()
|