Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -21,13 +21,11 @@ import time
|
|
21 |
os.environ['REPLICATE_API_TOKEN'] = os.getenv('REPLICATE_API_TOKEN')
|
22 |
|
23 |
# Video Model Configuration
|
24 |
-
LANDSCAPE_WIDTH = 512
|
25 |
-
LANDSCAPE_HEIGHT = 320
|
26 |
MAX_SEED = np.iinfo(np.int32).max
|
27 |
-
FIXED_FPS =
|
28 |
|
29 |
-
default_prompt_i2v = "make this image come alive, smooth animation"
|
30 |
-
default_negative_prompt = "static, still, blurry, low quality"
|
31 |
|
32 |
# ===========================
|
33 |
# Helper Functions
|
@@ -46,6 +44,8 @@ def upload_image_to_hosting(image):
|
|
46 |
background = Image.new('RGB', image.size, (255, 255, 255))
|
47 |
background.paste(image, mask=image.split()[3])
|
48 |
image = background
|
|
|
|
|
49 |
|
50 |
# Try imgbb.com first
|
51 |
buffered = BytesIO()
|
@@ -69,7 +69,21 @@ def upload_image_to_hosting(image):
|
|
69 |
except Exception as e:
|
70 |
print(f"Upload error: {e}")
|
71 |
|
72 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
buffered = BytesIO()
|
74 |
image.save(buffered, format="PNG")
|
75 |
buffered.seek(0)
|
@@ -81,61 +95,116 @@ def upload_image_to_hosting(image):
|
|
81 |
# ===========================
|
82 |
|
83 |
def process_images(prompt, image1, image2=None):
|
84 |
-
"""Process images using Replicate API"""
|
85 |
-
if not prompt or prompt.strip() == "":
|
86 |
-
return None, "Please enter a prompt", None
|
87 |
-
|
88 |
if not check_api_token():
|
89 |
return None, "β οΈ Please set REPLICATE_API_TOKEN in Space settings (Settings > Repository secrets)", None
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
"
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
110 |
img_url = output[0]
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
else:
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
#
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
# ===========================
|
134 |
# Video Generation Functions
|
135 |
# ===========================
|
136 |
|
137 |
-
def resize_image_for_video(image: Image.Image
|
138 |
-
"""Resize
|
|
|
139 |
# Convert RGBA to RGB
|
140 |
if image.mode == 'RGBA':
|
141 |
background = Image.new('RGB', image.size, (255, 255, 255))
|
@@ -144,9 +213,32 @@ def resize_image_for_video(image: Image.Image) -> Image.Image:
|
|
144 |
elif image.mode != 'RGB':
|
145 |
image = image.convert('RGB')
|
146 |
|
147 |
-
#
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
@spaces.GPU(duration=60)
|
152 |
def generate_video_gpu(
|
@@ -156,24 +248,21 @@ def generate_video_gpu(
|
|
156 |
negative_prompt,
|
157 |
duration_seconds,
|
158 |
seed,
|
159 |
-
randomize_seed
|
|
|
160 |
):
|
161 |
"""GPU-accelerated video generation"""
|
162 |
try:
|
163 |
# This function runs on GPU
|
164 |
-
# For demo purposes, we'll just process the image
|
165 |
-
# In production, you would run actual video generation here
|
166 |
-
|
167 |
# Clear GPU memory
|
168 |
if torch.cuda.is_available():
|
169 |
torch.cuda.empty_cache()
|
170 |
gc.collect()
|
171 |
|
172 |
-
# Simulate video generation
|
173 |
-
time.sleep(2)
|
174 |
|
175 |
-
|
176 |
-
return None, seed, "π¬ Video generation simulated (GPU function executed successfully)"
|
177 |
|
178 |
except Exception as e:
|
179 |
return None, seed, f"GPU Error: {str(e)[:200]}"
|
@@ -185,9 +274,10 @@ def generate_video_replicate(
|
|
185 |
negative_prompt="",
|
186 |
duration_seconds=2.0,
|
187 |
seed=42,
|
188 |
-
randomize_seed=False
|
|
|
189 |
):
|
190 |
-
"""Generate video using Replicate API
|
191 |
|
192 |
if not check_api_token():
|
193 |
return None, seed, "β οΈ Please set REPLICATE_API_TOKEN"
|
@@ -196,8 +286,14 @@ def generate_video_replicate(
|
|
196 |
return None, seed, "Please provide an input image"
|
197 |
|
198 |
try:
|
199 |
-
#
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
# Upload image
|
203 |
img_url = upload_image_to_hosting(resized_image)
|
@@ -206,16 +302,17 @@ def generate_video_replicate(
|
|
206 |
|
207 |
print("Generating video with Replicate...")
|
208 |
|
209 |
-
# Use
|
210 |
output = replicate.run(
|
211 |
"stability-ai/stable-video-diffusion:3f0457e4619daac51203dedb472816fd4af51f3149fa7a9e0b5ffcf1b8172438",
|
212 |
input={
|
213 |
"input_image": img_url,
|
214 |
"frames_per_second": FIXED_FPS,
|
215 |
-
"motion_bucket_id": 127, # Controls motion amount
|
216 |
-
"cond_aug": 0.02,
|
217 |
-
"decoding_t": 7, # Number of frames
|
218 |
-
"seed": current_seed
|
|
|
219 |
}
|
220 |
)
|
221 |
|
@@ -227,7 +324,7 @@ def generate_video_replicate(
|
|
227 |
if response.status_code == 200:
|
228 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_video:
|
229 |
tmp_video.write(response.content)
|
230 |
-
return tmp_video.name, current_seed, "π¬ Video generated
|
231 |
|
232 |
return None, seed, "Failed to generate video"
|
233 |
|
@@ -244,42 +341,66 @@ def generate_video_replicate(
|
|
244 |
|
245 |
css = """
|
246 |
.gradio-container {
|
247 |
-
max-width:
|
248 |
margin: 0 auto !important;
|
249 |
padding: 20px !important;
|
250 |
}
|
251 |
.header-container {
|
252 |
-
background: linear-gradient(135deg, #ffd93d
|
253 |
-
padding:
|
254 |
border-radius: 20px;
|
255 |
margin-bottom: 2rem;
|
256 |
text-align: center;
|
257 |
-
box-shadow: 0
|
258 |
}
|
259 |
.logo-text {
|
260 |
-
font-size:
|
261 |
font-weight: 900;
|
262 |
color: #2d3436;
|
263 |
margin: 0;
|
264 |
-
text-shadow:
|
|
|
265 |
}
|
266 |
.subtitle {
|
267 |
color: #2d3436;
|
268 |
-
font-size: 1.
|
269 |
margin-top: 0.5rem;
|
270 |
-
font-weight:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
}
|
272 |
.status-box {
|
273 |
-
padding:
|
274 |
-
border-radius:
|
275 |
-
margin:
|
|
|
276 |
}
|
277 |
.gr-button {
|
278 |
transition: all 0.3s ease;
|
|
|
279 |
}
|
280 |
.gr-button:hover {
|
281 |
transform: translateY(-2px);
|
282 |
-
box-shadow: 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
}
|
284 |
"""
|
285 |
|
@@ -296,10 +417,10 @@ def create_interface():
|
|
296 |
gr.HTML("""
|
297 |
<div class="header-container">
|
298 |
<h1 class="logo-text">π Nano Banana + Video</h1>
|
299 |
-
<p class="subtitle">
|
300 |
<div style="margin-top: 1rem;">
|
301 |
-
<p style="color: #
|
302 |
-
|
303 |
</p>
|
304 |
</div>
|
305 |
</div>
|
@@ -310,47 +431,62 @@ def create_interface():
|
|
310 |
gr.HTML(f"""
|
311 |
<div class="status-box" style="background: {'#d4edda' if check_api_token() else '#f8d7da'};
|
312 |
color: {'#155724' if check_api_token() else '#721c24'};">
|
313 |
-
<b>API Status:</b> {'β
|
314 |
</div>
|
315 |
""")
|
316 |
|
317 |
# Tabs
|
318 |
with gr.Tabs() as tabs:
|
319 |
# Image Generation Tab
|
320 |
-
with gr.TabItem("π¨ Step 1: Generate Image", id=1):
|
321 |
with gr.Row():
|
322 |
-
with gr.Column(scale=
|
323 |
style_prompt = gr.Textbox(
|
324 |
-
label="
|
325 |
-
placeholder="
|
326 |
lines=3,
|
327 |
-
value=""
|
328 |
)
|
329 |
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
|
|
|
|
|
|
342 |
|
343 |
generate_img_btn = gr.Button(
|
344 |
-
"π¨ Generate Image",
|
345 |
variant="primary",
|
346 |
size="lg"
|
347 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
348 |
|
349 |
-
with gr.Column(scale=
|
350 |
output_image = gr.Image(
|
351 |
-
label="Generated
|
352 |
type="pil",
|
353 |
-
height=
|
354 |
)
|
355 |
|
356 |
img_status = gr.Textbox(
|
@@ -359,61 +495,92 @@ def create_interface():
|
|
359 |
value="Ready to generate..."
|
360 |
)
|
361 |
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
|
369 |
# Video Generation Tab
|
370 |
with gr.TabItem("π¬ Step 2: Generate Video", id=2):
|
371 |
-
gr.Markdown("###
|
372 |
|
373 |
with gr.Row():
|
374 |
-
with gr.Column(scale=
|
375 |
video_input_image = gr.Image(
|
376 |
type="pil",
|
377 |
-
label="Input Image
|
378 |
-
height=
|
379 |
)
|
380 |
|
381 |
video_prompt = gr.Textbox(
|
382 |
-
label="
|
383 |
-
value=
|
384 |
-
lines=2
|
|
|
385 |
)
|
386 |
|
387 |
with gr.Row():
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
)
|
395 |
-
|
396 |
-
|
|
|
397 |
value=True
|
398 |
)
|
399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
400 |
generate_video_btn = gr.Button(
|
401 |
-
"π¬ Generate Video
|
402 |
variant="primary",
|
403 |
size="lg"
|
404 |
)
|
405 |
|
406 |
-
# GPU Test Button (
|
407 |
-
with gr.Accordion("
|
408 |
test_gpu_btn = gr.Button(
|
409 |
"π₯οΈ Test GPU Function",
|
410 |
variant="secondary"
|
411 |
)
|
412 |
|
413 |
-
with gr.Column(scale=
|
414 |
video_output = gr.Video(
|
415 |
-
label="Generated Video",
|
416 |
-
autoplay=True
|
|
|
417 |
)
|
418 |
|
419 |
video_status = gr.Textbox(
|
@@ -421,23 +588,30 @@ def create_interface():
|
|
421 |
interactive=False,
|
422 |
value="Ready to generate video..."
|
423 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
424 |
|
425 |
# Event Handlers
|
426 |
-
def on_image_generated(prompt,
|
427 |
-
img, status, state_img = process_images(prompt,
|
428 |
if img:
|
429 |
return img, status, state_img, gr.update(visible=True)
|
430 |
return None, status, None, gr.update(visible=False)
|
431 |
|
432 |
def send_image_to_video(img):
|
433 |
if img:
|
434 |
-
return img, "β
Image loaded! Ready to generate video."
|
435 |
return None, "β No image to send."
|
436 |
|
437 |
# Connect events
|
438 |
generate_img_btn.click(
|
439 |
fn=on_image_generated,
|
440 |
-
inputs=[style_prompt],
|
441 |
outputs=[output_image, img_status, generated_image_state, send_to_video_btn]
|
442 |
)
|
443 |
|
@@ -453,11 +627,12 @@ def create_interface():
|
|
453 |
inputs=[
|
454 |
video_input_image,
|
455 |
video_prompt,
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
video_seed,
|
460 |
-
randomize_seed
|
|
|
461 |
],
|
462 |
outputs=[video_output, video_seed, video_status]
|
463 |
)
|
@@ -468,22 +643,33 @@ def create_interface():
|
|
468 |
inputs=[
|
469 |
video_input_image,
|
470 |
video_prompt,
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
video_seed,
|
475 |
-
randomize_seed
|
|
|
476 |
],
|
477 |
outputs=[video_output, video_seed, video_status]
|
478 |
)
|
479 |
|
480 |
# Footer
|
481 |
gr.HTML("""
|
482 |
-
<div style="margin-top:
|
483 |
-
<p style="text-align: center; color:
|
484 |
-
|
485 |
-
<
|
486 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
487 |
</p>
|
488 |
</div>
|
489 |
""")
|
@@ -493,7 +679,7 @@ def create_interface():
|
|
493 |
# Launch
|
494 |
if __name__ == "__main__":
|
495 |
print("=" * 50)
|
496 |
-
print("
|
497 |
print("=" * 50)
|
498 |
|
499 |
if check_api_token():
|
@@ -503,6 +689,11 @@ if __name__ == "__main__":
|
|
503 |
print("Please add it in Settings > Repository secrets")
|
504 |
|
505 |
print("=" * 50)
|
|
|
|
|
|
|
|
|
|
|
506 |
|
507 |
# Create and launch the interface
|
508 |
demo = create_interface()
|
|
|
21 |
os.environ['REPLICATE_API_TOKEN'] = os.getenv('REPLICATE_API_TOKEN')
|
22 |
|
23 |
# Video Model Configuration
|
|
|
|
|
24 |
MAX_SEED = np.iinfo(np.int32).max
|
25 |
+
FIXED_FPS = 16
|
26 |
|
27 |
+
default_prompt_i2v = "make this image come alive, smooth animation, cinematic motion"
|
28 |
+
default_negative_prompt = "static, still, blurry, low quality, distorted"
|
29 |
|
30 |
# ===========================
|
31 |
# Helper Functions
|
|
|
44 |
background = Image.new('RGB', image.size, (255, 255, 255))
|
45 |
background.paste(image, mask=image.split()[3])
|
46 |
image = background
|
47 |
+
elif image.mode != 'RGB':
|
48 |
+
image = image.convert('RGB')
|
49 |
|
50 |
# Try imgbb.com first
|
51 |
buffered = BytesIO()
|
|
|
69 |
except Exception as e:
|
70 |
print(f"Upload error: {e}")
|
71 |
|
72 |
+
# Try 0x0.st as fallback
|
73 |
+
try:
|
74 |
+
buffered = BytesIO()
|
75 |
+
image.save(buffered, format="PNG")
|
76 |
+
buffered.seek(0)
|
77 |
+
|
78 |
+
files = {'file': ('image.png', buffered, 'image/png')}
|
79 |
+
response = requests.post("https://0x0.st", files=files, timeout=30)
|
80 |
+
|
81 |
+
if response.status_code == 200:
|
82 |
+
return response.text.strip()
|
83 |
+
except:
|
84 |
+
pass
|
85 |
+
|
86 |
+
# Final fallback to base64
|
87 |
buffered = BytesIO()
|
88 |
image.save(buffered, format="PNG")
|
89 |
buffered.seek(0)
|
|
|
95 |
# ===========================
|
96 |
|
97 |
def process_images(prompt, image1, image2=None):
|
98 |
+
"""Process images using Replicate API with style transfer"""
|
|
|
|
|
|
|
99 |
if not check_api_token():
|
100 |
return None, "β οΈ Please set REPLICATE_API_TOKEN in Space settings (Settings > Repository secrets)", None
|
101 |
|
102 |
+
# Check if we have at least one image for style transfer
|
103 |
+
if image1 is None:
|
104 |
+
# Pure text-to-image generation
|
105 |
+
if not prompt or prompt.strip() == "":
|
106 |
+
return None, "Please enter a prompt or upload an image", None
|
107 |
+
|
108 |
+
try:
|
109 |
+
print(f"Generating image from text: {prompt}")
|
110 |
+
|
111 |
+
output = replicate.run(
|
112 |
+
"stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b",
|
113 |
+
input={
|
114 |
+
"prompt": prompt + ", high quality, detailed, 8k",
|
115 |
+
"negative_prompt": "blurry, low quality, distorted, deformed",
|
116 |
+
"width": 1024,
|
117 |
+
"height": 1024,
|
118 |
+
"num_inference_steps": 30,
|
119 |
+
"guidance_scale": 7.5
|
120 |
+
}
|
121 |
+
)
|
122 |
+
|
123 |
+
if output and isinstance(output, list) and len(output) > 0:
|
124 |
img_url = output[0]
|
125 |
+
response = requests.get(img_url, timeout=30)
|
126 |
+
if response.status_code == 200:
|
127 |
+
img = Image.open(BytesIO(response.content))
|
128 |
+
return img, "β¨ Image generated from text!", img
|
129 |
+
|
130 |
+
except Exception as e:
|
131 |
+
return None, f"Error: {str(e)[:200]}", None
|
132 |
+
|
133 |
+
else:
|
134 |
+
# Style transfer with images
|
135 |
+
try:
|
136 |
+
print(f"Processing style transfer with prompt: {prompt}")
|
137 |
+
|
138 |
+
# Upload primary image
|
139 |
+
url1 = upload_image_to_hosting(image1)
|
140 |
+
|
141 |
+
# If we have two images, combine them for style transfer
|
142 |
+
if image2:
|
143 |
+
url2 = upload_image_to_hosting(image2)
|
144 |
+
|
145 |
+
# Use a style transfer model (example using SDXL with image prompt)
|
146 |
+
# Note: Replace with actual style transfer model if available
|
147 |
+
output = replicate.run(
|
148 |
+
"stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b",
|
149 |
+
input={
|
150 |
+
"prompt": f"{prompt}, style fusion, artistic blend",
|
151 |
+
"negative_prompt": "blurry, low quality, distorted",
|
152 |
+
"width": 1024,
|
153 |
+
"height": 1024,
|
154 |
+
"num_inference_steps": 30,
|
155 |
+
"guidance_scale": 7.5,
|
156 |
+
# Some models support image inputs for style
|
157 |
+
# "image": url1, # Uncomment if model supports
|
158 |
+
# "style_image": url2, # Uncomment if model supports
|
159 |
+
}
|
160 |
+
)
|
161 |
+
|
162 |
+
status_msg = "β¨ Style transfer completed with both images!"
|
163 |
else:
|
164 |
+
# Single image enhancement/modification
|
165 |
+
output = replicate.run(
|
166 |
+
"tencentarc/gfpgan:9283608cc6b7be6b65a8e44983db012355fde4132009bf99d976b2f0896856a3",
|
167 |
+
input={
|
168 |
+
"img": url1,
|
169 |
+
"version": "v1.4",
|
170 |
+
"scale": 2
|
171 |
+
}
|
172 |
+
)
|
173 |
+
|
174 |
+
status_msg = "β¨ Image enhanced successfully!"
|
175 |
|
176 |
+
# Process output
|
177 |
+
if output:
|
178 |
+
if isinstance(output, list) and len(output) > 0:
|
179 |
+
img_url = output[0]
|
180 |
+
elif isinstance(output, str):
|
181 |
+
img_url = output
|
182 |
+
else:
|
183 |
+
img_url = str(output)
|
184 |
+
|
185 |
+
response = requests.get(img_url, timeout=30)
|
186 |
+
if response.status_code == 200:
|
187 |
+
img = Image.open(BytesIO(response.content))
|
188 |
+
return img, status_msg, img
|
189 |
+
|
190 |
+
return None, "Failed to process images", None
|
191 |
+
|
192 |
+
except Exception as e:
|
193 |
+
error_msg = str(e)
|
194 |
+
if "authentication" in error_msg.lower():
|
195 |
+
return None, "β Invalid API token. Please check your REPLICATE_API_TOKEN.", None
|
196 |
+
elif "rate limit" in error_msg.lower():
|
197 |
+
return None, "β³ Rate limit reached. Please try again later.", None
|
198 |
+
else:
|
199 |
+
return None, f"Error: {error_msg[:200]}", None
|
200 |
|
201 |
# ===========================
|
202 |
# Video Generation Functions
|
203 |
# ===========================
|
204 |
|
205 |
+
def resize_image_for_video(image: Image.Image, target_width=None, target_height=None):
|
206 |
+
"""Resize image for video generation while maintaining aspect ratio"""
|
207 |
+
|
208 |
# Convert RGBA to RGB
|
209 |
if image.mode == 'RGBA':
|
210 |
background = Image.new('RGB', image.size, (255, 255, 255))
|
|
|
213 |
elif image.mode != 'RGB':
|
214 |
image = image.convert('RGB')
|
215 |
|
216 |
+
# Get original dimensions
|
217 |
+
orig_width, orig_height = image.size
|
218 |
+
aspect_ratio = orig_width / orig_height
|
219 |
+
|
220 |
+
# If no target dimensions specified, use original aspect ratio with constraints
|
221 |
+
if target_width is None or target_height is None:
|
222 |
+
# Determine if landscape or portrait
|
223 |
+
if aspect_ratio > 1: # Landscape
|
224 |
+
target_width = min(1024, orig_width)
|
225 |
+
target_height = int(target_width / aspect_ratio)
|
226 |
+
else: # Portrait or square
|
227 |
+
target_height = min(1024, orig_height)
|
228 |
+
target_width = int(target_height * aspect_ratio)
|
229 |
+
|
230 |
+
# Ensure dimensions are divisible by 8 (required by many models)
|
231 |
+
target_width = (target_width // 8) * 8
|
232 |
+
target_height = (target_height // 8) * 8
|
233 |
+
|
234 |
+
# Minimum size constraints
|
235 |
+
target_width = max(256, target_width)
|
236 |
+
target_height = max(256, target_height)
|
237 |
+
|
238 |
+
# Resize image
|
239 |
+
resized = image.resize((target_width, target_height), Image.LANCZOS)
|
240 |
+
|
241 |
+
return resized, target_width, target_height
|
242 |
|
243 |
@spaces.GPU(duration=60)
|
244 |
def generate_video_gpu(
|
|
|
248 |
negative_prompt,
|
249 |
duration_seconds,
|
250 |
seed,
|
251 |
+
randomize_seed,
|
252 |
+
maintain_aspect_ratio
|
253 |
):
|
254 |
"""GPU-accelerated video generation"""
|
255 |
try:
|
256 |
# This function runs on GPU
|
|
|
|
|
|
|
257 |
# Clear GPU memory
|
258 |
if torch.cuda.is_available():
|
259 |
torch.cuda.empty_cache()
|
260 |
gc.collect()
|
261 |
|
262 |
+
# Simulate video generation for testing
|
263 |
+
time.sleep(2)
|
264 |
|
265 |
+
return None, seed, "π¬ GPU test completed (actual video generation requires specific models)"
|
|
|
266 |
|
267 |
except Exception as e:
|
268 |
return None, seed, f"GPU Error: {str(e)[:200]}"
|
|
|
274 |
negative_prompt="",
|
275 |
duration_seconds=2.0,
|
276 |
seed=42,
|
277 |
+
randomize_seed=False,
|
278 |
+
maintain_aspect_ratio=True
|
279 |
):
|
280 |
+
"""Generate video using Replicate API with aspect ratio preservation"""
|
281 |
|
282 |
if not check_api_token():
|
283 |
return None, seed, "β οΈ Please set REPLICATE_API_TOKEN"
|
|
|
286 |
return None, seed, "Please provide an input image"
|
287 |
|
288 |
try:
|
289 |
+
# Get image dimensions while maintaining aspect ratio
|
290 |
+
if maintain_aspect_ratio:
|
291 |
+
resized_image, video_width, video_height = resize_image_for_video(input_image)
|
292 |
+
print(f"Video dimensions: {video_width}x{video_height} (maintaining aspect ratio)")
|
293 |
+
else:
|
294 |
+
# Default landscape dimensions
|
295 |
+
resized_image, video_width, video_height = resize_image_for_video(input_image, 768, 512)
|
296 |
+
print(f"Video dimensions: {video_width}x{video_height} (fixed landscape)")
|
297 |
|
298 |
# Upload image
|
299 |
img_url = upload_image_to_hosting(resized_image)
|
|
|
302 |
|
303 |
print("Generating video with Replicate...")
|
304 |
|
305 |
+
# Use Stable Video Diffusion
|
306 |
output = replicate.run(
|
307 |
"stability-ai/stable-video-diffusion:3f0457e4619daac51203dedb472816fd4af51f3149fa7a9e0b5ffcf1b8172438",
|
308 |
input={
|
309 |
"input_image": img_url,
|
310 |
"frames_per_second": FIXED_FPS,
|
311 |
+
"motion_bucket_id": 127, # Controls motion amount (0-255)
|
312 |
+
"cond_aug": 0.02, # Conditioning augmentation
|
313 |
+
"decoding_t": min(14, int(duration_seconds * 7)), # Number of frames
|
314 |
+
"seed": current_seed,
|
315 |
+
"sizing_strategy": "maintain_aspect_ratio" # Preserve aspect ratio
|
316 |
}
|
317 |
)
|
318 |
|
|
|
324 |
if response.status_code == 200:
|
325 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_video:
|
326 |
tmp_video.write(response.content)
|
327 |
+
return tmp_video.name, current_seed, f"π¬ Video generated! ({video_width}x{video_height}, aspect ratio preserved)"
|
328 |
|
329 |
return None, seed, "Failed to generate video"
|
330 |
|
|
|
341 |
|
342 |
css = """
|
343 |
.gradio-container {
|
344 |
+
max-width: 1400px !important;
|
345 |
margin: 0 auto !important;
|
346 |
padding: 20px !important;
|
347 |
}
|
348 |
.header-container {
|
349 |
+
background: linear-gradient(135deg, #ffd93d 0%, #ffb347 50%, #ff6b6b 100%);
|
350 |
+
padding: 3rem;
|
351 |
border-radius: 20px;
|
352 |
margin-bottom: 2rem;
|
353 |
text-align: center;
|
354 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
355 |
}
|
356 |
.logo-text {
|
357 |
+
font-size: 3.5rem;
|
358 |
font-weight: 900;
|
359 |
color: #2d3436;
|
360 |
margin: 0;
|
361 |
+
text-shadow: 3px 3px 6px rgba(0,0,0,0.1);
|
362 |
+
letter-spacing: -2px;
|
363 |
}
|
364 |
.subtitle {
|
365 |
color: #2d3436;
|
366 |
+
font-size: 1.3rem;
|
367 |
margin-top: 0.5rem;
|
368 |
+
font-weight: 600;
|
369 |
+
}
|
370 |
+
.image-upload-container {
|
371 |
+
border: 3px dashed #ffd93d;
|
372 |
+
border-radius: 15px;
|
373 |
+
padding: 20px;
|
374 |
+
background: #fffef5;
|
375 |
+
transition: all 0.3s ease;
|
376 |
+
}
|
377 |
+
.image-upload-container:hover {
|
378 |
+
border-color: #ffb347;
|
379 |
+
background: #fff9e6;
|
380 |
}
|
381 |
.status-box {
|
382 |
+
padding: 12px;
|
383 |
+
border-radius: 10px;
|
384 |
+
margin: 15px 0;
|
385 |
+
font-weight: 500;
|
386 |
}
|
387 |
.gr-button {
|
388 |
transition: all 0.3s ease;
|
389 |
+
font-weight: 600 !important;
|
390 |
}
|
391 |
.gr-button:hover {
|
392 |
transform: translateY(-2px);
|
393 |
+
box-shadow: 0 8px 20px rgba(0,0,0,0.15);
|
394 |
+
}
|
395 |
+
.gr-button-primary {
|
396 |
+
background: linear-gradient(135deg, #ffd93d, #ffb347) !important;
|
397 |
+
color: #2d3436 !important;
|
398 |
+
border: none !important;
|
399 |
+
}
|
400 |
+
.gr-button-secondary {
|
401 |
+
background: linear-gradient(135deg, #667eea, #764ba2) !important;
|
402 |
+
color: white !important;
|
403 |
+
border: none !important;
|
404 |
}
|
405 |
"""
|
406 |
|
|
|
417 |
gr.HTML("""
|
418 |
<div class="header-container">
|
419 |
<h1 class="logo-text">π Nano Banana + Video</h1>
|
420 |
+
<p class="subtitle">Style Transfer & AI Video Generation</p>
|
421 |
<div style="margin-top: 1rem;">
|
422 |
+
<p style="color: #2d3436; font-size: 1rem; opacity: 0.9;">
|
423 |
+
Upload up to 2 images for style transfer β Generate amazing visuals β Convert to video
|
424 |
</p>
|
425 |
</div>
|
426 |
</div>
|
|
|
431 |
gr.HTML(f"""
|
432 |
<div class="status-box" style="background: {'#d4edda' if check_api_token() else '#f8d7da'};
|
433 |
color: {'#155724' if check_api_token() else '#721c24'};">
|
434 |
+
<b>API Status:</b> {'β
Replicate token configured' if check_api_token() else 'β Token missing - Add REPLICATE_API_TOKEN in Settings > Repository secrets'}
|
435 |
</div>
|
436 |
""")
|
437 |
|
438 |
# Tabs
|
439 |
with gr.Tabs() as tabs:
|
440 |
# Image Generation Tab
|
441 |
+
with gr.TabItem("π¨ Step 1: Generate/Transform Image", id=1):
|
442 |
with gr.Row():
|
443 |
+
with gr.Column(scale=1):
|
444 |
style_prompt = gr.Textbox(
|
445 |
+
label="βοΈ Style Description",
|
446 |
+
placeholder="Describe the style or transformation you want...",
|
447 |
lines=3,
|
448 |
+
value="Transform into a magical fantasy scene with vibrant colors"
|
449 |
)
|
450 |
|
451 |
+
with gr.Column(elem_classes="image-upload-container"):
|
452 |
+
gr.Markdown("### π€ Upload Images (Optional)")
|
453 |
+
gr.Markdown("Upload 1-2 images for style transfer, or leave empty for text-to-image")
|
454 |
+
|
455 |
+
with gr.Row():
|
456 |
+
image1 = gr.Image(
|
457 |
+
label="Primary Image",
|
458 |
+
type="pil",
|
459 |
+
height=200
|
460 |
+
)
|
461 |
+
image2 = gr.Image(
|
462 |
+
label="Secondary Image (Optional)",
|
463 |
+
type="pil",
|
464 |
+
height=200
|
465 |
+
)
|
466 |
|
467 |
generate_img_btn = gr.Button(
|
468 |
+
"π¨ Generate/Transform Image",
|
469 |
variant="primary",
|
470 |
size="lg"
|
471 |
)
|
472 |
+
|
473 |
+
# Examples
|
474 |
+
gr.Examples(
|
475 |
+
examples=[
|
476 |
+
["Magical forest with glowing mushrooms, fantasy art style", None, None],
|
477 |
+
["Cyberpunk city with neon lights, blade runner style", None, None],
|
478 |
+
["Studio Ghibli style peaceful countryside", None, None],
|
479 |
+
["Steampunk mechanical dragon, brass and copper", None, None],
|
480 |
+
],
|
481 |
+
inputs=[style_prompt, image1, image2],
|
482 |
+
label="π‘ Quick Prompts"
|
483 |
+
)
|
484 |
|
485 |
+
with gr.Column(scale=1):
|
486 |
output_image = gr.Image(
|
487 |
+
label="β¨ Generated Result",
|
488 |
type="pil",
|
489 |
+
height=450
|
490 |
)
|
491 |
|
492 |
img_status = gr.Textbox(
|
|
|
495 |
value="Ready to generate..."
|
496 |
)
|
497 |
|
498 |
+
send_to_video_btn = gr.Button(
|
499 |
+
"β‘οΈ Send to Video Generation",
|
500 |
+
variant="secondary",
|
501 |
+
size="lg",
|
502 |
+
visible=False
|
503 |
+
)
|
504 |
|
505 |
# Video Generation Tab
|
506 |
with gr.TabItem("π¬ Step 2: Generate Video", id=2):
|
507 |
+
gr.Markdown("### π₯ Bring your image to life with AI-powered animation!")
|
508 |
|
509 |
with gr.Row():
|
510 |
+
with gr.Column(scale=1):
|
511 |
video_input_image = gr.Image(
|
512 |
type="pil",
|
513 |
+
label="πΈ Input Image",
|
514 |
+
height=350
|
515 |
)
|
516 |
|
517 |
video_prompt = gr.Textbox(
|
518 |
+
label="π¬ Animation Style",
|
519 |
+
value=default_prompt_i2v,
|
520 |
+
lines=2,
|
521 |
+
placeholder="Describe how you want the image to move..."
|
522 |
)
|
523 |
|
524 |
with gr.Row():
|
525 |
+
duration_input = gr.Slider(
|
526 |
+
minimum=1.0,
|
527 |
+
maximum=4.0,
|
528 |
+
step=0.5,
|
529 |
+
value=2.0,
|
530 |
+
label="β±οΈ Duration (seconds)"
|
531 |
)
|
532 |
+
|
533 |
+
maintain_aspect = gr.Checkbox(
|
534 |
+
label="πΌοΈ Maintain Original Aspect Ratio",
|
535 |
value=True
|
536 |
)
|
537 |
|
538 |
+
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
539 |
+
video_negative_prompt = gr.Textbox(
|
540 |
+
label="Negative Prompt",
|
541 |
+
value=default_negative_prompt,
|
542 |
+
lines=2
|
543 |
+
)
|
544 |
+
|
545 |
+
with gr.Row():
|
546 |
+
video_seed = gr.Slider(
|
547 |
+
label="Seed",
|
548 |
+
minimum=0,
|
549 |
+
maximum=MAX_SEED,
|
550 |
+
step=1,
|
551 |
+
value=42
|
552 |
+
)
|
553 |
+
randomize_seed = gr.Checkbox(
|
554 |
+
label="π² Random seed",
|
555 |
+
value=True
|
556 |
+
)
|
557 |
+
|
558 |
+
steps_slider = gr.Slider(
|
559 |
+
minimum=10,
|
560 |
+
maximum=50,
|
561 |
+
step=5,
|
562 |
+
value=30,
|
563 |
+
label="Quality Steps"
|
564 |
+
)
|
565 |
+
|
566 |
generate_video_btn = gr.Button(
|
567 |
+
"π¬ Generate Video",
|
568 |
variant="primary",
|
569 |
size="lg"
|
570 |
)
|
571 |
|
572 |
+
# GPU Test Button (hidden by default)
|
573 |
+
with gr.Accordion("π§ͺ Developer Options", open=False):
|
574 |
test_gpu_btn = gr.Button(
|
575 |
"π₯οΈ Test GPU Function",
|
576 |
variant="secondary"
|
577 |
)
|
578 |
|
579 |
+
with gr.Column(scale=1):
|
580 |
video_output = gr.Video(
|
581 |
+
label="π¬ Generated Video",
|
582 |
+
autoplay=True,
|
583 |
+
height=450
|
584 |
)
|
585 |
|
586 |
video_status = gr.Textbox(
|
|
|
588 |
interactive=False,
|
589 |
value="Ready to generate video..."
|
590 |
)
|
591 |
+
|
592 |
+
gr.Markdown("""
|
593 |
+
### π‘ Tips for better videos:
|
594 |
+
- **Aspect Ratio**: Enable "Maintain Original Aspect Ratio" for best results
|
595 |
+
- **Duration**: Start with 2 seconds for faster generation
|
596 |
+
- **Motion**: Describe smooth, simple movements for stability
|
597 |
+
""")
|
598 |
|
599 |
# Event Handlers
|
600 |
+
def on_image_generated(prompt, img1, img2):
|
601 |
+
img, status, state_img = process_images(prompt, img1, img2)
|
602 |
if img:
|
603 |
return img, status, state_img, gr.update(visible=True)
|
604 |
return None, status, None, gr.update(visible=False)
|
605 |
|
606 |
def send_image_to_video(img):
|
607 |
if img:
|
608 |
+
return img, "β
Image loaded! Ready to generate video with original aspect ratio."
|
609 |
return None, "β No image to send."
|
610 |
|
611 |
# Connect events
|
612 |
generate_img_btn.click(
|
613 |
fn=on_image_generated,
|
614 |
+
inputs=[style_prompt, image1, image2],
|
615 |
outputs=[output_image, img_status, generated_image_state, send_to_video_btn]
|
616 |
)
|
617 |
|
|
|
627 |
inputs=[
|
628 |
video_input_image,
|
629 |
video_prompt,
|
630 |
+
steps_slider,
|
631 |
+
video_negative_prompt,
|
632 |
+
duration_input,
|
633 |
video_seed,
|
634 |
+
randomize_seed,
|
635 |
+
maintain_aspect
|
636 |
],
|
637 |
outputs=[video_output, video_seed, video_status]
|
638 |
)
|
|
|
643 |
inputs=[
|
644 |
video_input_image,
|
645 |
video_prompt,
|
646 |
+
steps_slider,
|
647 |
+
video_negative_prompt,
|
648 |
+
duration_input,
|
649 |
video_seed,
|
650 |
+
randomize_seed,
|
651 |
+
maintain_aspect
|
652 |
],
|
653 |
outputs=[video_output, video_seed, video_status]
|
654 |
)
|
655 |
|
656 |
# Footer
|
657 |
gr.HTML("""
|
658 |
+
<div style="margin-top: 3rem; padding: 2rem; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 15px;">
|
659 |
+
<p style="text-align: center; color: white; font-size: 1.1rem;">
|
660 |
+
<b>π Powered by Replicate API & Hugging Face Spaces</b><br>
|
661 |
+
<span style="font-size: 0.9rem; opacity: 0.9;">
|
662 |
+
Transform your ideas into stunning visuals and animations
|
663 |
+
</span>
|
664 |
+
</p>
|
665 |
+
<p style="text-align: center; margin-top: 1rem;">
|
666 |
+
<a href="https://replicate.com/" target="_blank" style="color: #ffd93d; text-decoration: none; margin: 0 10px;">
|
667 |
+
π Get Replicate Token
|
668 |
+
</a>
|
669 |
+
|
|
670 |
+
<a href="https://huggingface.co/spaces" target="_blank" style="color: #ffd93d; text-decoration: none; margin: 0 10px;">
|
671 |
+
π€ Hugging Face Spaces
|
672 |
+
</a>
|
673 |
</p>
|
674 |
</div>
|
675 |
""")
|
|
|
679 |
# Launch
|
680 |
if __name__ == "__main__":
|
681 |
print("=" * 50)
|
682 |
+
print("π Nano Banana + Video Application")
|
683 |
print("=" * 50)
|
684 |
|
685 |
if check_api_token():
|
|
|
689 |
print("Please add it in Settings > Repository secrets")
|
690 |
|
691 |
print("=" * 50)
|
692 |
+
print("Features:")
|
693 |
+
print("- Upload up to 2 images for style transfer")
|
694 |
+
print("- Text-to-image generation")
|
695 |
+
print("- Video generation with aspect ratio preservation")
|
696 |
+
print("=" * 50)
|
697 |
|
698 |
# Create and launch the interface
|
699 |
demo = create_interface()
|