Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,66 @@
|
|
|
|
|
| 1 |
import sys
|
| 2 |
import time
|
|
|
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
# Create a new video
|
| 9 |
-
# --------------------------------------------------------------
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
progress = getattr(video, "progress", 0)
|
| 40 |
-
bar_length = 30
|
| 41 |
-
|
| 42 |
-
while video.status in ("in_progress", "queued"):
|
| 43 |
-
video = openai.videos.retrieve(video.id)
|
| 44 |
-
progress = getattr(video, "progress", 0)
|
| 45 |
-
|
| 46 |
-
filled_length = int((progress / 100) * bar_length)
|
| 47 |
-
bar = "=" * filled_length + "-" * (bar_length - filled_length)
|
| 48 |
-
status_text = "Queued" if video.status == "queued" else "Processing"
|
| 49 |
-
|
| 50 |
-
sys.stdout.write(f"\r{status_text}: [{bar}] {progress:.1f}%")
|
| 51 |
-
sys.stdout.flush()
|
| 52 |
-
time.sleep(2)
|
| 53 |
-
|
| 54 |
-
sys.stdout.write("\n")
|
| 55 |
-
|
| 56 |
-
if video.status == "failed":
|
| 57 |
-
message = getattr(
|
| 58 |
-
getattr(video, "error", None), "message", "Video generation failed"
|
| 59 |
)
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
sys.exit(1)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import sys
|
| 3 |
import time
|
| 4 |
+
import pathlib
|
| 5 |
from openai import OpenAI
|
| 6 |
|
| 7 |
+
# Ambil API key dari environment (set di Space Secrets atau lokal)
|
| 8 |
+
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
if not API_KEY:
|
| 10 |
+
print("ERROR: Set environment variable OPENAI_API_KEY terlebih dahulu.")
|
| 11 |
+
sys.exit(1)
|
| 12 |
|
| 13 |
+
client = OpenAI(api_key=API_KEY)
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
MODEL = "sora-2" # atau "gpt-video-1" tergantung akses akunmu
|
| 16 |
+
PROMPT = (
|
| 17 |
+
"YouTuber, black t-shirt, in a professional studio setup, sitting at his desk "
|
| 18 |
+
"talking directly into a Shure SM7B microphone on a boom arm. He says with a smirk: "
|
| 19 |
+
"'Wait... did an AI just generate me to teach you about AI? That's pretty meta...' "
|
| 20 |
+
"Dark background with a soft blue LED light glow. Professional lighting setup with key light and rim light. "
|
| 21 |
+
"Shot on Sony FX6, cinematic depth of field, crisp focus, vertical format 9:16."
|
| 22 |
)
|
| 23 |
+
SIZE = "720x1280" # 9:16
|
| 24 |
+
SECONDS = 10 # durasi 10 detik
|
| 25 |
+
|
| 26 |
+
outdir = pathlib.Path("output")
|
| 27 |
+
outdir.mkdir(parents=True, exist_ok=True)
|
| 28 |
+
|
| 29 |
+
def as_percent(v):
|
| 30 |
+
try:
|
| 31 |
+
x = float(v or 0)
|
| 32 |
+
return x * 100 if 0 <= x <= 1 else x
|
| 33 |
+
except Exception:
|
| 34 |
+
return 0.0
|
| 35 |
+
|
| 36 |
+
# 1) Create job
|
| 37 |
+
try:
|
| 38 |
+
job = client.videos.create(
|
| 39 |
+
model=MODEL,
|
| 40 |
+
prompt=PROMPT,
|
| 41 |
+
size=SIZE,
|
| 42 |
+
seconds=SECONDS,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
+
vid_id = job.id
|
| 45 |
+
print(f"Video job created: {vid_id} (status={job.status})")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
s = str(e)
|
| 48 |
+
if "billing_hard_limit_reached" in s:
|
| 49 |
+
print("Gagal membuat video: limit/saldo akun OpenAI habis. Tambahkan kredit/naikkan hard limit atau gunakan API key lain.")
|
| 50 |
+
elif "insufficient_quota" in s:
|
| 51 |
+
print("Gagal: insufficient_quota. Cek Billing/kuota akun kamu.")
|
| 52 |
+
elif "invalid_api_key" in s:
|
| 53 |
+
print("Gagal: API key tidak valid.")
|
| 54 |
+
else:
|
| 55 |
+
print("Gagal membuat video:", s)
|
| 56 |
sys.exit(1)
|
| 57 |
|
| 58 |
+
# 2) Poll sampai selesai
|
| 59 |
+
bar_len = 30
|
| 60 |
+
while True:
|
| 61 |
+
job = client.videos.retrieve(vid_id)
|
| 62 |
+
status = getattr(job, "status", "unknown")
|
| 63 |
+
progress = as_percent(getattr(job, "progress", 0))
|
| 64 |
+
filled = int((progress / 100) * bar_len)
|
| 65 |
+
bar = "=" * filled + "-" * (bar_len - filled)
|
| 66 |
+
sys.stdout.write(f"\r{status.capitalize():<11} [{bar}] {progress:5.1f}%")
|