Katon1 commited on
Commit
9926f71
verified
1 Parent(s): 6538330

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")
8
+
9
+ # Clase para manejar m煤ltiples modelos
10
+ class ModelHandler:
11
+ def __init__(self, model_names, token):
12
+ self.clients = {model_key: InferenceClient(model_name, token=token) for model_key, model_name in model_names.items()}
13
+ self.current_model = list(model_names.keys())[0]
14
+
15
+ def switch_model(self, model_key):
16
+ if model_key in self.clients:
17
+ self.current_model = model_key
18
+ else:
19
+ raise ValueError(f"Modelo {model_key} 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}]
25
+ client = self.clients[self.current_model]
26
+ response = client.chat_completion(messages=messages, max_tokens=500)
27
+ if hasattr(response, 'choices') and response.choices:
28
+ return response.choices[0].message.content
29
+ else:
30
+ return str(response)
31
+ except Exception as e:
32
+ return f"Error al realizar la inferencia: {e}"
33
+
34
+ # Lista de modelos disponibles (con nombres amigables para la interfaz)
35
+ model_names = {
36
+ "CHATBOT": "microsoft/Phi-3-mini-4k-instruct"
37
+ }
38
+
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 el modelo de "stabilityai/stable-diffusion-2" y muestra un progreso.
46
+ """
47
+ try:
48
+ client = InferenceClient("stabilityai/stable-diffusion-2", 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:
62
+ gr.Markdown(
63
+ """
64
+ ## Chatbot Multi-Modelo LLM con Generaci贸n de Im谩genes
65
+ Este chatbot permite elegir entre m煤ltiples modelos de lenguaje para responder preguntas o generar im谩genes
66
+ a partir de descripciones.
67
+ """
68
+ )
69
+ with gr.Row():
70
+ model_dropdown = gr.Dropdown(
71
+ choices=list(model_names.keys()) + ["Generaci贸n de Im谩genes"],
72
+ value="CHATBOT",
73
+ label="Seleccionar Acci贸n/Modelo",
74
+ interactive=True
75
+ )
76
+ with gr.Row():
77
+ with gr.Column():
78
+ input_text = gr.Textbox(
79
+ lines=5,
80
+ placeholder="Escribe tu consulta o descripci贸n para la imagen...",
81
+ label="Entrada"
82
+ )
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
+ try:
98
+ if selected_action == "Generaci贸n de Im谩genes":
99
+ # Manejamos el generador de progreso
100
+ progress_generator = generate_image_with_progress(user_input)
101
+ last_status = None
102
+ last_image = None
103
+ for status, image in progress_generator:
104
+ last_status = status
105
+ last_image = image
106
+ return last_status, last_image
107
+ else:
108
+ model_handler.switch_model(selected_action)
109
+ response = model_handler.generate_response(user_input)
110
+ return response, None
111
+ except Exception as e:
112
+ return f"Error: {e}", None
113
+
114
+ # Conecta la funci贸n a los componentes
115
+ submit_button.click(
116
+ fn=process_input,
117
+ inputs=[model_dropdown, input_text],
118
+ outputs=[output_display, output_image]
119
+ )
120
+
121
+ # Lanza la interfaz
122
+ demo.launch()