oviet711 commited on
Commit
ebe0347
·
verified ·
1 Parent(s): a3186c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -49
app.py CHANGED
@@ -2,8 +2,6 @@ import gradio as gr
2
  import subprocess
3
  import json
4
  import os
5
- import asyncio
6
- import edge_tts
7
  import requests
8
  import urllib.parse
9
  import spaces
@@ -13,23 +11,11 @@ HF_TOKEN = os.getenv("HF_WRITE_TOKEN")
13
  api = HfApi(token=HF_TOKEN)
14
  DATASET_ID = "oviet711/render-factory"
15
 
16
- async def generate_audio(text, chunk_id):
17
- filename = f"audio_{chunk_id}.mp3"
18
- # Menggunakan suara pria Indonesia yang lebih natural
19
- communicate = edge_tts.Communicate(text, "id-ID-ArdiNeural")
20
- await communicate.save(filename)
21
- return filename
22
-
23
  def generate_ai_image(keyword, chunk_id):
24
- print(f"🎨 Menarik gambar dari server eksternal untuk keyword: {keyword}")
25
  try:
26
- # Prompt visual yang memaksa gambar estetik dan gelap
27
- prompt = f"cinematic photography, ultra realistic, highly detailed, {keyword}, dark moody lighting, 8k resolution"
28
- encoded_prompt = urllib.parse.quote(prompt)
29
-
30
- # Trik bypass menggunakan Pollinations AI (Tanpa API Key, gambar pasti jadi)
31
  url = f"https://image.pollinations.ai/prompt/{encoded_prompt}?width=1080&height=1920&nologo=true"
32
-
33
  response = requests.get(url, stream=True)
34
  if response.status_code == 200:
35
  bg_filename = f"bg_{chunk_id}.png"
@@ -37,8 +23,7 @@ def generate_ai_image(keyword, chunk_id):
37
  for chunk in response.iter_content(1024):
38
  f.write(chunk)
39
  return bg_filename
40
- except Exception as e:
41
- print(f"⚠️ Gagal menarik gambar: {e}")
42
  return None
43
 
44
  @spaces.GPU
@@ -47,56 +32,38 @@ def render_worker(json_data):
47
  data = json.loads(json_data)
48
  chunk_id = data.get('chunk_id', '000')
49
  text = data.get('text', 'Teks kosong')
50
- keyword = data.get('keyword', 'technology')
51
-
52
- print(f"⚙️ Memproses Chunk {chunk_id}: {text}")
53
 
54
- # 1. Generate Audio
55
- asyncio.run(generate_audio(text, chunk_id))
 
 
56
 
57
- # 2. Tarik Visual 4K
58
  bg_image = generate_ai_image(keyword, chunk_id)
59
-
60
  output_filename = f"chunk_{chunk_id}.mp4"
61
- status_visual = "Layar Hitam (Fallback)"
62
 
63
- # 3. Render Video
64
  if bg_image:
65
- status_visual = "Gambar AI 4K"
66
  cmd = [
67
  "ffmpeg", "-y",
68
  "-loop", "1", "-framerate", "25",
69
  "-i", bg_image,
70
- "-i", f"audio_{chunk_id}.mp3",
71
- "-vf", "scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920",
72
  "-c:v", "libx264", "-c:a", "aac",
73
- "-shortest",
74
- "-pix_fmt", "yuv420p",
75
- "-movflags", "frag_keyframe+empty_moov",
76
- output_filename
77
  ]
78
- else:
79
- cmd = [
80
- "ffmpeg", "-y",
81
- "-f", "lavfi", "-i", "color=c=black:s=1080x1920:d=30",
82
- "-i", f"audio_{chunk_id}.mp3",
83
- "-c:v", "libx264", "-c:a", "aac",
84
- "-shortest",
85
- "-movflags", "frag_keyframe+empty_moov",
86
- output_filename
87
- ]
88
-
89
- subprocess.run(cmd, check=True)
90
 
91
- # 4. Upload
92
  api.upload_file(
93
  path_or_fileobj=output_filename,
94
  path_in_repo=f"chunks/{output_filename}",
95
  repo_id=DATASET_ID,
96
  repo_type="dataset"
97
  )
98
-
99
- return f"✅ SUKSES: {output_filename} (Keyword: {keyword}) dirender dengan {status_visual}."
100
  except Exception as e:
101
  return f"❌ ERROR: {str(e)}"
102
 
 
2
  import subprocess
3
  import json
4
  import os
 
 
5
  import requests
6
  import urllib.parse
7
  import spaces
 
11
  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"
 
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
 
32
  data = json.loads(json_data)
33
  chunk_id = data.get('chunk_id', '000')
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
  if bg_image:
 
48
  cmd = [
49
  "ffmpeg", "-y",
50
  "-loop", "1", "-framerate", "25",
51
  "-i", bg_image,
52
+ "-i", audio_file,
53
+ "-vf", f"scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,zoompan=z='min(zoom+0.001,1.1)':d=500,subtitles={vtt_file}:force_style='FontSize=24,PrimaryColour=&H00FFFF,Alignment=2,MarginV=150'",
54
  "-c:v", "libx264", "-c:a", "aac",
55
+ "-shortest", "-pix_fmt", "yuv420p", output_filename
 
 
 
56
  ]
57
+ subprocess.run(cmd, check=True)
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # 4. Upload ke HF
60
  api.upload_file(
61
  path_or_fileobj=output_filename,
62
  path_in_repo=f"chunks/{output_filename}",
63
  repo_id=DATASET_ID,
64
  repo_type="dataset"
65
  )
66
+ return f"✅ SUKSES Chunk {chunk_id}"
 
67
  except Exception as e:
68
  return f"❌ ERROR: {str(e)}"
69