sharul20001 commited on
Commit
b3b68e4
·
verified ·
1 Parent(s): bbda759

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -59
app.py CHANGED
@@ -1,69 +1,66 @@
 
1
  import sys
2
  import time
 
3
  from openai import OpenAI
4
 
5
- openai = OpenAI()
 
 
 
 
6
 
7
- # --------------------------------------------------------------
8
- # Create a new video
9
- # --------------------------------------------------------------
10
 
11
- # Takes about 2 minutes
12
- video = openai.videos.create(
13
- model="sora-2",
14
- prompt="YouTuber, black t-shirt, in a professional studio setup, sitting at his desk talking directly into a Shure SM7B microphone on a boom arm. He says with a smirk: 'Wait... did an AI just generate me to teach you about AI? That's pretty meta...' Dark background with a soft blue LED light glow. Professional lighting setup with key light and rim light. Shot on Sony FX6, cinematic depth of field, crisp focus, vertical format 9:16.",
15
- size="720x1280",
16
- seconds="4",
 
17
  )
18
-
19
- print("Video generation started:", video)
20
-
21
- # --------------------------------------------------------------
22
- # List all videos
23
- # --------------------------------------------------------------
24
-
25
- time.sleep(3)
26
- videos_list = openai.videos.list().data
27
-
28
- # --------------------------------------------------------------
29
- # Check the last's video status
30
- # --------------------------------------------------------------
31
-
32
- video = openai.videos.retrieve(videos_list[0].id)
33
- print("Video status:", video.status)
34
-
35
- # --------------------------------------------------------------
36
- # Download video
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
- print(message)
 
 
 
 
 
 
 
 
 
 
 
61
  sys.exit(1)
62
 
63
- print("Video generation completed:", video)
64
- print("Downloading video content...")
65
-
66
- content = openai.videos.download_content(video.id, variant="video")
67
- content.write_to_file(f"output/{video.id}.mp4")
68
-
69
- print(f"Wrote output/{video.id}.mp4")
 
 
 
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}%")