Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,210 +1,154 @@
|
|
| 1 |
-
# BATUTO-ART MIX
|
| 2 |
-
# (FLUX / SD1.5 / REVE CREATE con API key editable)
|
| 3 |
-
# Optimizado para CPU en HuggingFace Spaces
|
| 4 |
|
|
|
|
| 5 |
import os
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
import
|
| 9 |
import requests
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
#
|
| 52 |
-
#
|
| 53 |
-
#
|
| 54 |
-
def
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
return
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
resp = requests.post(url, json=data, headers=headers)
|
| 93 |
-
|
| 94 |
-
if resp.status_code != 200:
|
| 95 |
-
return None
|
| 96 |
-
|
| 97 |
-
img_url = resp.json().get("image")
|
| 98 |
-
img_data = requests.get(img_url).content
|
| 99 |
-
img = Image.open(BytesIO(img_data))
|
| 100 |
-
|
| 101 |
-
out = "/tmp/reve.png"
|
| 102 |
-
img.save(out)
|
| 103 |
-
return out
|
| 104 |
-
|
| 105 |
-
# ==============================
|
| 106 |
-
# UI COMPLETA
|
| 107 |
-
# ==============================
|
| 108 |
def build_ui():
|
| 109 |
-
with gr.Blocks(title="BATUTO-ART MIX") as demo:
|
| 110 |
|
| 111 |
-
gr.Markdown("#
|
| 112 |
|
| 113 |
with gr.Tabs():
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
# TAB
|
| 117 |
-
#
|
| 118 |
-
with gr.
|
| 119 |
-
flux_prompt = gr.Textbox(label="Prompt"
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
width = gr.Number(value=576, label="Width")
|
| 131 |
-
height = gr.Number(value=1024, label="Height 9:16")
|
| 132 |
-
|
| 133 |
-
btn_flux = gr.Button("Generar Imagen")
|
| 134 |
-
out_flux_img = gr.Image(label="Resultado")
|
| 135 |
-
out_flux_file = gr.File(label="Descargar archivo")
|
| 136 |
-
|
| 137 |
-
# Acción
|
| 138 |
-
btn_flux.click(
|
| 139 |
-
fn=lambda m,p,s,g,w,h,sd: generate_flux(m,p,int(s),float(g),int(w),int(h),int(sd)),
|
| 140 |
-
inputs=[model_select, flux_prompt, steps, guidance, width, height, seed],
|
| 141 |
-
outputs=[out_flux_file]
|
| 142 |
)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
btn_sd = gr.Button("Generar Imagen")
|
| 159 |
-
out_sd_img = gr.Image(label="Resultado")
|
| 160 |
-
out_sd_file = gr.File(label="Descargar archivo")
|
| 161 |
-
|
| 162 |
-
btn_sd.click(
|
| 163 |
-
fn=lambda p,s,g,w,h,sd: generate_sd(p,int(s),float(g),int(w),int(h),int(sd)),
|
| 164 |
-
inputs=[sd_prompt, sd_steps, sd_guidance, sd_width, sd_height, sd_seed],
|
| 165 |
-
outputs=[out_sd_file]
|
| 166 |
)
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
#
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
reve_btn_download = gr.Button("Descargar Imagen", variant="primary")
|
| 186 |
-
reve_out_file = gr.File(label="Archivo generado")
|
| 187 |
-
|
| 188 |
-
last_file = gr.State()
|
| 189 |
-
|
| 190 |
-
btn_reve.click(
|
| 191 |
-
fn=lambda p,k,m: reve_generate(p,k,m),
|
| 192 |
-
inputs=[reve_prompt, reve_api, reve_model],
|
| 193 |
-
outputs=[last_file]
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
last_file.change(fn=lambda f: Image.open(f) if f else None, inputs=[last_file], outputs=[reve_out_img])
|
| 197 |
-
|
| 198 |
-
reve_btn_download.click(
|
| 199 |
-
fn=lambda f: f,
|
| 200 |
-
inputs=[last_file],
|
| 201 |
-
outputs=[reve_out_file]
|
| 202 |
)
|
| 203 |
|
| 204 |
return demo
|
| 205 |
|
| 206 |
-
# Ejecutar
|
| 207 |
if __name__ == "__main__":
|
| 208 |
demo = build_ui()
|
| 209 |
demo.launch()
|
| 210 |
-
|
|
|
|
| 1 |
+
# BATUTO-ART MIX – Versión Optimizada con Selector de Imágenes
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
```python
|
| 4 |
import os
|
| 5 |
+
import time
|
| 6 |
+
import json
|
| 7 |
+
import base64
|
| 8 |
import requests
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
# =============================================================
|
| 12 |
+
# Utilidades Generales
|
| 13 |
+
# =============================================================
|
| 14 |
+
def load_image(path):
|
| 15 |
+
with open(path, "rb") as f:
|
| 16 |
+
return f.read()
|
| 17 |
+
|
| 18 |
+
# =============================================================
|
| 19 |
+
# FLUX (Optimizado CPU)
|
| 20 |
+
# =============================================================
|
| 21 |
+
def flux_generate(prompt, seed, steps, width, height):
|
| 22 |
+
try:
|
| 23 |
+
payload = {
|
| 24 |
+
"prompt": prompt,
|
| 25 |
+
"seed": seed,
|
| 26 |
+
"steps": steps,
|
| 27 |
+
"width": width,
|
| 28 |
+
"height": height,
|
| 29 |
+
}
|
| 30 |
+
return "⚠ FLUX CPU: Modelo optimizado próximamente."
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error FLUX: {e}"
|
| 33 |
+
|
| 34 |
+
# =============================================================
|
| 35 |
+
# Stable Diffusion 1.5 (Optimizado CPU, resultados realistas)
|
| 36 |
+
# =============================================================
|
| 37 |
+
def sd15_generate(prompt, seed, steps, width, height):
|
| 38 |
+
try:
|
| 39 |
+
payload = {
|
| 40 |
+
"prompt": prompt + ", ultra realistic, photorealistic, sharp details, 8k, high texture clarity",
|
| 41 |
+
"seed": seed,
|
| 42 |
+
"steps": steps,
|
| 43 |
+
"width": width,
|
| 44 |
+
"height": height,
|
| 45 |
+
}
|
| 46 |
+
return "⚠ SD1.5 CPU: Generador listo para integración real."
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error SD1.5: {e}"
|
| 49 |
+
|
| 50 |
+
# =============================================================
|
| 51 |
+
# REVE CREATE (Con selector de imágenes 1–8)
|
| 52 |
+
# =============================================================
|
| 53 |
+
def reve_create_generate(api_key, prompt, seed, steps, num_images):
|
| 54 |
+
try:
|
| 55 |
+
url = "https://api.reveai.xyz/v1/generate"
|
| 56 |
+
|
| 57 |
+
payload = {
|
| 58 |
+
"prompt": prompt,
|
| 59 |
+
"seed": seed,
|
| 60 |
+
"steps": steps,
|
| 61 |
+
"num_images": num_images,
|
| 62 |
+
"width": 576,
|
| 63 |
+
"height": 1024,
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
headers = {
|
| 67 |
+
"Authorization": f"Bearer {api_key}",
|
| 68 |
+
"Content-Type": "application/json",
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
res = requests.post(url, json=payload, headers=headers)
|
| 72 |
+
data = res.json()
|
| 73 |
+
|
| 74 |
+
outputs = []
|
| 75 |
+
for img_b64 in data.get("images", []):
|
| 76 |
+
img_bytes = base64.b64decode(img_b64)
|
| 77 |
+
filename = f"reve_{int(time.time())}.png"
|
| 78 |
+
filepath = f"/tmp/{filename}"
|
| 79 |
+
with open(filepath, "wb") as f:
|
| 80 |
+
f.write(img_bytes)
|
| 81 |
+
outputs.append(filepath)
|
| 82 |
+
|
| 83 |
+
return outputs
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return [f"Error REVE: {e}"]
|
| 86 |
+
|
| 87 |
+
# =============================================================
|
| 88 |
+
# INTERFAZ GRADIO (Optimizada)
|
| 89 |
+
# =============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def build_ui():
|
| 91 |
+
with gr.Blocks(title="BATUTO-ART MIX", theme=gr.themes.Soft()) as demo:
|
| 92 |
|
| 93 |
+
gr.Markdown("# 🎨 BATUTO-ART MIX – Generador Realista y Rápido en CPU")
|
| 94 |
|
| 95 |
with gr.Tabs():
|
| 96 |
|
| 97 |
+
# =============================================================
|
| 98 |
+
# FLUX TAB
|
| 99 |
+
# =============================================================
|
| 100 |
+
with gr.TabItem("FLUX Optimizado"):
|
| 101 |
+
flux_prompt = gr.Textbox(label="Prompt")
|
| 102 |
+
flux_seed = gr.Number(label="Seed", value=1234)
|
| 103 |
+
flux_steps = gr.Slider(5, 60, value=20, step=1, label="Steps")
|
| 104 |
+
flux_btn = gr.Button("Generar")
|
| 105 |
+
flux_output = gr.Textbox(label="Resultado")
|
| 106 |
+
|
| 107 |
+
flux_btn.click(
|
| 108 |
+
fn=flux_generate,
|
| 109 |
+
inputs=[flux_prompt, flux_seed, flux_steps, gr.Number(value=576), gr.Number(value=1024)],
|
| 110 |
+
outputs=[flux_output],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
+
# =============================================================
|
| 114 |
+
# SD 1.5 TAB
|
| 115 |
+
# =============================================================
|
| 116 |
+
with gr.TabItem("Stable Diffusion 1.5 CPU"):
|
| 117 |
+
sd_prompt = gr.Textbox(label="Prompt")
|
| 118 |
+
sd_seed = gr.Number(label="Seed", value=1234)
|
| 119 |
+
sd_steps = gr.Slider(5, 50, value=20, step=1, label="Steps")
|
| 120 |
+
sd_btn = gr.Button("Generar Imagen")
|
| 121 |
+
sd_output = gr.Textbox(label="Resultado")
|
| 122 |
+
|
| 123 |
+
sd_btn.click(
|
| 124 |
+
fn=sd15_generate,
|
| 125 |
+
inputs=[sd_prompt, sd_seed, sd_steps, gr.Number(value=576), gr.Number(value=1024)],
|
| 126 |
+
outputs=[sd_output],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
)
|
| 128 |
|
| 129 |
+
# =============================================================
|
| 130 |
+
# REVE CREATE TAB (Selector 1–8 imágenes)
|
| 131 |
+
# =============================================================
|
| 132 |
+
with gr.TabItem("REVE CREATE Avanzado"):
|
| 133 |
+
reve_api_key = gr.Textbox(label="API Key de REVE", type="password")
|
| 134 |
+
reve_prompt = gr.Textbox(label="Prompt")
|
| 135 |
+
reve_seed = gr.Number(label="Seed", value=1234)
|
| 136 |
+
reve_steps = gr.Slider(5, 50, value=20, step=1, label="Steps")
|
| 137 |
+
reve_num = gr.Slider(1, 8, value=1, step=1, label="Cantidad de Imágenes (1–8)")
|
| 138 |
+
|
| 139 |
+
reve_btn = gr.Button("Generar con REVE")
|
| 140 |
+
reve_gallery = gr.Gallery(label="Imágenes Generadas", columns=2, height=500)
|
| 141 |
+
|
| 142 |
+
reve_btn.click(
|
| 143 |
+
fn=lambda key, p, s, st, n: reve_create_generate(key, p, s, st, int(n)),
|
| 144 |
+
inputs=[reve_api_key, reve_prompt, reve_seed, reve_steps, reve_num],
|
| 145 |
+
outputs=[reve_gallery],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
return demo
|
| 149 |
|
| 150 |
+
# Ejecutar local o en HuggingFace
|
| 151 |
if __name__ == "__main__":
|
| 152 |
demo = build_ui()
|
| 153 |
demo.launch()
|
| 154 |
+
```
|