Add FLUX.1-Kontext-Dev external API integration - Add external API connection to black-forest-labs/FLUX.1-Kontext-Dev - Add fallback error handling for API calls - Update README with external model documentation
Browse files
README.md
CHANGED
|
@@ -24,6 +24,7 @@ Un generador de im谩genes con IA con m煤ltiples modelos de alta calidad optimiza
|
|
| 24 |
### Modelos FLUX (Alta Calidad)
|
| 25 |
- **FLUX.1-dev** - Modelo de desarrollo de alta calidad
|
| 26 |
- **FLUX.1-schnell** - Modelo r谩pido de alta calidad
|
|
|
|
| 27 |
|
| 28 |
### Modelos Est谩ndar
|
| 29 |
- **SDXL Base** - Modelo base de Stable Diffusion XL
|
|
@@ -43,13 +44,19 @@ Un generador de im谩genes con IA con m煤ltiples modelos de alta calidad optimiza
|
|
| 43 |
- **Configuraciones autom谩ticas**: Cada modelo usa sus par谩metros 贸ptimos
|
| 44 |
- **Carga bajo demanda**: Los modelos se cargan solo cuando se necesitan
|
| 45 |
- **Configuraciones espec铆ficas**: Cada modelo tiene sus propios par谩metros de guidance y steps
|
|
|
|
| 46 |
|
| 47 |
## 馃敡 Configuraciones por Modelo
|
| 48 |
|
| 49 |
- **Turbo/Lightning**: guidance_scale=0.0, steps=1-4
|
| 50 |
- **FLUX**: guidance_scale=7.5, steps=20
|
|
|
|
| 51 |
- **Realistic Vision**: guidance_scale=7.5, steps=25
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
---
|
| 54 |
|
| 55 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 24 |
### Modelos FLUX (Alta Calidad)
|
| 25 |
- **FLUX.1-dev** - Modelo de desarrollo de alta calidad
|
| 26 |
- **FLUX.1-schnell** - Modelo r谩pido de alta calidad
|
| 27 |
+
- **FLUX.1-Kontext-Dev** - Modelo externo via API (requiere conexi贸n)
|
| 28 |
|
| 29 |
### Modelos Est谩ndar
|
| 30 |
- **SDXL Base** - Modelo base de Stable Diffusion XL
|
|
|
|
| 44 |
- **Configuraciones autom谩ticas**: Cada modelo usa sus par谩metros 贸ptimos
|
| 45 |
- **Carga bajo demanda**: Los modelos se cargan solo cuando se necesitan
|
| 46 |
- **Configuraciones espec铆ficas**: Cada modelo tiene sus propios par谩metros de guidance y steps
|
| 47 |
+
- **API Externa**: FLUX.1-Kontext-Dev usa la API del Space oficial
|
| 48 |
|
| 49 |
## 馃敡 Configuraciones por Modelo
|
| 50 |
|
| 51 |
- **Turbo/Lightning**: guidance_scale=0.0, steps=1-4
|
| 52 |
- **FLUX**: guidance_scale=7.5, steps=20
|
| 53 |
+
- **FLUX.1-Kontext-Dev**: guidance_scale=2.5, steps=28 (via API)
|
| 54 |
- **Realistic Vision**: guidance_scale=7.5, steps=25
|
| 55 |
|
| 56 |
+
## 馃寪 Modelo Externo
|
| 57 |
+
|
| 58 |
+
**FLUX.1-Kontext-Dev** se conecta a la API oficial del Space `black-forest-labs/FLUX.1-Kontext-Dev` para generar im谩genes de alta calidad. Este modelo requiere conexi贸n a internet y puede tener tiempos de respuesta variables.
|
| 59 |
+
|
| 60 |
---
|
| 61 |
|
| 62 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -1,6 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
|
@@ -13,6 +18,7 @@ MODELS = {
|
|
| 13 |
"SDXL Turbo (stabilityai/sdxl-turbo)": "stabilityai/sdxl-turbo",
|
| 14 |
"FLUX.1-dev (black-forest-labs/FLUX.1-dev)": "black-forest-labs/FLUX.1-dev",
|
| 15 |
"FLUX.1-schnell (black-forest-labs/FLUX.1-schnell)": "black-forest-labs/FLUX.1-schnell",
|
|
|
|
| 16 |
"SDXL Lightning (ByteDance/SDXL-Lightning)": "ByteDance/SDXL-Lightning",
|
| 17 |
"SDXL Lightning 4Step (ByteDance/SDXL-Lightning-4Step)": "ByteDance/SDXL-Lightning-4Step",
|
| 18 |
"SD Turbo (stabilityai/sd-turbo)": "stabilityai/sd-turbo",
|
|
@@ -40,6 +46,53 @@ def load_model(model_id):
|
|
| 40 |
pipe = pipe.to(device)
|
| 41 |
current_model_id = model_id
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 44 |
def infer(
|
| 45 |
prompt,
|
|
@@ -53,6 +106,10 @@ def infer(
|
|
| 53 |
model_name,
|
| 54 |
progress=gr.Progress(track_tqdm=True),
|
| 55 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Cargar el modelo seleccionado
|
| 57 |
model_id = MODELS[model_name]
|
| 58 |
load_model(model_id)
|
|
@@ -126,7 +183,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 126 |
choices=list(MODELS.keys()),
|
| 127 |
value=list(MODELS.keys())[0],
|
| 128 |
label="Model",
|
| 129 |
-
info="Select a high-quality model"
|
| 130 |
)
|
| 131 |
|
| 132 |
negative_prompt = gr.Text(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
+
import requests
|
| 5 |
+
import base64
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
+
import json
|
| 9 |
|
| 10 |
# import spaces #[uncomment to use ZeroGPU]
|
| 11 |
from diffusers import DiffusionPipeline
|
|
|
|
| 18 |
"SDXL Turbo (stabilityai/sdxl-turbo)": "stabilityai/sdxl-turbo",
|
| 19 |
"FLUX.1-dev (black-forest-labs/FLUX.1-dev)": "black-forest-labs/FLUX.1-dev",
|
| 20 |
"FLUX.1-schnell (black-forest-labs/FLUX.1-schnell)": "black-forest-labs/FLUX.1-schnell",
|
| 21 |
+
"FLUX.1-Kontext-Dev (API External)": "api_external",
|
| 22 |
"SDXL Lightning (ByteDance/SDXL-Lightning)": "ByteDance/SDXL-Lightning",
|
| 23 |
"SDXL Lightning 4Step (ByteDance/SDXL-Lightning-4Step)": "ByteDance/SDXL-Lightning-4Step",
|
| 24 |
"SD Turbo (stabilityai/sd-turbo)": "stabilityai/sd-turbo",
|
|
|
|
| 46 |
pipe = pipe.to(device)
|
| 47 |
current_model_id = model_id
|
| 48 |
|
| 49 |
+
# Funci贸n para usar la API externa de FLUX.1-Kontext-Dev
|
| 50 |
+
def use_external_api(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 51 |
+
try:
|
| 52 |
+
# URL de la API del Space externo
|
| 53 |
+
api_url = "https://black-forest-labs-flux-1-kontext-dev.hf.space/api/predict/"
|
| 54 |
+
|
| 55 |
+
# Crear una imagen base simple para la API (requiere input_image)
|
| 56 |
+
base_image = Image.new('RGB', (width, height), color='white')
|
| 57 |
+
img_byte_arr = io.BytesIO()
|
| 58 |
+
base_image.save(img_byte_arr, format='PNG')
|
| 59 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 60 |
+
|
| 61 |
+
# Preparar los datos para la API
|
| 62 |
+
files = {
|
| 63 |
+
'data': (None, json.dumps([
|
| 64 |
+
base64.b64encode(img_byte_arr).decode('utf-8'), # input_image
|
| 65 |
+
prompt, # prompt
|
| 66 |
+
seed, # seed
|
| 67 |
+
randomize_seed, # randomize_seed
|
| 68 |
+
guidance_scale, # guidance_scale
|
| 69 |
+
num_inference_steps # steps
|
| 70 |
+
]))
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
# Hacer la petici贸n a la API
|
| 74 |
+
response = requests.post(api_url, files=files)
|
| 75 |
+
|
| 76 |
+
if response.status_code == 200:
|
| 77 |
+
result = response.json()
|
| 78 |
+
# La API devuelve [image_data, seed]
|
| 79 |
+
image_data = result['data'][0]
|
| 80 |
+
new_seed = result['data'][1]
|
| 81 |
+
|
| 82 |
+
# Decodificar la imagen
|
| 83 |
+
image_bytes = base64.b64decode(image_data.split(',')[1])
|
| 84 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 85 |
+
|
| 86 |
+
return image, new_seed
|
| 87 |
+
else:
|
| 88 |
+
raise Exception(f"API Error: {response.status_code}")
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"Error usando API externa: {e}")
|
| 92 |
+
# Fallback: crear una imagen de error
|
| 93 |
+
error_image = Image.new('RGB', (width, height), color='red')
|
| 94 |
+
return error_image, seed
|
| 95 |
+
|
| 96 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 97 |
def infer(
|
| 98 |
prompt,
|
|
|
|
| 106 |
model_name,
|
| 107 |
progress=gr.Progress(track_tqdm=True),
|
| 108 |
):
|
| 109 |
+
# Verificar si es el modelo externo
|
| 110 |
+
if model_name == "FLUX.1-Kontext-Dev (API External)":
|
| 111 |
+
return use_external_api(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps)
|
| 112 |
+
|
| 113 |
# Cargar el modelo seleccionado
|
| 114 |
model_id = MODELS[model_name]
|
| 115 |
load_model(model_id)
|
|
|
|
| 183 |
choices=list(MODELS.keys()),
|
| 184 |
value=list(MODELS.keys())[0],
|
| 185 |
label="Model",
|
| 186 |
+
info="Select a high-quality model (FLUX.1-Kontext-Dev uses external API)"
|
| 187 |
)
|
| 188 |
|
| 189 |
negative_prompt = gr.Text(
|