ginipick commited on
Commit
9f85f32
·
verified ·
1 Parent(s): 3558e0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -8
app.py CHANGED
@@ -8,17 +8,17 @@ import os
8
  import gradio as gr
9
  import replicate
10
  import base64
11
- from PIL import Image
12
  import io
13
  import requests
14
  from datetime import datetime
15
  import tempfile
16
  import time
 
 
17
 
18
  # API token setup
19
  api_token = os.getenv("RAPI_TOKEN")
20
- if api_token:
21
- os.environ["REPLICATE_API_TOKEN"] = api_token
22
 
23
  # Aspect ratio options
24
  ASPECT_RATIOS = {
@@ -35,6 +35,64 @@ ASPECT_RATIOS = {
35
  DEFAULT_TEXT_PROMPT = ""
36
  DEFAULT_IMAGE_PROMPT = "Generate a video with smooth and natural movement. Objects should have visible motion while maintaining fluid transitions."
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  def update_prompt_placeholder(mode):
39
  """Update prompt placeholder based on mode"""
40
  if mode == "Text to Video":
@@ -203,11 +261,31 @@ def generate_video(mode, prompt, image, aspect_ratio, seed, api_key_input, progr
203
  # Save to temporary file
204
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
205
  tmp_file.write(video_data)
206
- video_path = tmp_file.name
 
 
 
 
 
 
 
 
207
 
208
  # Also save as output.mp4
209
- with open("output.mp4", "wb") as file:
210
- file.write(video_data)
 
 
 
 
 
 
 
 
 
 
 
 
211
 
212
  progress(1.0, desc="Complete!")
213
 
@@ -220,9 +298,10 @@ def generate_video(mode, prompt, image, aspect_ratio, seed, api_key_input, progr
220
  - Seed: {seed}
221
  - Duration: 5 seconds
222
  - Resolution: 480p
 
223
  - File: output.mp4"""
224
 
225
- return video_path, info
226
 
227
  except requests.exceptions.Timeout:
228
  return None, "⏱️ Request timed out. The server might be under heavy load. Please try again in a few minutes."
@@ -301,6 +380,7 @@ with gr.Blocks(title="Bytedance Seedance Video Free", theme=gr.themes.Soft()) as
301
  ### 📋 Fixed Settings
302
  - **Duration**: 5 seconds
303
  - **Resolution**: 480p
 
304
  """)
305
 
306
  with gr.Column(scale=2):
@@ -334,7 +414,7 @@ with gr.Blocks(title="Bytedance Seedance Video Free", theme=gr.themes.Soft()) as
334
 
335
  1. **Install required packages**:
336
  ```bash
337
- pip install gradio replicate pillow requests
338
  ```
339
 
340
  2. **Set environment variable** (optional):
@@ -353,6 +433,7 @@ with gr.Blocks(title="Bytedance Seedance Video Free", theme=gr.themes.Soft()) as
353
  - **Image to Video**: Transform uploaded image into animated video
354
  - **Aspect Ratios**: Choose ratios optimized for various social media platforms
355
  - **Seed Value**: Use same seed to reproduce identical results
 
356
 
357
  ### Prompt Writing Tips
358
 
@@ -366,6 +447,7 @@ with gr.Blocks(title="Bytedance Seedance Video Free", theme=gr.themes.Soft()) as
366
  - **Timeout errors**: The model might be cold starting. Wait 1-2 minutes and try again.
367
  - **Model booting**: First requests after inactivity may take longer as the model boots up.
368
  - **Extended wait times**: Complex prompts or server load may cause longer generation times.
 
369
  """)
370
 
371
  # Examples
 
8
  import gradio as gr
9
  import replicate
10
  import base64
11
+ from PIL import Image, ImageDraw, ImageFont
12
  import io
13
  import requests
14
  from datetime import datetime
15
  import tempfile
16
  import time
17
+ from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
18
+ import numpy as np
19
 
20
  # API token setup
21
  api_token = os.getenv("RAPI_TOKEN")
 
 
22
 
23
  # Aspect ratio options
24
  ASPECT_RATIOS = {
 
35
  DEFAULT_TEXT_PROMPT = ""
36
  DEFAULT_IMAGE_PROMPT = "Generate a video with smooth and natural movement. Objects should have visible motion while maintaining fluid transitions."
37
 
38
+ def add_watermark(input_video_path, output_video_path):
39
+ """Add watermark to video using moviepy"""
40
+ try:
41
+ # Load the video
42
+ video = VideoFileClip(input_video_path)
43
+
44
+ # Get video dimensions
45
+ width, height = video.size
46
+
47
+ # Calculate watermark size and position
48
+ watermark_text = "ginigen.com"
49
+ font_size = max(12, int(height * 0.025)) # 2.5% of video height, minimum 12px
50
+
51
+ # Create text clip for watermark
52
+ watermark = TextClip(
53
+ watermark_text,
54
+ fontsize=font_size,
55
+ color='white',
56
+ font='Arial',
57
+ stroke_color='black',
58
+ stroke_width=1,
59
+ method='caption',
60
+ size=(None, None)
61
+ )
62
+
63
+ # Position watermark at bottom right with padding
64
+ padding = int(width * 0.02) # 2% padding
65
+ watermark = watermark.set_position((width - watermark.w - padding, height - watermark.h - padding))
66
+ watermark = watermark.set_duration(video.duration)
67
+
68
+ # Set opacity (semi-transparent)
69
+ watermark = watermark.set_opacity(0.7)
70
+
71
+ # Composite the watermark onto the video
72
+ final_video = CompositeVideoClip([video, watermark])
73
+
74
+ # Write the output video
75
+ final_video.write_videofile(
76
+ output_video_path,
77
+ codec='libx264',
78
+ audio_codec='aac',
79
+ temp_audiofile=tempfile.mktemp('.m4a'),
80
+ remove_temp=True,
81
+ logger=None # Suppress moviepy output
82
+ )
83
+
84
+ # Clean up
85
+ video.close()
86
+ final_video.close()
87
+
88
+ return True
89
+ except Exception as e:
90
+ print(f"Watermark error: {str(e)}")
91
+ # If watermarking fails, just copy the original
92
+ import shutil
93
+ shutil.copy2(input_video_path, output_video_path)
94
+ return False
95
+
96
  def update_prompt_placeholder(mode):
97
  """Update prompt placeholder based on mode"""
98
  if mode == "Text to Video":
 
261
  # Save to temporary file
262
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
263
  tmp_file.write(video_data)
264
+ temp_video_path = tmp_file.name
265
+
266
+ progress(0.8, desc="Adding watermark...")
267
+
268
+ # Create final output path
269
+ final_video_path = tempfile.mktemp(suffix='.mp4')
270
+
271
+ # Add watermark
272
+ watermark_added = add_watermark(temp_video_path, final_video_path)
273
 
274
  # Also save as output.mp4
275
+ if os.path.exists(final_video_path):
276
+ with open(final_video_path, "rb") as f:
277
+ watermarked_data = f.read()
278
+ with open("output.mp4", "wb") as file:
279
+ file.write(watermarked_data)
280
+ else:
281
+ # Fallback if watermarking failed
282
+ with open("output.mp4", "wb") as file:
283
+ file.write(video_data)
284
+ final_video_path = temp_video_path
285
+
286
+ # Clean up temp file if different
287
+ if temp_video_path != final_video_path and os.path.exists(temp_video_path):
288
+ os.unlink(temp_video_path)
289
 
290
  progress(1.0, desc="Complete!")
291
 
 
298
  - Seed: {seed}
299
  - Duration: 5 seconds
300
  - Resolution: 480p
301
+ - Watermark: {"Added" if watermark_added else "Failed (original saved)"}
302
  - File: output.mp4"""
303
 
304
+ return final_video_path, info
305
 
306
  except requests.exceptions.Timeout:
307
  return None, "⏱️ Request timed out. The server might be under heavy load. Please try again in a few minutes."
 
380
  ### 📋 Fixed Settings
381
  - **Duration**: 5 seconds
382
  - **Resolution**: 480p
383
+ - **Watermark**: ginigen.com
384
  """)
385
 
386
  with gr.Column(scale=2):
 
414
 
415
  1. **Install required packages**:
416
  ```bash
417
+ pip install gradio replicate pillow requests moviepy
418
  ```
419
 
420
  2. **Set environment variable** (optional):
 
433
  - **Image to Video**: Transform uploaded image into animated video
434
  - **Aspect Ratios**: Choose ratios optimized for various social media platforms
435
  - **Seed Value**: Use same seed to reproduce identical results
436
+ - **Watermark**: Automatically adds "ginigen.com" watermark to generated videos
437
 
438
  ### Prompt Writing Tips
439
 
 
447
  - **Timeout errors**: The model might be cold starting. Wait 1-2 minutes and try again.
448
  - **Model booting**: First requests after inactivity may take longer as the model boots up.
449
  - **Extended wait times**: Complex prompts or server load may cause longer generation times.
450
+ - **Watermark issues**: If watermark fails, the original video will be saved without watermark.
451
  """)
452
 
453
  # Examples