jumannendange commited on
Commit
4dd606b
Β·
verified Β·
1 Parent(s): 537ab38

Upload 2 files

Browse files
Files changed (1) hide show
  1. video_ad.py +123 -32
video_ad.py CHANGED
@@ -1,9 +1,12 @@
1
  """
2
- video_ad.py β€” Product Ad Video Generator (Wan2.2-TI2V-5B kwenye ZeroGPU)
 
 
3
  Inaitwa kutoka app.py kwa video_ad.render(), kama exe_builder.render().
4
  """
5
- import os, shutil, subprocess
6
  import gradio as gr
 
7
 
8
  try:
9
  import spaces
@@ -11,10 +14,14 @@ except Exception:
11
  spaces = None
12
 
13
  MODEL = "Wan-AI/Wan2.2-TI2V-5B-Diffusers"
14
- STYLE = ("commercial product advertisement, smooth cinematic camera motion, "
15
- "studio lighting, sharp focus, premium look. ")
16
- NEG = "blurry, distorted, warped text, extra objects, low quality, static, watermark"
 
 
 
17
  FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
 
18
 
19
  # ── Pakia model wakati wa kuanza (ZeroGPU inaruhusu .to("cuda") hapa).
20
  # Ikishindwa, builder ya APK/EXE inaendelea kufanya kazi kama kawaida.
@@ -29,24 +36,28 @@ try:
29
  except Exception as e:
30
  _load_err = str(e)[:300]
31
 
32
-
33
  def _duration(image, prompt, secs, steps):
34
- return int(min(120, 20 + float(steps) * float(secs) * 1.2))
35
 
36
 
37
  def _gpu(fn):
38
  return spaces.GPU(duration=_duration)(fn) if spaces else fn
39
 
40
 
 
 
 
 
 
 
41
  @_gpu
42
- def _generate_clip(image, prompt, secs, steps):
43
- w, h = 832, 480
44
- image = image.convert("RGB").resize((w, h))
45
  frames = int(float(secs) * 24) // 4 * 4 + 1
46
  out = _pipe(image=image, prompt=STYLE + prompt, negative_prompt=NEG,
47
- height=h, width=w, num_frames=frames,
48
  guidance_scale=5.0, num_inference_steps=int(steps)).frames[0]
49
- path = "/tmp/ad_clip.mp4"
50
  export_to_video(out, path, fps=24)
51
  return path
52
 
@@ -62,52 +73,132 @@ def _ffmpeg_bin():
62
  return None
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def _overlay(src, text):
66
  """Ongeza maandishi (bei/namba) chini ya video. Ikishindwa, rudisha video ya awali."""
67
  text = (text or "").strip()
68
  ff = _ffmpeg_bin()
69
  if not text or not ff:
70
  return src
71
- dst = "/tmp/ad_final.mp4"
72
  esc = (text.replace("\\", "\\\\").replace(":", "\\:")
73
  .replace("'", "\u2019").replace("%", "\\%"))
74
  vf = (f"drawtext=fontfile={FONT}:text='{esc}':fontcolor=white:fontsize=h/14:"
75
  f"box=1:boxcolor=black@0.55:boxborderw=14:x=(w-text_w)/2:y=h-text_h-h/12")
76
  try:
77
  r = subprocess.run([ff, "-y", "-i", src, "-vf", vf, "-pix_fmt", "yuv420p", dst],
78
- capture_output=True, text=True, timeout=120)
79
  return dst if r.returncode == 0 and os.path.exists(dst) else src
80
  except Exception:
81
  return src
82
 
83
 
84
- def _run(image, prompt, caption, secs, steps):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  if _pipe is None:
86
- return None, f"❌ Model haikupakiwa: {_load_err or 'hakuna GPU'}"
87
- if image is None:
88
- return None, "⚠️ Pakia picha ya bidhaa kwanza."
89
- try:
90
- clip = _generate_clip(image, prompt or "slow rotating camera", secs, steps)
91
- final = _overlay(clip, caption)
92
- note = "βœ… Tayari." + (" (maandishi yameongezwa)" if final != clip else "")
93
- return final, note
94
- except Exception as e:
95
- return None, f"❌ {str(e)[:400]}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
 
98
  def render():
99
  gr.Markdown("---\n## 🎬 VIDEO AD GENERATOR\n"
100
- "_Picha ya bidhaa β†’ video fupi ya kibiashara (Wan2.2 kwenye ZeroGPU)._")
 
101
  with gr.Row():
102
  with gr.Column(scale=1, min_width=340):
103
- v_img = gr.Image(type="pil", label="Picha ya bidhaa")
104
- v_prompt = gr.Textbox(label="Maelezo ya mwendo (Kiingereza)",
105
- value="slow rotating camera, soft light reflections")
 
 
 
 
106
  v_caption = gr.Textbox(label="Maandishi chini ya video (bei / namba ya simu) β€” hiari")
107
- v_secs = gr.Slider(2, 5, value=3, step=1, label="Sekunde")
108
  v_steps = gr.Slider(15, 40, value=25, step=5, label="Steps")
109
  v_btn = gr.Button("🎬 TENGENEZA VIDEO", variant="primary")
110
  with gr.Column(scale=2):
111
  v_out = gr.Video(label="Video")
112
- v_status = gr.Textbox(label="Hali", lines=2)
113
- v_btn.click(_run, [v_img, v_prompt, v_caption, v_secs, v_steps], [v_out, v_status])
 
1
  """
2
+ video_ad.py β€” Product Ad Video Generator (ZeroGPU)
3
+ - Picha nyingi (gallery) β†’ clip kwa kila picha β†’ zinaunganishwa kuwa video moja.
4
+ - Wewe unaandika prompt: mstari mmoja = kwa picha zote; mistari mingi = mstari wa N kwa picha ya N.
5
  Inaitwa kutoka app.py kwa video_ad.render(), kama exe_builder.render().
6
  """
7
+ import os, re, shutil, subprocess, tempfile, uuid
8
  import gradio as gr
9
+ from PIL import Image, ImageOps
10
 
11
  try:
12
  import spaces
 
14
  spaces = None
15
 
16
  MODEL = "Wan-AI/Wan2.2-TI2V-5B-Diffusers"
17
+ MAX_IMAGES = 10 # kinga ya quota ya ZeroGPU; ongeza ukitaka
18
+ STYLE = ("commercial product advertisement, the product stays in frame and keeps its exact "
19
+ "shape, smooth cinematic motion, studio lighting, sharp focus, premium look. ")
20
+ NEG = ("camera, photographer, hands, person, new objects, extra objects, morphing, "
21
+ "warped text, distorted logo, blurry, low quality, watermark")
22
+ DEFAULT_PROMPT = "the product slowly rotates, soft light reflections sweep across its surface"
23
  FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
24
+ W, H = 832, 480
25
 
26
  # ── Pakia model wakati wa kuanza (ZeroGPU inaruhusu .to("cuda") hapa).
27
  # Ikishindwa, builder ya APK/EXE inaendelea kufanya kazi kama kawaida.
 
36
  except Exception as e:
37
  _load_err = str(e)[:300]
38
 
 
39
  def _duration(image, prompt, secs, steps):
40
+ return int(min(120, 30 + float(steps) * float(secs) * 1.2))
41
 
42
 
43
  def _gpu(fn):
44
  return spaces.GPU(duration=_duration)(fn) if spaces else fn
45
 
46
 
47
+ def _fit(image):
48
+ """Weka picha kwenye 832x480 bila kuinyoosha; pembeni zinajazwa kwa rangi ya kona."""
49
+ image = image.convert("RGB")
50
+ return ImageOps.pad(image, (W, H), color=image.getpixel((0, 0)), method=Image.LANCZOS)
51
+
52
+
53
  @_gpu
54
+ def _make_clip(image, prompt, secs, steps):
55
+ image = _fit(image)
 
56
  frames = int(float(secs) * 24) // 4 * 4 + 1
57
  out = _pipe(image=image, prompt=STYLE + prompt, negative_prompt=NEG,
58
+ height=H, width=W, num_frames=frames,
59
  guidance_scale=5.0, num_inference_steps=int(steps)).frames[0]
60
+ path = f"/tmp/ad_clip_{uuid.uuid4().hex[:8]}.mp4"
61
  export_to_video(out, path, fps=24)
62
  return path
63
 
 
73
  return None
74
 
75
 
76
+ def _concat(clips):
77
+ """Unganisha clips zote kuwa video moja. Ikishindwa, rudisha clip ya kwanza."""
78
+ if len(clips) == 1:
79
+ return clips[0]
80
+ ff = _ffmpeg_bin()
81
+ if not ff:
82
+ return clips[0]
83
+ lst = tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False)
84
+ for c in clips:
85
+ lst.write(f"file '{c}'\n")
86
+ lst.close()
87
+ dst = f"/tmp/ad_joined_{uuid.uuid4().hex[:8]}.mp4"
88
+ try:
89
+ r = subprocess.run([ff, "-y", "-f", "concat", "-safe", "0", "-i", lst.name,
90
+ "-c:v", "libx264", "-pix_fmt", "yuv420p", dst],
91
+ capture_output=True, text=True, timeout=300)
92
+ return dst if r.returncode == 0 and os.path.exists(dst) else clips[0]
93
+ except Exception:
94
+ return clips[0]
95
+
96
+
97
  def _overlay(src, text):
98
  """Ongeza maandishi (bei/namba) chini ya video. Ikishindwa, rudisha video ya awali."""
99
  text = (text or "").strip()
100
  ff = _ffmpeg_bin()
101
  if not text or not ff:
102
  return src
103
+ dst = f"/tmp/ad_final_{uuid.uuid4().hex[:8]}.mp4"
104
  esc = (text.replace("\\", "\\\\").replace(":", "\\:")
105
  .replace("'", "\u2019").replace("%", "\\%"))
106
  vf = (f"drawtext=fontfile={FONT}:text='{esc}':fontcolor=white:fontsize=h/14:"
107
  f"box=1:boxcolor=black@0.55:boxborderw=14:x=(w-text_w)/2:y=h-text_h-h/12")
108
  try:
109
  r = subprocess.run([ff, "-y", "-i", src, "-vf", vf, "-pix_fmt", "yuv420p", dst],
110
+ capture_output=True, text=True, timeout=300)
111
  return dst if r.returncode == 0 and os.path.exists(dst) else src
112
  except Exception:
113
  return src
114
 
115
 
116
+ def _to_pils(gallery):
117
+ out = []
118
+ for it in gallery or []:
119
+ x = it[0] if isinstance(it, (tuple, list)) else it
120
+ try:
121
+ if isinstance(x, str):
122
+ x = Image.open(x)
123
+ elif not isinstance(x, Image.Image) and hasattr(x, "name"):
124
+ x = Image.open(x.name)
125
+ if isinstance(x, Image.Image):
126
+ out.append(x)
127
+ except Exception:
128
+ pass
129
+ return out
130
+
131
+
132
+ def _clean_prompt(p):
133
+ # neno 'camera' kwenye prompt huchora kamera halisi; badilisha na 'view'
134
+ p = re.sub(r"\bcamera\b", "view", p.strip(), flags=re.I)
135
+ return p or DEFAULT_PROMPT
136
+
137
+
138
+ def _prompts_for(n, text):
139
+ """Mstari mmoja = kwa picha zote. Mistari mingi = mstari wa N kwa picha ya N (ya mwisho inajirudia)."""
140
+ lines = [_clean_prompt(l) for l in (text or "").splitlines() if l.strip()]
141
+ if not lines:
142
+ lines = [DEFAULT_PROMPT]
143
+ return [lines[min(i, len(lines) - 1)] for i in range(n)]
144
+
145
+
146
+ def _run(gallery, manual, caption, secs, steps):
147
  if _pipe is None:
148
+ yield None, f"❌ Model haikupakiwa: {_load_err or 'hakuna GPU'}"
149
+ return
150
+ imgs = _to_pils(gallery)
151
+ if not imgs:
152
+ yield None, "⚠️ Pakia picha moja au zaidi kwanza."
153
+ return
154
+ note = ""
155
+ if len(imgs) > MAX_IMAGES:
156
+ note = f"ℹ️ Picha {len(imgs)} zimepakiwa; nitatumia {MAX_IMAGES} za kwanza.\n"
157
+ imgs = imgs[:MAX_IMAGES]
158
+ if not (manual or "").strip():
159
+ note += "ℹ️ Hukuandika prompt; natumia prompt ya kawaida.\n"
160
+ prompts = _prompts_for(len(imgs), manual)
161
+
162
+ log, clips = [], []
163
+ for i, img in enumerate(imgs, 1):
164
+ log.append(f"⏳ Picha {i}/{len(imgs)} inatengenezwa...")
165
+ yield None, note + "\n".join(log)
166
+ try:
167
+ path = _make_clip(img, prompts[i - 1], secs, steps)
168
+ clips.append(path)
169
+ log[-1] = f"βœ… Picha {i}: {prompts[i - 1]}"
170
+ except Exception as e:
171
+ log[-1] = f"❌ Picha {i}: {str(e)[:200]}"
172
+ log.append("β†ͺ Nimesimama; naunganisha zilizokamilika.")
173
+ break
174
+ yield None, note + "\n".join(log)
175
+
176
+ if not clips:
177
+ yield None, note + "\n".join(log)
178
+ return
179
+ final = _overlay(_concat(clips), caption)
180
+ log.append(f"🎬 Tayari: clips {len(clips)}.")
181
+ yield final, note + "\n".join(log)
182
 
183
 
184
  def render():
185
  gr.Markdown("---\n## 🎬 VIDEO AD GENERATOR\n"
186
+ "_Pakia picha nyingi za bidhaa na uandike prompt yako. "
187
+ "Clips zinaunganishwa kuwa video moja ya kibiashara._")
188
  with gr.Row():
189
  with gr.Column(scale=1, min_width=340):
190
+ v_imgs = gr.Gallery(label=f"Picha za bidhaa (hadi {MAX_IMAGES})", type="pil",
191
+ columns=3, height=260, interactive=True)
192
+ v_manual = gr.Textbox(
193
+ label="Prompt yako (Kiingereza). Mstari 1 = picha zote; mistari mingi = mstari wa N kwa picha ya N",
194
+ lines=4,
195
+ placeholder="the bottle slowly rotates, soft light reflections sweep across it\n"
196
+ "the shoe floats upward, gentle sparkles around it")
197
  v_caption = gr.Textbox(label="Maandishi chini ya video (bei / namba ya simu) β€” hiari")
198
+ v_secs = gr.Slider(2, 5, value=3, step=1, label="Sekunde kwa kila picha")
199
  v_steps = gr.Slider(15, 40, value=25, step=5, label="Steps")
200
  v_btn = gr.Button("🎬 TENGENEZA VIDEO", variant="primary")
201
  with gr.Column(scale=2):
202
  v_out = gr.Video(label="Video")
203
+ v_status = gr.Textbox(label="Hali na prompts zilizotumika", lines=8)
204
+ v_btn.click(_run, [v_imgs, v_manual, v_caption, v_secs, v_steps], [v_out, v_status])