Actualizar app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import base64
|
|
| 6 |
import io
|
| 7 |
from PIL import Image
|
| 8 |
|
|
|
|
| 9 |
def process_image(image):
|
| 10 |
if image is None:
|
| 11 |
return None
|
|
@@ -13,8 +14,29 @@ def process_image(image):
|
|
| 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"
|
| 19 |
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
|
| 20 |
if not API_KEY:
|
|
@@ -38,23 +60,25 @@ def analizar_imagen_y_generar_prompt(image_base64):
|
|
| 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([
|
| 48 |
|
| 49 |
if auto_mode and image_b64:
|
| 50 |
respuesta = analizar_imagen_y_generar_prompt(image_b64)
|
| 51 |
else:
|
| 52 |
-
respuesta =
|
| 53 |
|
| 54 |
-
chat_history.append([
|
| 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()
|
|
|
|
| 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 |
image.save(buffered, format="PNG")
|
| 15 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 16 |
|
| 17 |
+
# Generar respuesta generativa con Sambanova
|
| 18 |
+
def generar_respuesta_api(mensaje_usuario):
|
| 19 |
+
API_KEY = os.getenv("SAMBANOVA_API_KEY")
|
| 20 |
+
API_URL = "https://api.sambanova.ai/v1/chat/completions"
|
| 21 |
+
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
|
| 22 |
+
|
| 23 |
+
messages = [
|
| 24 |
+
{"role": "system", "content": "Eres un asistente útil y detallado."},
|
| 25 |
+
{"role": "user", "content": mensaje_usuario}
|
| 26 |
+
]
|
| 27 |
+
json_data = {"model": "Llama-4-Maverick-17B-128E-Instruct", "messages": messages, "stream": False}
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
response = requests.post(API_URL, headers=headers, json=json_data)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
content = response.json()["choices"][0]["message"]["content"]
|
| 33 |
+
return content
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error en la generación de respuesta: {str(e)}"
|
| 36 |
+
|
| 37 |
+
# Analizar imagen para prompt
|
| 38 |
def analizar_imagen_y_generar_prompt(image_base64):
|
| 39 |
+
API_KEY = os.getenv("SAMBANOVA_API_KEY")
|
| 40 |
API_URL = "https://api.sambanova.ai/v1/chat/completions"
|
| 41 |
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
|
| 42 |
if not API_KEY:
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error al analizar imagen: {str(e)}"
|
| 62 |
|
| 63 |
+
# Función principal del chat dinámico
|
| 64 |
def chat_mode(user_message, image_input, chat_history, auto_mode):
|
| 65 |
if chat_history is None:
|
| 66 |
chat_history = []
|
| 67 |
image_b64 = process_image(image_input) if image_input else None
|
| 68 |
|
| 69 |
if user_message.strip() != "":
|
| 70 |
+
chat_history.append(["Usuario", user_message])
|
| 71 |
|
| 72 |
if auto_mode and image_b64:
|
| 73 |
respuesta = analizar_imagen_y_generar_prompt(image_b64)
|
| 74 |
else:
|
| 75 |
+
respuesta = generar_respuesta_api(user_message)
|
| 76 |
|
| 77 |
+
chat_history.append(["IA", respuesta])
|
| 78 |
|
| 79 |
return chat_history, chat_history
|
| 80 |
|
| 81 |
+
# Interfaz Gradio chat dinámica
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
gr.Markdown("# ⚡ BATUTO / Prompt Studio — Chat IA + Imagen")
|
| 84 |
chat_box = gr.Chatbot()
|