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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import base64
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
def generate_image(prompt, cfg_scale, width, height, seed, steps, input_image=None):
|
| 10 |
+
"""
|
| 11 |
+
Generate image using NVIDIA's Flux.1-dev API
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
# Get API key from environment
|
| 15 |
+
api_key = os.getenv("NVIDIA_API_KEY")
|
| 16 |
+
if not api_key:
|
| 17 |
+
return None, "Error: NVIDIA_API_KEY environment variable not set"
|
| 18 |
+
|
| 19 |
+
# API endpoint
|
| 20 |
+
invoke_url = "https://ai.api.nvidia.com/v1/genai/black-forest-labs/flux.1-dev"
|
| 21 |
+
|
| 22 |
+
# Headers
|
| 23 |
+
headers = {
|
| 24 |
+
"Authorization": f"Bearer {api_key}",
|
| 25 |
+
"Accept": "application/json",
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# Prepare payload
|
| 29 |
+
payload = {
|
| 30 |
+
"prompt": prompt,
|
| 31 |
+
"cfg_scale": cfg_scale,
|
| 32 |
+
"width": int(width),
|
| 33 |
+
"height": int(height),
|
| 34 |
+
"seed": int(seed) if seed != -1 else 0,
|
| 35 |
+
"steps": int(steps)
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# Handle input image for img2img or control modes
|
| 39 |
+
if input_image is not None:
|
| 40 |
+
# Convert PIL image to base64
|
| 41 |
+
buffered = BytesIO()
|
| 42 |
+
input_image.save(buffered, format="PNG")
|
| 43 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 44 |
+
payload["image"] = f"data:image/png;base64,{img_str}"
|
| 45 |
+
payload["mode"] = "canny" # You can make this configurable
|
| 46 |
+
|
| 47 |
+
# Make API request
|
| 48 |
+
response = requests.post(invoke_url, headers=headers, json=payload, timeout=120)
|
| 49 |
+
response.raise_for_status()
|
| 50 |
+
|
| 51 |
+
response_body = response.json()
|
| 52 |
+
|
| 53 |
+
# Check if response contains image data
|
| 54 |
+
if "image" in response_body:
|
| 55 |
+
# Decode base64 image
|
| 56 |
+
image_data = response_body["image"]
|
| 57 |
+
if image_data.startswith("data:image"):
|
| 58 |
+
# Remove data URL prefix
|
| 59 |
+
image_data = image_data.split(",")[1]
|
| 60 |
+
|
| 61 |
+
# Decode and create PIL Image
|
| 62 |
+
image_bytes = base64.b64decode(image_data)
|
| 63 |
+
image = Image.open(BytesIO(image_bytes))
|
| 64 |
+
|
| 65 |
+
return image, f"β
Image generated successfully!\nSeed used: {response_body.get('seed', 'unknown')}"
|
| 66 |
+
else:
|
| 67 |
+
return None, f"β Error: No image in response\n{json.dumps(response_body, indent=2)}"
|
| 68 |
+
|
| 69 |
+
except requests.exceptions.RequestException as e:
|
| 70 |
+
return None, f"β API Request Error: {str(e)}"
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return None, f"β Error: {str(e)}"
|
| 73 |
+
|
| 74 |
+
def create_gradio_interface():
|
| 75 |
+
"""
|
| 76 |
+
Create and configure the Gradio interface
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
with gr.Blocks(title="NVIDIA Flux.1-dev Image Generator", theme=gr.themes.Soft()) as demo:
|
| 80 |
+
gr.Markdown(
|
| 81 |
+
"""
|
| 82 |
+
# π¨ NVIDIA Flux.1-dev Image Generator
|
| 83 |
+
Generate high-quality images using NVIDIA's Flux.1-dev model via their API.
|
| 84 |
+
|
| 85 |
+
**Requirements:** Set your `NVIDIA_API_KEY` environment variable before running.
|
| 86 |
+
"""
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column(scale=1):
|
| 91 |
+
# Input controls
|
| 92 |
+
prompt = gr.Textbox(
|
| 93 |
+
label="Prompt",
|
| 94 |
+
placeholder="Describe the image you want to generate...",
|
| 95 |
+
lines=3,
|
| 96 |
+
value="a simple coffee shop interior"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
input_image = gr.Image(
|
| 100 |
+
label="Input Image (optional - for img2img/control)",
|
| 101 |
+
type="pil",
|
| 102 |
+
height=300
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
cfg_scale = gr.Slider(
|
| 107 |
+
minimum=1.0,
|
| 108 |
+
maximum=20.0,
|
| 109 |
+
value=3.5,
|
| 110 |
+
step=0.5,
|
| 111 |
+
label="CFG Scale",
|
| 112 |
+
info="Higher values follow prompt more closely"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
steps = gr.Slider(
|
| 116 |
+
minimum=10,
|
| 117 |
+
maximum=100,
|
| 118 |
+
value=50,
|
| 119 |
+
step=5,
|
| 120 |
+
label="Steps",
|
| 121 |
+
info="More steps = higher quality but slower"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
with gr.Row():
|
| 125 |
+
width = gr.Dropdown(
|
| 126 |
+
choices=[512, 768, 1024, 1280],
|
| 127 |
+
value=1024,
|
| 128 |
+
label="Width"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
height = gr.Dropdown(
|
| 132 |
+
choices=[512, 768, 1024, 1280],
|
| 133 |
+
value=1024,
|
| 134 |
+
label="Height"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
seed = gr.Number(
|
| 138 |
+
label="Seed",
|
| 139 |
+
value=-1,
|
| 140 |
+
info="Use -1 for random seed"
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
generate_btn = gr.Button(
|
| 144 |
+
"π¨ Generate Image",
|
| 145 |
+
variant="primary",
|
| 146 |
+
size="lg"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
with gr.Column(scale=1):
|
| 150 |
+
# Output
|
| 151 |
+
output_image = gr.Image(
|
| 152 |
+
label="Generated Image",
|
| 153 |
+
type="pil",
|
| 154 |
+
height=500
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
status_text = gr.Textbox(
|
| 158 |
+
label="Status",
|
| 159 |
+
lines=3,
|
| 160 |
+
interactive=False
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# Example prompts
|
| 164 |
+
gr.Markdown("### π‘ Example Prompts")
|
| 165 |
+
examples = gr.Examples(
|
| 166 |
+
examples=[
|
| 167 |
+
["a cozy coffee shop with warm lighting and vintage furniture"],
|
| 168 |
+
["a futuristic cityscape at sunset with flying cars"],
|
| 169 |
+
["a magical forest with glowing mushrooms and fairy lights"],
|
| 170 |
+
["a minimalist modern kitchen with marble countertops"],
|
| 171 |
+
["a steampunk laboratory with brass machinery and glowing tubes"],
|
| 172 |
+
["a serene mountain lake reflecting snow-capped peaks"],
|
| 173 |
+
],
|
| 174 |
+
inputs=[prompt]
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# Event handlers
|
| 178 |
+
generate_btn.click(
|
| 179 |
+
fn=generate_image,
|
| 180 |
+
inputs=[prompt, cfg_scale, width, height, seed, steps, input_image],
|
| 181 |
+
outputs=[output_image, status_text]
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
# Keyboard shortcut
|
| 185 |
+
prompt.submit(
|
| 186 |
+
fn=generate_image,
|
| 187 |
+
inputs=[prompt, cfg_scale, width, height, seed, steps, input_image],
|
| 188 |
+
outputs=[output_image, status_text]
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
return demo
|
| 192 |
+
|
| 193 |
+
if __name__ == "__main__":
|
| 194 |
+
# Check if API key is set
|
| 195 |
+
if not os.getenv("NVIDIA_API_KEY"):
|
| 196 |
+
print("β οΈ Warning: NVIDIA_API_KEY environment variable not set!")
|
| 197 |
+
print("Please set it before running the app:")
|
| 198 |
+
print("export NVIDIA_API_KEY=your_api_key_here")
|
| 199 |
+
print()
|
| 200 |
+
|
| 201 |
+
# Create and launch the app
|
| 202 |
+
demo = create_gradio_interface()
|
| 203 |
+
demo.launch(
|
| 204 |
+
share=False, # Set to True if you want a public link
|
| 205 |
+
server_name="0.0.0.0", # Allow external connections
|
| 206 |
+
server_port=7860,
|
| 207 |
+
show_error=True
|
| 208 |
+
)
|