Miguel Diaz commited on
Commit 路
58acd19
1
Parent(s): 621656c
Generar paso 1
Browse files
modules/chat_functions/generar_imagen.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import openai
|
| 4 |
+
from .. import log_module
|
| 5 |
+
|
| 6 |
+
# Verifica si la API est谩 activa
|
| 7 |
+
activo = True
|
| 8 |
+
try:
|
| 9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
except:
|
| 11 |
+
activo = False
|
| 12 |
+
|
| 13 |
+
user_text = "Generar imagen"
|
| 14 |
+
|
| 15 |
+
structure_info = {
|
| 16 |
+
"imagen_generada": {
|
| 17 |
+
"url": "URL de la imagen generada"
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
info = {
|
| 22 |
+
"type": "function",
|
| 23 |
+
"function": {
|
| 24 |
+
"name": "generate_image",
|
| 25 |
+
"description": "Genera una imagen basada en un prompt utilizando la API de OpenAI. Devuelve la URL de la imagen generada.",
|
| 26 |
+
"parameters": {
|
| 27 |
+
"type": "object",
|
| 28 |
+
"properties": {
|
| 29 |
+
"prompt": {
|
| 30 |
+
"type": "string",
|
| 31 |
+
"description": "El texto que describe la imagen a generar. Ejemplo: Un gato montado en un unicornio bajo la lluvia.",
|
| 32 |
+
},
|
| 33 |
+
"info": {
|
| 34 |
+
"type": "string",
|
| 35 |
+
"description": "El usuario no ver谩 esto. Explica por qu茅 se llam贸 a esta funci贸n.",
|
| 36 |
+
}
|
| 37 |
+
},
|
| 38 |
+
"required": ["prompt"],
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
def ejecutar(params, gid):
|
| 44 |
+
prompt = params["prompt"]
|
| 45 |
+
try:
|
| 46 |
+
response = openai.Image.create(
|
| 47 |
+
prompt=prompt,
|
| 48 |
+
n=1,
|
| 49 |
+
size="512x512"
|
| 50 |
+
)
|
| 51 |
+
image_url = response['data'][0]['url']
|
| 52 |
+
retorno = {"imagen_generada": {"url": image_url}}
|
| 53 |
+
log_module.logger(gid).info(f"Generar imagen {params}")
|
| 54 |
+
return json.dumps(retorno)
|
| 55 |
+
except Exception as e:
|
| 56 |
+
log_module.logger(gid).error(f"Generar imagen fall贸: {repr(e)}")
|
| 57 |
+
return json.dumps({"status": "api failed"})
|