Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,123 +60,83 @@ def upload_image_to_hosting(image):
|
|
| 60 |
|
| 61 |
def process_images(prompt, image1, image2=None):
|
| 62 |
"""
|
| 63 |
-
Process
|
| 64 |
"""
|
| 65 |
if not os.getenv('REPLICATE_API_TOKEN'):
|
| 66 |
return None, "Please set REPLICATE_API_TOKEN"
|
| 67 |
|
| 68 |
try:
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
# Run
|
| 87 |
output = replicate.run(
|
| 88 |
"google/nano-banana",
|
| 89 |
-
input=
|
| 90 |
-
"prompt": prompt,
|
| 91 |
-
"image_input": image_urls
|
| 92 |
-
}
|
| 93 |
)
|
| 94 |
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# Use a text-to-image model when no images are provided
|
| 106 |
-
# Using SDXL-Lightning for fast generation
|
| 107 |
-
output = replicate.run(
|
| 108 |
-
"bytedance/sdxl-lightning-4step:5599ed30703defd1d160a25a63321b4dec97101d98b4674bcc56e41f62f35637",
|
| 109 |
-
input={
|
| 110 |
-
"prompt": prompt,
|
| 111 |
-
"negative_prompt": "worst quality, low quality, blurry, nsfw",
|
| 112 |
-
"width": 1024,
|
| 113 |
-
"height": 1024,
|
| 114 |
-
"num_inference_steps": 4,
|
| 115 |
-
"guidance_scale": 0,
|
| 116 |
-
"scheduler": "K_EULER"
|
| 117 |
-
}
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
return process_output(output, "✨ Generated from text successfully!")
|
| 121 |
|
| 122 |
-
except Exception as e:
|
| 123 |
-
# Fallback to another model if the first one fails
|
| 124 |
try:
|
| 125 |
-
output
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
def process_output(output, success_message="✨ Generated successfully!"):
|
| 142 |
-
"""
|
| 143 |
-
Process the output from Replicate API
|
| 144 |
-
"""
|
| 145 |
-
if output is None:
|
| 146 |
-
return None, "No output received"
|
| 147 |
-
|
| 148 |
-
# Get the generated image
|
| 149 |
-
try:
|
| 150 |
-
if hasattr(output, 'read'):
|
| 151 |
-
img_data = output.read()
|
| 152 |
-
img = Image.open(BytesIO(img_data))
|
| 153 |
-
return img, success_message
|
| 154 |
-
except:
|
| 155 |
-
pass
|
| 156 |
-
|
| 157 |
-
try:
|
| 158 |
-
if hasattr(output, 'url'):
|
| 159 |
-
output_url = output.url()
|
| 160 |
response = requests.get(output_url, timeout=30)
|
| 161 |
if response.status_code == 200:
|
| 162 |
img = Image.open(BytesIO(response.content))
|
| 163 |
-
return img,
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
if output_url:
|
| 174 |
-
response = requests.get(output_url, timeout=30)
|
| 175 |
-
if response.status_code == 200:
|
| 176 |
-
img = Image.open(BytesIO(response.content))
|
| 177 |
-
return img, success_message
|
| 178 |
-
|
| 179 |
-
return None, "Could not process output"
|
| 180 |
|
| 181 |
# Enhanced CSS with modern, minimal design
|
| 182 |
css = """
|
|
@@ -321,7 +281,7 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
|
| 321 |
<h1 class="logo-text">🍌 Free Nano Banana</h1>
|
| 322 |
<p class="subtitle">AI-Powered Image Generation & Style Transfer</p>
|
| 323 |
<div class="mode-indicator">
|
| 324 |
-
💡 Works
|
| 325 |
</div>
|
| 326 |
<div style="display: flex; justify-content: center; align-items: center; gap: 10px; margin-top: 20px;">
|
| 327 |
<a href="https://huggingface.co/spaces/ginigen/Nano-Banana-PRO" target="_blank">
|
|
@@ -346,9 +306,10 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
|
| 346 |
# Info box
|
| 347 |
gr.HTML("""
|
| 348 |
<div class="info-box">
|
| 349 |
-
<strong>How to use:</strong><br>
|
| 350 |
-
• <b>Text
|
| 351 |
-
• <b>Style Transfer:</b>
|
|
|
|
| 352 |
</div>
|
| 353 |
""")
|
| 354 |
|
|
@@ -357,9 +318,9 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
|
| 357 |
with gr.Column(scale=1):
|
| 358 |
prompt = gr.Textbox(
|
| 359 |
label="Prompt / Style Description",
|
| 360 |
-
placeholder="Describe what you want to generate
|
| 361 |
lines=3,
|
| 362 |
-
value="A beautiful
|
| 363 |
elem_classes="prompt-input"
|
| 364 |
)
|
| 365 |
|
|
@@ -397,7 +358,7 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
|
| 397 |
interactive=False,
|
| 398 |
lines=1,
|
| 399 |
elem_classes="status-text",
|
| 400 |
-
value="Ready to generate..."
|
| 401 |
)
|
| 402 |
|
| 403 |
# Event handler
|
|
@@ -407,16 +368,17 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
|
| 407 |
outputs=[output_image, status]
|
| 408 |
)
|
| 409 |
|
| 410 |
-
# Examples
|
| 411 |
gr.Examples(
|
| 412 |
examples=[
|
| 413 |
-
["A
|
| 414 |
-
["
|
| 415 |
-
["
|
| 416 |
-
["
|
|
|
|
| 417 |
],
|
| 418 |
inputs=[prompt, image1, image2],
|
| 419 |
-
label="Text
|
| 420 |
)
|
| 421 |
|
| 422 |
# Launch
|
|
|
|
| 60 |
|
| 61 |
def process_images(prompt, image1, image2=None):
|
| 62 |
"""
|
| 63 |
+
Process with Nano Banana - works with or without images
|
| 64 |
"""
|
| 65 |
if not os.getenv('REPLICATE_API_TOKEN'):
|
| 66 |
return None, "Please set REPLICATE_API_TOKEN"
|
| 67 |
|
| 68 |
try:
|
| 69 |
+
# Prepare input for Nano Banana model
|
| 70 |
+
model_input = {
|
| 71 |
+
"prompt": prompt
|
| 72 |
+
}
|
| 73 |
|
| 74 |
+
# Only add image_input if images are provided
|
| 75 |
+
if image1 or image2:
|
| 76 |
+
image_urls = []
|
| 77 |
+
|
| 78 |
+
if image1:
|
| 79 |
+
url1 = upload_image_to_hosting(image1)
|
| 80 |
+
image_urls.append(url1)
|
| 81 |
+
|
| 82 |
+
if image2:
|
| 83 |
+
url2 = upload_image_to_hosting(image2)
|
| 84 |
+
image_urls.append(url2)
|
| 85 |
+
|
| 86 |
+
model_input["image_input"] = image_urls
|
| 87 |
+
status_msg = "✨ Generated with style transfer!"
|
| 88 |
+
else:
|
| 89 |
+
# No images - text-only generation with Nano Banana
|
| 90 |
+
status_msg = "✨ Generated from text!"
|
| 91 |
|
| 92 |
+
# Run Nano Banana model (it should handle both cases)
|
| 93 |
output = replicate.run(
|
| 94 |
"google/nano-banana",
|
| 95 |
+
input=model_input
|
|
|
|
|
|
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
+
if output is None:
|
| 99 |
+
return None, "No output received"
|
| 100 |
|
| 101 |
+
# Get the generated image
|
| 102 |
+
try:
|
| 103 |
+
if hasattr(output, 'read'):
|
| 104 |
+
img_data = output.read()
|
| 105 |
+
img = Image.open(BytesIO(img_data))
|
| 106 |
+
return img, status_msg
|
| 107 |
+
except:
|
| 108 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
|
|
|
|
|
|
| 110 |
try:
|
| 111 |
+
if hasattr(output, 'url'):
|
| 112 |
+
output_url = output.url()
|
| 113 |
+
response = requests.get(output_url, timeout=30)
|
| 114 |
+
if response.status_code == 200:
|
| 115 |
+
img = Image.open(BytesIO(response.content))
|
| 116 |
+
return img, status_msg
|
| 117 |
+
except:
|
| 118 |
+
pass
|
| 119 |
+
|
| 120 |
+
output_url = None
|
| 121 |
+
if isinstance(output, str):
|
| 122 |
+
output_url = output
|
| 123 |
+
elif isinstance(output, list) and len(output) > 0:
|
| 124 |
+
output_url = output[0]
|
| 125 |
+
|
| 126 |
+
if output_url:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
response = requests.get(output_url, timeout=30)
|
| 128 |
if response.status_code == 200:
|
| 129 |
img = Image.open(BytesIO(response.content))
|
| 130 |
+
return img, status_msg
|
| 131 |
+
|
| 132 |
+
return None, "Could not process output"
|
| 133 |
+
|
| 134 |
+
except Exception as e:
|
| 135 |
+
error_msg = str(e)
|
| 136 |
+
if "image_input" in error_msg.lower():
|
| 137 |
+
# If the model requires images, provide a helpful message
|
| 138 |
+
return None, "Note: This model may require at least one image. Try uploading an image or check the error: " + error_msg[:100]
|
| 139 |
+
return None, f"Error: {error_msg[:100]}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
# Enhanced CSS with modern, minimal design
|
| 142 |
css = """
|
|
|
|
| 281 |
<h1 class="logo-text">🍌 Free Nano Banana</h1>
|
| 282 |
<p class="subtitle">AI-Powered Image Generation & Style Transfer</p>
|
| 283 |
<div class="mode-indicator">
|
| 284 |
+
💡 Works with or without images - Just describe what you want!
|
| 285 |
</div>
|
| 286 |
<div style="display: flex; justify-content: center; align-items: center; gap: 10px; margin-top: 20px;">
|
| 287 |
<a href="https://huggingface.co/spaces/ginigen/Nano-Banana-PRO" target="_blank">
|
|
|
|
| 306 |
# Info box
|
| 307 |
gr.HTML("""
|
| 308 |
<div class="info-box">
|
| 309 |
+
<strong>How to use Nano Banana:</strong><br>
|
| 310 |
+
• <b>Text Generation:</b> Just enter a prompt - no images needed!<br>
|
| 311 |
+
• <b>Style Transfer:</b> Add images to apply specific styles<br>
|
| 312 |
+
• The powerful Nano Banana model handles both modes seamlessly
|
| 313 |
</div>
|
| 314 |
""")
|
| 315 |
|
|
|
|
| 318 |
with gr.Column(scale=1):
|
| 319 |
prompt = gr.Textbox(
|
| 320 |
label="Prompt / Style Description",
|
| 321 |
+
placeholder="Describe what you want to generate...",
|
| 322 |
lines=3,
|
| 323 |
+
value="A beautiful banana-themed paradise with golden sunset",
|
| 324 |
elem_classes="prompt-input"
|
| 325 |
)
|
| 326 |
|
|
|
|
| 358 |
interactive=False,
|
| 359 |
lines=1,
|
| 360 |
elem_classes="status-text",
|
| 361 |
+
value="Ready to generate with Nano Banana..."
|
| 362 |
)
|
| 363 |
|
| 364 |
# Event handler
|
|
|
|
| 368 |
outputs=[output_image, status]
|
| 369 |
)
|
| 370 |
|
| 371 |
+
# Examples for text-only generation
|
| 372 |
gr.Examples(
|
| 373 |
examples=[
|
| 374 |
+
["A majestic banana kingdom floating in the clouds", None, None],
|
| 375 |
+
["Cyberpunk banana city with neon lights", None, None],
|
| 376 |
+
["Ancient banana temple in a mystical forest", None, None],
|
| 377 |
+
["Banana spaceship exploring the cosmos", None, None],
|
| 378 |
+
["Make the sheets in the style of the logo. Make the scene natural.", None, None],
|
| 379 |
],
|
| 380 |
inputs=[prompt, image1, image2],
|
| 381 |
+
label="Text Generation Examples (No Images Needed!)"
|
| 382 |
)
|
| 383 |
|
| 384 |
# Launch
|