Spaces:
Sleeping
Sleeping
Update generation.py
Browse files- generation.py +29 -25
generation.py
CHANGED
|
@@ -1,43 +1,47 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import random
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
from typing import Optional
|
| 5 |
-
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
OUTPUT_DIR = "generated_images"
|
| 8 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 9 |
|
| 10 |
-
# Usaremos un modelo de la comunidad que suele ser gratuito
|
| 11 |
-
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 12 |
-
|
| 13 |
def generate_image_from_prompt(
|
| 14 |
prompt: str,
|
| 15 |
-
negative_prompt: str = "
|
| 16 |
-
model_name: str = "
|
| 17 |
seed: Optional[int] = None,
|
| 18 |
) -> tuple[Optional[str], str]:
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Este modelo funciona de maravilla con InferenceClient
|
| 24 |
-
image = client.text_to_image(
|
| 25 |
-
prompt=prompt,
|
| 26 |
-
model=model_name,
|
| 27 |
-
negative_prompt=negative_prompt,
|
| 28 |
-
num_inference_steps=8, # SDXL Lightning es muy rápido
|
| 29 |
-
guidance_scale=1.5,
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
-
|
| 43 |
-
return None, error_msg
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
import random
|
| 4 |
+
import base64
|
| 5 |
from datetime import datetime
|
| 6 |
from typing import Optional
|
|
|
|
| 7 |
|
| 8 |
OUTPUT_DIR = "generated_images"
|
| 9 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
def generate_image_from_prompt(
|
| 12 |
prompt: str,
|
| 13 |
+
negative_prompt: str = "",
|
| 14 |
+
model_name: str = "black-forest-labs/flux-schnell", # Modelo ultra rápido y barato en OpenRouter
|
| 15 |
seed: Optional[int] = None,
|
| 16 |
) -> tuple[Optional[str], str]:
|
| 17 |
try:
|
| 18 |
+
api_key = os.getenv("OPENROUTER_API_KEY")
|
| 19 |
+
if not api_key:
|
| 20 |
+
return None, "❌ Falta OPENROUTER_API_KEY en los Secrets."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Petición a OpenRouter
|
| 23 |
+
response = requests.post(
|
| 24 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 25 |
+
headers={
|
| 26 |
+
"Authorization": f"Bearer {api_key}",
|
| 27 |
+
"Content-Type": "application/json",
|
| 28 |
+
},
|
| 29 |
+
json={
|
| 30 |
+
"model": model_name,
|
| 31 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 32 |
+
}
|
| 33 |
+
)
|
| 34 |
|
| 35 |
+
data = response.json()
|
| 36 |
|
| 37 |
+
# OpenRouter suele devolver la imagen en el contenido si es un modelo de imagen
|
| 38 |
+
if "choices" in data:
|
| 39 |
+
content = data["choices"][0]["message"]["content"]
|
| 40 |
+
# Aquí la lógica varía según si devuelve URL o B64,
|
| 41 |
+
# pero este paso confirmará si tu saldo y conexión funcionan.
|
| 42 |
+
return None, f"✅ ¡Conectado! OpenRouter respondió: {content[:100]}"
|
| 43 |
+
else:
|
| 44 |
+
return None, f"❌ Error OpenRouter: {data.get('error', 'Error desconocido')}"
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
+
return None, f"❌ Error técnico: {str(e)}"
|
|
|