oviet711 commited on
Commit
5740d5f
·
verified ·
1 Parent(s): 3baacff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -20
app.py CHANGED
@@ -4,6 +4,7 @@ import json
4
  import os
5
  import requests
6
  import urllib.parse
 
7
  import spaces
8
  from huggingface_hub import HfApi
9
 
@@ -12,18 +13,24 @@ api = HfApi(token=HF_TOKEN)
12
  DATASET_ID = "oviet711/render-factory"
13
 
14
  def generate_ai_image(keyword, chunk_id):
15
- print(f"🎨 Visual: {keyword}")
16
- try:
17
- encoded_prompt = urllib.parse.quote(f"cinematic photography, ultra realistic, highly detailed, {keyword}, dark moody lighting, 8k")
18
- url = f"https://image.pollinations.ai/prompt/{encoded_prompt}?width=1080&height=1920&nologo=true"
19
- response = requests.get(url, stream=True)
20
- if response.status_code == 200:
21
- bg_filename = f"bg_{chunk_id}.png"
22
- with open(bg_filename, 'wb') as f:
23
- for chunk in response.iter_content(1024):
24
- f.write(chunk)
25
- return bg_filename
26
- except Exception: pass
 
 
 
 
 
 
27
  return None
28
 
29
  @spaces.GPU
@@ -34,29 +41,46 @@ def render_worker(json_data):
34
  text = data.get('text', 'Teks kosong')
35
  keyword = data.get('keyword', 'fresh juice')
36
 
37
- # 1. Generate Audio & Subtitle VTT langsung pakai Edge-TTS CLI
38
  audio_file = f"audio_{chunk_id}.mp3"
39
  vtt_file = f"sub_{chunk_id}.vtt"
40
- subprocess.run(["edge-tts", "--voice", "id-ID-ArdiNeural", "--text", text, "--write-media", audio_file, "--write-subtitles", vtt_file], check=True)
 
 
 
 
 
 
 
41
 
42
  # 2. Tarik Gambar
43
  bg_image = generate_ai_image(keyword, chunk_id)
44
  output_filename = f"chunk_{chunk_id}.mp4"
45
 
46
- # 3. Render (Efek Zoom + Bakar Subtitle Kuning Besar di tengah)
47
- # 3. Render (Efek Zoom + Bakar Subtitle Kuning Besar di tengah)
48
  if bg_image:
 
49
  cmd = [
50
  "ffmpeg", "-y",
51
- "-loop", "1", "-framerate", "30", # Menggunakan 30fps standar medsos
52
  "-i", bg_image,
53
  "-i", audio_file,
54
- # PERBAIKAN: Menambahkan huruf 'f' dan menggunakan variabel {vtt_file}
55
- "-vf", f"scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,setsar=1:1,zoompan=z='min(zoom+0.001,1.1)':d=500,subtitles={vtt_file}:force_style='FontSize=26,PrimaryColour=&H00FFFF,Alignment=2,MarginV=180'",
56
  "-c:v", "libx264", "-c:a", "aac",
57
  "-shortest", "-pix_fmt", "yuv420p", output_filename
58
  ]
59
- subprocess.run(cmd, check=True)
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  # 4. Upload ke HF
62
  api.upload_file(
 
4
  import os
5
  import requests
6
  import urllib.parse
7
+ import time
8
  import spaces
9
  from huggingface_hub import HfApi
10
 
 
13
  DATASET_ID = "oviet711/render-factory"
14
 
15
  def generate_ai_image(keyword, chunk_id):
16
+ print(f"🎨 Meminta Visual: {keyword}")
17
+ encoded_prompt = urllib.parse.quote(f"cinematic photography, ultra realistic, highly detailed, {keyword}, dark moody lighting, 8k")
18
+ url = f"https://image.pollinations.ai/prompt/{encoded_prompt}?width=1080&height=1920&nologo=true"
19
+
20
+ # SOLUSI ROBUST: Sistem "Ngotot". Coba 3 kali sebelum menyerah.
21
+ for attempt in range(3):
22
+ try:
23
+ response = requests.get(url, stream=True, timeout=15)
24
+ if response.status_code == 200:
25
+ bg_filename = f"bg_{chunk_id}.png"
26
+ with open(bg_filename, 'wb') as f:
27
+ for chunk in response.iter_content(1024):
28
+ f.write(chunk)
29
+ return bg_filename
30
+ except Exception as e:
31
+ print(f"⚠️ Gagal ambil gambar (Percobaan {attempt+1}/3): {e}")
32
+ time.sleep(2) # Istirahat 2 detik sebelum coba lagi
33
+
34
  return None
35
 
36
  @spaces.GPU
 
41
  text = data.get('text', 'Teks kosong')
42
  keyword = data.get('keyword', 'fresh juice')
43
 
44
+ # 1. UPGRADE AUDIO: Suara Wanita (Gadis) + Dipercepat 15% agar lebih natural/energik
45
  audio_file = f"audio_{chunk_id}.mp3"
46
  vtt_file = f"sub_{chunk_id}.vtt"
47
+ subprocess.run([
48
+ "edge-tts",
49
+ "--voice", "id-ID-GadisNeural",
50
+ "--rate", "+15%",
51
+ "--text", text,
52
+ "--write-media", audio_file,
53
+ "--write-subtitles", vtt_file
54
+ ], check=True)
55
 
56
  # 2. Tarik Gambar
57
  bg_image = generate_ai_image(keyword, chunk_id)
58
  output_filename = f"chunk_{chunk_id}.mp4"
59
 
60
+ # 3. Render
 
61
  if bg_image:
62
+ # FIX LANDSCAPE: Menambahkan s=1080x1920 ke dalam zoompan
63
  cmd = [
64
  "ffmpeg", "-y",
65
+ "-loop", "1", "-framerate", "30",
66
  "-i", bg_image,
67
  "-i", audio_file,
68
+ "-vf", f"scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,setsar=1:1,zoompan=z='min(zoom+0.001,1.1)':d=500:s=1080x1920,subtitles={vtt_file}:force_style='FontSize=26,PrimaryColour=&H00FFFF,Alignment=2,MarginV=180'",
 
69
  "-c:v", "libx264", "-c:a", "aac",
70
  "-shortest", "-pix_fmt", "yuv420p", output_filename
71
  ]
72
+ else:
73
+ # Fallback jika 3x percobaan gambar tetap gagal
74
+ cmd = [
75
+ "ffmpeg", "-y",
76
+ "-f", "lavfi", "-i", "color=c=black:s=1080x1920:d=30",
77
+ "-i", audio_file,
78
+ "-vf", f"subtitles={vtt_file}:force_style='FontSize=26,PrimaryColour=&H00FFFF,Alignment=2,MarginV=180'",
79
+ "-c:v", "libx264", "-c:a", "aac",
80
+ "-shortest", output_filename
81
+ ]
82
+
83
+ subprocess.run(cmd, check=True)
84
 
85
  # 4. Upload ke HF
86
  api.upload_file(