ivanoctaviogaitansantos commited on
Commit
48a6e27
·
verified ·
1 Parent(s): 9c7c393

Actualizar app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -6,7 +6,6 @@ import base64
6
  import io
7
  from PIL import Image
8
 
9
- # Procesar imagen a base64
10
  def process_image(image):
11
  if image is None:
12
  return None
@@ -14,7 +13,6 @@ def process_image(image):
14
  image.save(buffered, format="PNG")
15
  return base64.b64encode(buffered.getvalue()).decode("utf-8")
16
 
17
- # Función para llamar a la API Sambanova para análisis de imagen + texto
18
  def analizar_imagen_y_generar_prompt(image_base64):
19
  API_KEY = os.getenv("SAMBANOVA_API_KEY")
20
  API_URL = "https://api.sambanova.ai/v1/chat/completions"
@@ -40,19 +38,23 @@ def analizar_imagen_y_generar_prompt(image_base64):
40
  except Exception as e:
41
  return f"Error al analizar imagen: {str(e)}"
42
 
43
- # Función principal del chat dinámico
44
  def chat_mode(user_message, image_input, chat_history, auto_mode):
45
- chat_history = chat_history or []
 
46
  image_b64 = process_image(image_input) if image_input else None
47
- chat_history.append(("Usuario", user_message))
 
 
 
48
  if auto_mode and image_b64:
49
  respuesta = analizar_imagen_y_generar_prompt(image_b64)
50
  else:
51
  respuesta = "Mensaje recibido: " + user_message
52
- chat_history.append(("IA", respuesta))
53
- return chat_history
54
 
55
- # Interfaz Gradio chat dinámica
 
 
 
56
  with gr.Blocks() as demo:
57
  gr.Markdown("# ⚡ BATUTO / Prompt Studio — Chat IA + Imagen")
58
  chat_box = gr.Chatbot()
@@ -60,7 +62,7 @@ with gr.Blocks() as demo:
60
  upload_img = gr.Image(label="Sube una imagen (opcional)", type="pil")
61
  auto_mode = gr.Checkbox(label="Modo automático: analizar imagen para prompt", value=False)
62
  send_button = gr.Button("Enviar")
63
- chat_state = gr.State([])
64
 
65
  send_button.click(
66
  chat_mode,
 
6
  import io
7
  from PIL import Image
8
 
 
9
  def process_image(image):
10
  if image is None:
11
  return None
 
13
  image.save(buffered, format="PNG")
14
  return base64.b64encode(buffered.getvalue()).decode("utf-8")
15
 
 
16
  def analizar_imagen_y_generar_prompt(image_base64):
17
  API_KEY = os.getenv("SAMBANOVA_API_KEY")
18
  API_URL = "https://api.sambanova.ai/v1/chat/completions"
 
38
  except Exception as e:
39
  return f"Error al analizar imagen: {str(e)}"
40
 
 
41
  def chat_mode(user_message, image_input, chat_history, auto_mode):
42
+ if chat_history is None:
43
+ chat_history = []
44
  image_b64 = process_image(image_input) if image_input else None
45
+
46
+ if user_message.strip() != "":
47
+ chat_history.append([ "Usuario", user_message ]) # Lista de dos elementos
48
+
49
  if auto_mode and image_b64:
50
  respuesta = analizar_imagen_y_generar_prompt(image_b64)
51
  else:
52
  respuesta = "Mensaje recibido: " + user_message
 
 
53
 
54
+ chat_history.append([ "IA", respuesta ]) # Lista de dos elementos
55
+
56
+ return chat_history, chat_history
57
+
58
  with gr.Blocks() as demo:
59
  gr.Markdown("# ⚡ BATUTO / Prompt Studio — Chat IA + Imagen")
60
  chat_box = gr.Chatbot()
 
62
  upload_img = gr.Image(label="Sube una imagen (opcional)", type="pil")
63
  auto_mode = gr.Checkbox(label="Modo automático: analizar imagen para prompt", value=False)
64
  send_button = gr.Button("Enviar")
65
+ chat_state = gr.State(value=[])
66
 
67
  send_button.click(
68
  chat_mode,