Spaces:
Sleeping
Sleeping
cambio codigo
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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")
|
|
@@ -8,28 +9,16 @@ hf_token = os.getenv("HF_API_TOKEN")
|
|
| 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}]
|
|
@@ -50,17 +39,23 @@ model_names = [
|
|
| 50 |
# Inicializa el manejador de modelos
|
| 51 |
model_handler = ModelHandler(model_names, hf_token)
|
| 52 |
|
| 53 |
-
# Define la funci贸n para generaci贸n de im谩genes
|
| 54 |
-
def
|
| 55 |
"""
|
| 56 |
-
Genera una imagen utilizando un modelo de generaci贸n de im谩genes.
|
| 57 |
"""
|
| 58 |
try:
|
| 59 |
client = InferenceClient("CompVis/stable-diffusion-v1-4", token=hf_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
image = client.text_to_image(prompt, width=512, height=512)
|
| 61 |
-
|
| 62 |
except Exception as e:
|
| 63 |
-
|
| 64 |
|
| 65 |
# Configura la interfaz en Gradio con selecci贸n de modelos y generaci贸n de im谩genes
|
| 66 |
with gr.Blocks(title="Multi-Model LLM Chatbot with Image Generation") as demo:
|
|
@@ -88,31 +83,29 @@ with gr.Blocks(title="Multi-Model LLM Chatbot with Image Generation") as demo:
|
|
| 88 |
with gr.Column():
|
| 89 |
output_display = gr.Textbox(
|
| 90 |
lines=5,
|
| 91 |
-
label="
|
| 92 |
-
interactive=False
|
| 93 |
-
visible=False
|
| 94 |
)
|
| 95 |
output_image = gr.Image(
|
| 96 |
label="Imagen Generada",
|
| 97 |
-
interactive=False
|
| 98 |
-
visible=False
|
| 99 |
)
|
| 100 |
submit_button = gr.Button("Enviar")
|
| 101 |
|
| 102 |
# Define la funci贸n de actualizaci贸n
|
| 103 |
def process_input(selected_action, user_input):
|
| 104 |
if selected_action == "Generaci贸n de Im谩genes":
|
| 105 |
-
return
|
| 106 |
else:
|
| 107 |
model_handler.switch_model(selected_action)
|
| 108 |
response = model_handler.generate_response(user_input)
|
| 109 |
-
return response, None
|
| 110 |
|
| 111 |
# Conecta la funci贸n a los componentes
|
| 112 |
submit_button.click(
|
| 113 |
fn=process_input,
|
| 114 |
inputs=[model_dropdown, input_text],
|
| 115 |
-
outputs=[output_display, output_image
|
| 116 |
)
|
| 117 |
|
| 118 |
# Lanza la interfaz
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
# Obt茅n el token de manera segura desde el entorno
|
| 7 |
hf_token = os.getenv("HF_API_TOKEN")
|
|
|
|
| 9 |
# Clase para manejar m煤ltiples modelos
|
| 10 |
class ModelHandler:
|
| 11 |
def __init__(self, model_names, token):
|
| 12 |
+
self.clients = {model_name: InferenceClient(model_name, token=token) for model_name in model_names}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
self.current_model = model_names[0]
|
| 14 |
|
| 15 |
def switch_model(self, model_name):
|
|
|
|
|
|
|
|
|
|
| 16 |
if model_name in self.clients:
|
| 17 |
self.current_model = model_name
|
| 18 |
else:
|
| 19 |
raise ValueError(f"Modelo {model_name} no est谩 disponible.")
|
| 20 |
|
| 21 |
def generate_response(self, input_text):
|
|
|
|
|
|
|
|
|
|
| 22 |
prompt = f"Debes de responder a cualquier pregunta:\nPregunta: {input_text}"
|
| 23 |
try:
|
| 24 |
messages = [{"role": "user", "content": prompt}]
|
|
|
|
| 39 |
# Inicializa el manejador de modelos
|
| 40 |
model_handler = ModelHandler(model_names, hf_token)
|
| 41 |
|
| 42 |
+
# Define la funci贸n para generaci贸n de im谩genes con progreso
|
| 43 |
+
def generate_image_with_progress(prompt):
|
| 44 |
"""
|
| 45 |
+
Genera una imagen utilizando un modelo de generaci贸n de im谩genes y muestra un progreso.
|
| 46 |
"""
|
| 47 |
try:
|
| 48 |
client = InferenceClient("CompVis/stable-diffusion-v1-4", token=hf_token)
|
| 49 |
+
|
| 50 |
+
# Simular progreso
|
| 51 |
+
for progress in range(0, 101, 20):
|
| 52 |
+
time.sleep(0.5)
|
| 53 |
+
yield f"Generando imagen... {progress}% completado", None
|
| 54 |
+
|
| 55 |
image = client.text_to_image(prompt, width=512, height=512)
|
| 56 |
+
yield "Imagen generada con 茅xito", image
|
| 57 |
except Exception as e:
|
| 58 |
+
yield f"Error al generar la imagen: {e}", None
|
| 59 |
|
| 60 |
# Configura la interfaz en Gradio con selecci贸n de modelos y generaci贸n de im谩genes
|
| 61 |
with gr.Blocks(title="Multi-Model LLM Chatbot with Image Generation") as demo:
|
|
|
|
| 83 |
with gr.Column():
|
| 84 |
output_display = gr.Textbox(
|
| 85 |
lines=5,
|
| 86 |
+
label="Estado",
|
| 87 |
+
interactive=False
|
|
|
|
| 88 |
)
|
| 89 |
output_image = gr.Image(
|
| 90 |
label="Imagen Generada",
|
| 91 |
+
interactive=False
|
|
|
|
| 92 |
)
|
| 93 |
submit_button = gr.Button("Enviar")
|
| 94 |
|
| 95 |
# Define la funci贸n de actualizaci贸n
|
| 96 |
def process_input(selected_action, user_input):
|
| 97 |
if selected_action == "Generaci贸n de Im谩genes":
|
| 98 |
+
return generate_image_with_progress(user_input)
|
| 99 |
else:
|
| 100 |
model_handler.switch_model(selected_action)
|
| 101 |
response = model_handler.generate_response(user_input)
|
| 102 |
+
return response, None
|
| 103 |
|
| 104 |
# Conecta la funci贸n a los componentes
|
| 105 |
submit_button.click(
|
| 106 |
fn=process_input,
|
| 107 |
inputs=[model_dropdown, input_text],
|
| 108 |
+
outputs=[output_display, output_image]
|
| 109 |
)
|
| 110 |
|
| 111 |
# Lanza la interfaz
|