Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
-
import requests
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
-
from PIL import Image
|
| 8 |
-
import replicate
|
| 9 |
-
import base64
|
| 10 |
-
from typing import Optional, Tuple, List
|
| 11 |
import json
|
| 12 |
|
| 13 |
# CSS untuk styling
|
| 14 |
CSS = """
|
| 15 |
-
.container {
|
| 16 |
-
max-width: 1400px;
|
| 17 |
-
margin: auto;
|
| 18 |
-
}
|
| 19 |
.title {
|
| 20 |
text-align: center;
|
| 21 |
-
font-size:
|
| 22 |
font-weight: bold;
|
| 23 |
margin-bottom: 20px;
|
| 24 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 25 |
-
-webkit-background-clip: text;
|
| 26 |
-
-webkit-text-fill-color: transparent;
|
| 27 |
-
}
|
| 28 |
-
.feature-card {
|
| 29 |
-
background: #f8f9fa;
|
| 30 |
-
padding: 20px;
|
| 31 |
-
border-radius: 12px;
|
| 32 |
-
margin: 10px 0;
|
| 33 |
-
border: 1px solid #e0e0e0;
|
| 34 |
-
}
|
| 35 |
-
.edit-btn {
|
| 36 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 37 |
-
color: white !important;
|
| 38 |
-
padding: 12px 24px !important;
|
| 39 |
-
border-radius: 8px !important;
|
| 40 |
-
border: none !important;
|
| 41 |
-
font-weight: bold !important;
|
| 42 |
}
|
| 43 |
"""
|
| 44 |
|
| 45 |
class VideoEditor:
|
| 46 |
def __init__(self):
|
| 47 |
self.api_key = None
|
| 48 |
-
|
| 49 |
-
def set_api_key(self, api_key: str) -> Tuple[bool, str]:
|
| 50 |
-
"""Validate and set API key"""
|
| 51 |
-
if not api_key:
|
| 52 |
-
return False, "❌ Please enter your API key"
|
| 53 |
-
|
| 54 |
-
try:
|
| 55 |
-
# Test API key
|
| 56 |
-
os.environ["REPLICATE_API_TOKEN"] = api_key
|
| 57 |
-
self.api_key = api_key
|
| 58 |
-
return True, "✅ API key set successfully!"
|
| 59 |
-
except Exception as e:
|
| 60 |
-
return False, f"❌ Error: {str(e)}"
|
| 61 |
|
| 62 |
-
def extract_frames(self, video_path
|
| 63 |
"""Extract frames from video"""
|
| 64 |
frames = []
|
|
|
|
|
|
|
|
|
|
| 65 |
cap = cv2.VideoCapture(video_path)
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
|
|
|
|
| 69 |
ret, frame = cap.read()
|
| 70 |
if not ret:
|
| 71 |
break
|
| 72 |
|
| 73 |
if frame_count % sample_rate == 0:
|
| 74 |
frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 75 |
-
|
| 76 |
frame_count += 1
|
| 77 |
|
| 78 |
cap.release()
|
| 79 |
return frames
|
| 80 |
|
| 81 |
-
def
|
| 82 |
-
"""
|
| 83 |
if not frames:
|
| 84 |
return None
|
| 85 |
-
|
| 86 |
-
|
| 87 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 88 |
-
out = cv2.VideoWriter(output_path, fourcc, fps, (
|
| 89 |
|
| 90 |
for frame in frames:
|
| 91 |
out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
|
|
@@ -93,419 +58,217 @@ class VideoEditor:
|
|
| 93 |
out.release()
|
| 94 |
return output_path
|
| 95 |
|
| 96 |
-
def
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
progress(0.1, desc="Extracting frames...")
|
| 109 |
-
frames = self.extract_frames(video_path, sample_rate=15)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
if not frames:
|
| 112 |
-
return None, "
|
| 113 |
-
|
| 114 |
-
edited_frames = []
|
| 115 |
|
|
|
|
|
|
|
| 116 |
for i, frame in enumerate(frames):
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
# Convert frame to PIL Image
|
| 121 |
-
pil_image = Image.fromarray(frame)
|
| 122 |
-
|
| 123 |
-
# Save temp image
|
| 124 |
-
temp_img = tempfile.mktemp(suffix='.jpg')
|
| 125 |
-
pil_image.save(temp_img)
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
# Process output (this is a simplified version)
|
| 137 |
-
# In reality, you'd need to handle the actual API response
|
| 138 |
-
edited_frames.append(frame) # Placeholder
|
| 139 |
-
|
| 140 |
-
os.remove(temp_img)
|
| 141 |
|
| 142 |
-
|
| 143 |
output_path = tempfile.mktemp(suffix='.mp4')
|
| 144 |
-
self.
|
| 145 |
|
| 146 |
-
return output_path, f"
|
| 147 |
|
| 148 |
except Exception as e:
|
| 149 |
-
return None, f"
|
| 150 |
|
| 151 |
-
def
|
| 152 |
-
|
| 153 |
-
video_path:
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
) -> Tuple[Optional[str], str]:
|
| 157 |
-
"""Change character outfit in video"""
|
| 158 |
-
|
| 159 |
-
if not self.api_key:
|
| 160 |
-
return None, "❌ Please set your API key first"
|
| 161 |
-
|
| 162 |
try:
|
| 163 |
-
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
)
|
| 174 |
|
| 175 |
-
|
| 176 |
-
return video_path, f"✅ Outfit changed to: {outfit_prompt}"
|
| 177 |
|
| 178 |
except Exception as e:
|
| 179 |
-
return None, f"
|
| 180 |
|
| 181 |
-
def
|
| 182 |
-
|
| 183 |
-
video_path:
|
| 184 |
-
|
| 185 |
-
color_prompt: str,
|
| 186 |
-
progress=gr.Progress()
|
| 187 |
-
) -> Tuple[Optional[str], str]:
|
| 188 |
-
"""Apply style and color changes to video"""
|
| 189 |
-
|
| 190 |
-
if not self.api_key:
|
| 191 |
-
return None, "❌ Please set your API key first"
|
| 192 |
-
|
| 193 |
-
try:
|
| 194 |
-
progress(0.1, desc="Applying style transfer...")
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
progress(0.1 + 0.8 * (i / len(frames)),
|
| 204 |
-
desc=f"Styling frame {i+1}/{len(frames)}...")
|
| 205 |
-
|
| 206 |
-
# Simple color adjustment for demo
|
| 207 |
if "warm" in color_prompt.lower():
|
| 208 |
-
frame[:,
|
|
|
|
| 209 |
elif "cool" in color_prompt.lower():
|
| 210 |
-
frame[:,
|
|
|
|
| 211 |
elif "vintage" in color_prompt.lower():
|
| 212 |
# Sepia effect
|
| 213 |
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
| 214 |
frame = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
|
| 215 |
-
frame[:,
|
| 216 |
-
frame[:, :, 1] = np.clip(frame[:, :, 1] * 1.0, 0, 255)
|
| 217 |
-
frame[:, :, 2] = np.clip(frame[:, :, 2] * 0.8, 0, 255)
|
| 218 |
|
| 219 |
-
|
|
|
|
|
|
|
| 220 |
|
| 221 |
output_path = tempfile.mktemp(suffix='.mp4')
|
| 222 |
-
self.
|
| 223 |
|
| 224 |
-
return output_path, f"
|
| 225 |
|
| 226 |
except Exception as e:
|
| 227 |
-
return None, f"
|
| 228 |
-
|
| 229 |
-
def edit_storyline(
|
| 230 |
-
self,
|
| 231 |
-
video_path: str,
|
| 232 |
-
storyline_prompt: str,
|
| 233 |
-
scene_changes: str,
|
| 234 |
-
progress=gr.Progress()
|
| 235 |
-
) -> Tuple[Optional[str], str]:
|
| 236 |
-
"""Edit video storyline and scenes"""
|
| 237 |
-
|
| 238 |
-
if not self.api_key:
|
| 239 |
-
return None, "❌ Please set your API key first"
|
| 240 |
-
|
| 241 |
-
try:
|
| 242 |
-
progress(0.1, desc="Analyzing storyline...")
|
| 243 |
-
|
| 244 |
-
# This would use advanced AI models for video understanding and generation
|
| 245 |
-
# For now, we'll create a storyboard visualization
|
| 246 |
-
|
| 247 |
-
frames = self.extract_frames(video_path, sample_rate=30)
|
| 248 |
-
|
| 249 |
-
# Create storyboard with annotations
|
| 250 |
-
storyboard_frames = []
|
| 251 |
-
scenes = scene_changes.split('\n')
|
| 252 |
-
|
| 253 |
-
for i, (frame, scene) in enumerate(zip(frames, scenes + [''] * len(frames))):
|
| 254 |
-
progress(0.1 + 0.8 * (i / len(frames)),
|
| 255 |
-
desc=f"Processing scene {i+1}...")
|
| 256 |
-
|
| 257 |
-
# Add text overlay for scene description
|
| 258 |
-
frame_copy = frame.copy()
|
| 259 |
-
cv2.putText(frame_copy, f"Scene {i+1}: {scene[:50]}...",
|
| 260 |
-
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
| 261 |
-
|
| 262 |
-
storyboard_frames.append(frame_copy)
|
| 263 |
-
|
| 264 |
-
output_path = tempfile.mktemp(suffix='.mp4')
|
| 265 |
-
self.frames_to_video(storyboard_frames, output_path, fps=10)
|
| 266 |
-
|
| 267 |
-
return output_path, f"✅ Storyline edited: {storyline_prompt[:100]}..."
|
| 268 |
-
|
| 269 |
-
except Exception as e:
|
| 270 |
-
return None, f"❌ Error: {str(e)}"
|
| 271 |
|
| 272 |
# Create instance
|
| 273 |
editor = VideoEditor()
|
| 274 |
|
| 275 |
-
# Create
|
| 276 |
-
with gr.Blocks(css=CSS
|
| 277 |
gr.HTML("""
|
| 278 |
-
<
|
| 279 |
-
|
| 280 |
-
<p style="text-align: center; font-size: 1.2em; color: #666;">
|
| 281 |
-
Transform your videos with AI-powered editing using simple prompts
|
| 282 |
-
</p>
|
| 283 |
-
</div>
|
| 284 |
""")
|
| 285 |
|
| 286 |
-
#
|
| 287 |
with gr.Row():
|
| 288 |
-
|
| 289 |
-
gr.Markdown("""
|
| 290 |
-
### 🔑 Step 1: API Setup
|
| 291 |
-
Get your API key from [Replicate](https://replicate.com/account/api-tokens)
|
| 292 |
-
or [RunwayML](https://runwayml.com/)
|
| 293 |
-
""")
|
| 294 |
-
|
| 295 |
-
api_key_input = gr.Textbox(
|
| 296 |
-
label="API Key",
|
| 297 |
-
placeholder="Enter your API key...",
|
| 298 |
-
type="password"
|
| 299 |
-
)
|
| 300 |
-
|
| 301 |
-
api_status = gr.Textbox(label="Status", interactive=False)
|
| 302 |
-
set_key_btn = gr.Button("Set API Key", size="sm")
|
| 303 |
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
# Video Upload
|
| 307 |
-
with gr.Row():
|
| 308 |
-
with gr.Column():
|
| 309 |
-
gr.Markdown("### 📹 Step 2: Upload Your Video")
|
| 310 |
-
video_input = gr.Video(label="Input Video")
|
| 311 |
-
|
| 312 |
-
# Editing Features
|
| 313 |
with gr.Tabs():
|
| 314 |
# Background Tab
|
| 315 |
-
with gr.
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
inputs=bg_prompt
|
| 340 |
-
)
|
| 341 |
-
|
| 342 |
-
change_bg_btn = gr.Button("Change Background", elem_classes="edit-btn")
|
| 343 |
-
gr.HTML('</div>')
|
| 344 |
-
|
| 345 |
-
with gr.Column():
|
| 346 |
-
bg_output = gr.Video(label="Result")
|
| 347 |
-
bg_status = gr.Textbox(label="Status", interactive=False)
|
| 348 |
-
|
| 349 |
-
# Character/Outfit Tab
|
| 350 |
-
with gr.TabItem("👔 Character & Outfit"):
|
| 351 |
-
with gr.Row():
|
| 352 |
-
with gr.Column():
|
| 353 |
-
gr.HTML('<div class="feature-card">')
|
| 354 |
-
gr.Markdown("""
|
| 355 |
-
**Change Character Appearance**
|
| 356 |
-
|
| 357 |
-
Modify clothing, accessories, and character style
|
| 358 |
-
""")
|
| 359 |
-
|
| 360 |
-
outfit_prompt = gr.Textbox(
|
| 361 |
-
label="Outfit Description",
|
| 362 |
-
placeholder="E.g., elegant business suit, superhero costume, medieval armor...",
|
| 363 |
-
lines=2
|
| 364 |
-
)
|
| 365 |
-
|
| 366 |
-
outfit_examples = gr.Examples(
|
| 367 |
-
examples=[
|
| 368 |
-
"Elegant black business suit with red tie",
|
| 369 |
-
"Superhero costume with cape and mask",
|
| 370 |
-
"Medieval knight armor with sword",
|
| 371 |
-
"Casual summer dress with sunhat",
|
| 372 |
-
"Futuristic space suit with helmet"
|
| 373 |
-
],
|
| 374 |
-
inputs=outfit_prompt
|
| 375 |
-
)
|
| 376 |
-
|
| 377 |
-
change_outfit_btn = gr.Button("Change Outfit", elem_classes="edit-btn")
|
| 378 |
-
gr.HTML('</div>')
|
| 379 |
-
|
| 380 |
-
with gr.Column():
|
| 381 |
-
outfit_output = gr.Video(label="Result")
|
| 382 |
-
outfit_status = gr.Textbox(label="Status", interactive=False)
|
| 383 |
|
| 384 |
-
#
|
| 385 |
-
with gr.
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
["Cinematic Hollywood style", "Warm golden hour lighting"],
|
| 410 |
-
["Anime/manga art style", "Vibrant saturated colors"],
|
| 411 |
-
["Film noir style", "High contrast black and white"],
|
| 412 |
-
["Oil painting style", "Rich earthy tones"],
|
| 413 |
-
["Cyberpunk neon style", "Purple and cyan neon colors"]
|
| 414 |
-
],
|
| 415 |
-
inputs=[style_prompt, color_prompt]
|
| 416 |
-
)
|
| 417 |
-
|
| 418 |
-
apply_style_btn = gr.Button("Apply Style", elem_classes="edit-btn")
|
| 419 |
-
gr.HTML('</div>')
|
| 420 |
-
|
| 421 |
-
with gr.Column():
|
| 422 |
-
style_output = gr.Video(label="Result")
|
| 423 |
-
style_status = gr.Textbox(label="Status", interactive=False)
|
| 424 |
|
| 425 |
-
#
|
| 426 |
-
with gr.
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
|
| 455 |
-
# Instructions
|
| 456 |
gr.Markdown("""
|
| 457 |
---
|
| 458 |
-
###
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
3. **Choose an editing feature** from the tabs above
|
| 463 |
-
4. **Describe your changes** using natural language prompts
|
| 464 |
-
5. **Click the edit button** and wait for AI processing
|
| 465 |
-
|
| 466 |
-
### 🚀 Capabilities:
|
| 467 |
-
|
| 468 |
-
- **Background Replacement**: Remove and replace video backgrounds
|
| 469 |
-
- **Outfit Change**: Modify character clothing and accessories
|
| 470 |
-
- **Style Transfer**: Apply artistic styles and color grading
|
| 471 |
-
- **Storyline Editing**: Restructure narrative and add transitions
|
| 472 |
-
|
| 473 |
-
### ⚠️ Note:
|
| 474 |
-
|
| 475 |
-
Processing time depends on video length and complexity. Shorter clips (< 30s) work best.
|
| 476 |
""")
|
| 477 |
-
|
| 478 |
-
# Event handlers
|
| 479 |
-
set_key_btn.click(
|
| 480 |
-
fn=editor.set_api_key,
|
| 481 |
-
inputs=[api_key_input],
|
| 482 |
-
outputs=[api_status]
|
| 483 |
-
)
|
| 484 |
-
|
| 485 |
-
change_bg_btn.click(
|
| 486 |
-
fn=editor.change_background,
|
| 487 |
-
inputs=[video_input, bg_prompt],
|
| 488 |
-
outputs=[bg_output, bg_status]
|
| 489 |
-
)
|
| 490 |
-
|
| 491 |
-
change_outfit_btn.click(
|
| 492 |
-
fn=editor.change_character_outfit,
|
| 493 |
-
inputs=[video_input, outfit_prompt],
|
| 494 |
-
outputs=[outfit_output, outfit_status]
|
| 495 |
-
)
|
| 496 |
-
|
| 497 |
-
apply_style_btn.click(
|
| 498 |
-
fn=editor.change_video_style,
|
| 499 |
-
inputs=[video_input, style_prompt, color_prompt],
|
| 500 |
-
outputs=[style_output, style_status]
|
| 501 |
-
)
|
| 502 |
-
|
| 503 |
-
edit_story_btn.click(
|
| 504 |
-
fn=editor.edit_storyline,
|
| 505 |
-
inputs=[video_input, storyline_prompt, scene_changes],
|
| 506 |
-
outputs=[story_output, story_status]
|
| 507 |
-
)
|
| 508 |
|
| 509 |
# Launch
|
| 510 |
-
|
| 511 |
-
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import tempfile
|
|
|
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
+
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
|
|
|
| 7 |
import json
|
| 8 |
|
| 9 |
# CSS untuk styling
|
| 10 |
CSS = """
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
.title {
|
| 12 |
text-align: center;
|
| 13 |
+
font-size: 2em;
|
| 14 |
font-weight: bold;
|
| 15 |
margin-bottom: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
"""
|
| 18 |
|
| 19 |
class VideoEditor:
|
| 20 |
def __init__(self):
|
| 21 |
self.api_key = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def extract_frames(self, video_path, max_frames=30):
|
| 24 |
"""Extract frames from video"""
|
| 25 |
frames = []
|
| 26 |
+
if not video_path:
|
| 27 |
+
return frames
|
| 28 |
+
|
| 29 |
cap = cv2.VideoCapture(video_path)
|
| 30 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 31 |
+
sample_rate = max(1, total_frames // max_frames)
|
| 32 |
|
| 33 |
+
frame_count = 0
|
| 34 |
+
while cap.isOpened() and len(frames) < max_frames:
|
| 35 |
ret, frame = cap.read()
|
| 36 |
if not ret:
|
| 37 |
break
|
| 38 |
|
| 39 |
if frame_count % sample_rate == 0:
|
| 40 |
frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
|
|
|
| 41 |
frame_count += 1
|
| 42 |
|
| 43 |
cap.release()
|
| 44 |
return frames
|
| 45 |
|
| 46 |
+
def create_video_from_frames(self, frames, output_path, fps=24):
|
| 47 |
+
"""Create video from frames"""
|
| 48 |
if not frames:
|
| 49 |
return None
|
| 50 |
+
|
| 51 |
+
h, w = frames[0].shape[:2]
|
| 52 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 53 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (w, h))
|
| 54 |
|
| 55 |
for frame in frames:
|
| 56 |
out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
|
|
|
|
| 58 |
out.release()
|
| 59 |
return output_path
|
| 60 |
|
| 61 |
+
def add_text_overlay(self, frame, text, position=(50, 50)):
|
| 62 |
+
"""Add text overlay to frame"""
|
| 63 |
+
frame_copy = frame.copy()
|
| 64 |
+
cv2.putText(frame_copy, text, position,
|
| 65 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2,
|
| 66 |
+
cv2.LINE_AA)
|
| 67 |
+
return frame_copy
|
| 68 |
+
|
| 69 |
+
def change_background(self, video_path, bg_prompt):
|
| 70 |
+
"""Simulate background change"""
|
| 71 |
+
if not video_path:
|
| 72 |
+
return None, "Please upload a video first"
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
try:
|
| 75 |
+
# Extract frames
|
| 76 |
+
frames = self.extract_frames(video_path)
|
| 77 |
if not frames:
|
| 78 |
+
return None, "Failed to extract frames"
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
# Process frames (simulation)
|
| 81 |
+
processed_frames = []
|
| 82 |
for i, frame in enumerate(frames):
|
| 83 |
+
# Add overlay to simulate processing
|
| 84 |
+
text = f"BG: {bg_prompt[:30]}..."
|
| 85 |
+
processed = self.add_text_overlay(frame, text, (10, 30))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# Simple color tint based on prompt
|
| 88 |
+
if "beach" in bg_prompt.lower():
|
| 89 |
+
processed[:,:,2] = np.clip(processed[:,:,2] * 1.1, 0, 255)
|
| 90 |
+
elif "forest" in bg_prompt.lower():
|
| 91 |
+
processed[:,:,1] = np.clip(processed[:,:,1] * 1.2, 0, 255)
|
| 92 |
+
elif "city" in bg_prompt.lower():
|
| 93 |
+
processed = processed * 0.8
|
| 94 |
+
|
| 95 |
+
processed_frames.append(processed.astype(np.uint8))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
# Create output video
|
| 98 |
output_path = tempfile.mktemp(suffix='.mp4')
|
| 99 |
+
self.create_video_from_frames(processed_frames, output_path)
|
| 100 |
|
| 101 |
+
return output_path, f"Background changed to: {bg_prompt}"
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
+
return None, f"Error: {str(e)}"
|
| 105 |
|
| 106 |
+
def change_outfit(self, video_path, outfit_prompt):
|
| 107 |
+
"""Simulate outfit change"""
|
| 108 |
+
if not video_path:
|
| 109 |
+
return None, "Please upload a video first"
|
| 110 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
try:
|
| 112 |
+
frames = self.extract_frames(video_path)
|
| 113 |
+
if not frames:
|
| 114 |
+
return None, "Failed to extract frames"
|
| 115 |
|
| 116 |
+
processed_frames = []
|
| 117 |
+
for frame in frames:
|
| 118 |
+
text = f"Outfit: {outfit_prompt[:25]}..."
|
| 119 |
+
processed = self.add_text_overlay(frame, text, (10, 60))
|
| 120 |
+
processed_frames.append(processed)
|
| 121 |
+
|
| 122 |
+
output_path = tempfile.mktemp(suffix='.mp4')
|
| 123 |
+
self.create_video_from_frames(processed_frames, output_path)
|
|
|
|
| 124 |
|
| 125 |
+
return output_path, f"Outfit changed to: {outfit_prompt}"
|
|
|
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
+
return None, f"Error: {str(e)}"
|
| 129 |
|
| 130 |
+
def apply_style(self, video_path, style_prompt, color_prompt):
|
| 131 |
+
"""Apply style and color effects"""
|
| 132 |
+
if not video_path:
|
| 133 |
+
return None, "Please upload a video first"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
try:
|
| 136 |
+
frames = self.extract_frames(video_path)
|
| 137 |
+
if not frames:
|
| 138 |
+
return None, "Failed to extract frames"
|
| 139 |
|
| 140 |
+
processed_frames = []
|
| 141 |
+
for frame in frames:
|
| 142 |
+
# Apply color effects
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
if "warm" in color_prompt.lower():
|
| 144 |
+
frame[:,:,0] = np.clip(frame[:,:,0] * 1.2, 0, 255)
|
| 145 |
+
frame[:,:,2] = np.clip(frame[:,:,2] * 0.8, 0, 255)
|
| 146 |
elif "cool" in color_prompt.lower():
|
| 147 |
+
frame[:,:,0] = np.clip(frame[:,:,0] * 0.8, 0, 255)
|
| 148 |
+
frame[:,:,2] = np.clip(frame[:,:,2] * 1.2, 0, 255)
|
| 149 |
elif "vintage" in color_prompt.lower():
|
| 150 |
# Sepia effect
|
| 151 |
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
| 152 |
frame = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
|
| 153 |
+
frame[:,:,0] = np.clip(frame[:,:,0] * 1.1, 0, 255)
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
text = f"Style: {style_prompt[:20]}..."
|
| 156 |
+
processed = self.add_text_overlay(frame.astype(np.uint8), text, (10, 90))
|
| 157 |
+
processed_frames.append(processed)
|
| 158 |
|
| 159 |
output_path = tempfile.mktemp(suffix='.mp4')
|
| 160 |
+
self.create_video_from_frames(processed_frames, output_path)
|
| 161 |
|
| 162 |
+
return output_path, f"Style applied: {style_prompt}"
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
+
return None, f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
# Create instance
|
| 168 |
editor = VideoEditor()
|
| 169 |
|
| 170 |
+
# Create interface
|
| 171 |
+
with gr.Blocks(css=CSS) as demo:
|
| 172 |
gr.HTML("""
|
| 173 |
+
<h1 class="title">🎬 AI Video Editor</h1>
|
| 174 |
+
<p style="text-align: center;">Transform videos with AI-powered editing</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
""")
|
| 176 |
|
| 177 |
+
# Video input
|
| 178 |
with gr.Row():
|
| 179 |
+
video_input = gr.Video(label="Upload Video")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
+
# Tabs for different features
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
with gr.Tabs():
|
| 183 |
# Background Tab
|
| 184 |
+
with gr.Tab("🏞️ Background"):
|
| 185 |
+
gr.Markdown("### Change Video Background")
|
| 186 |
+
bg_prompt = gr.Textbox(
|
| 187 |
+
placeholder="Describe the new background...",
|
| 188 |
+
label="Background Description"
|
| 189 |
+
)
|
| 190 |
+
bg_button = gr.Button("Change Background", variant="primary")
|
| 191 |
+
bg_output = gr.Video(label="Result")
|
| 192 |
+
bg_status = gr.Textbox(label="Status")
|
| 193 |
+
|
| 194 |
+
gr.Examples(
|
| 195 |
+
examples=[
|
| 196 |
+
"Tropical beach with palm trees",
|
| 197 |
+
"Modern city skyline at night",
|
| 198 |
+
"Enchanted forest with fireflies"
|
| 199 |
+
],
|
| 200 |
+
inputs=bg_prompt
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
bg_button.click(
|
| 204 |
+
fn=editor.change_background,
|
| 205 |
+
inputs=[video_input, bg_prompt],
|
| 206 |
+
outputs=[bg_output, bg_status]
|
| 207 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
# Outfit Tab
|
| 210 |
+
with gr.Tab("👔 Outfit"):
|
| 211 |
+
gr.Markdown("### Change Character Outfit")
|
| 212 |
+
outfit_prompt = gr.Textbox(
|
| 213 |
+
placeholder="Describe the new outfit...",
|
| 214 |
+
label="Outfit Description"
|
| 215 |
+
)
|
| 216 |
+
outfit_button = gr.Button("Change Outfit", variant="primary")
|
| 217 |
+
outfit_output = gr.Video(label="Result")
|
| 218 |
+
outfit_status = gr.Textbox(label="Status")
|
| 219 |
+
|
| 220 |
+
gr.Examples(
|
| 221 |
+
examples=[
|
| 222 |
+
"Business suit with red tie",
|
| 223 |
+
"Casual summer dress",
|
| 224 |
+
"Superhero costume"
|
| 225 |
+
],
|
| 226 |
+
inputs=outfit_prompt
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
outfit_button.click(
|
| 230 |
+
fn=editor.change_outfit,
|
| 231 |
+
inputs=[video_input, outfit_prompt],
|
| 232 |
+
outputs=[outfit_output, outfit_status]
|
| 233 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
+
# Style Tab
|
| 236 |
+
with gr.Tab("🎨 Style"):
|
| 237 |
+
gr.Markdown("### Apply Visual Style")
|
| 238 |
+
style_prompt = gr.Textbox(
|
| 239 |
+
placeholder="Visual style...",
|
| 240 |
+
label="Style"
|
| 241 |
+
)
|
| 242 |
+
color_prompt = gr.Textbox(
|
| 243 |
+
placeholder="Color grading...",
|
| 244 |
+
label="Colors"
|
| 245 |
+
)
|
| 246 |
+
style_button = gr.Button("Apply Style", variant="primary")
|
| 247 |
+
style_output = gr.Video(label="Result")
|
| 248 |
+
style_status = gr.Textbox(label="Status")
|
| 249 |
+
|
| 250 |
+
gr.Examples(
|
| 251 |
+
examples=[
|
| 252 |
+
["Cinematic", "Warm sunset tones"],
|
| 253 |
+
["Anime style", "Vibrant colors"],
|
| 254 |
+
["Film noir", "Cool blue tones"]
|
| 255 |
+
],
|
| 256 |
+
inputs=[style_prompt, color_prompt]
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
style_button.click(
|
| 260 |
+
fn=editor.apply_style,
|
| 261 |
+
inputs=[video_input, style_prompt, color_prompt],
|
| 262 |
+
outputs=[style_output, style_status]
|
| 263 |
+
)
|
| 264 |
|
|
|
|
| 265 |
gr.Markdown("""
|
| 266 |
---
|
| 267 |
+
### 📝 Notes:
|
| 268 |
+
- This is a demonstration with simulated effects
|
| 269 |
+
- Real implementation requires AI model APIs
|
| 270 |
+
- Upload short videos (<30s) for best results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
# Launch
|
| 274 |
+
demo.launch()
|
|
|