sharul20001 commited on
Commit
84a7681
·
verified ·
1 Parent(s): e4de7b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -16
app.py CHANGED
@@ -12,10 +12,7 @@ def make_headers(user_key: str | None):
12
  api_key = (user_key or os.getenv("KIE_API_KEY") or "").strip()
13
  if not api_key:
14
  raise gr.Error("Masukkan KIE API key atau set KIE_API_KEY di Secrets.")
15
- return {
16
- "Authorization": f"Bearer {api_key}",
17
- "Content-Type": "application/json",
18
- }
19
 
20
  def create_task(headers, prompt: str, aspect_ratio: str, duration: str | None):
21
  payload = {
@@ -23,7 +20,7 @@ def create_task(headers, prompt: str, aspect_ratio: str, duration: str | None):
23
  "input": {
24
  "prompt": prompt,
25
  "aspect_ratio": aspect_ratio,
26
- "remove_watermark": False, # patuh ToS
27
  },
28
  }
29
  if duration:
@@ -57,23 +54,17 @@ def create_task(headers, prompt: str, aspect_ratio: str, duration: str | None):
57
  def infer_status_and_result(obj: dict):
58
  status = obj.get("status") or obj.get("state") or (obj.get("data") or {}).get("status")
59
  progress = obj.get("progress") or (obj.get("data") or {}).get("progress")
60
- # cari link video pada beberapa kemungkinan field
61
- for key in [
62
- "result_url","output_url","video_url","download_url",
63
- ]:
64
  if key in obj and isinstance(obj[key], str) and obj[key].startswith("http"):
65
  return status, progress, obj[key]
66
  data = obj.get("data") or {}
67
- for key in [
68
- "result_url","output_url","video_url","download_url",
69
- ]:
70
  v = data.get(key)
71
  if isinstance(v, str) and v.startswith("http"):
72
  return status, progress, v
73
  return status, progress, None
74
 
75
  def probe_status_url(headers, task_id: str):
76
- # Coba beberapa pola umum sampai ada yang OK
77
  candidates = [
78
  f"{BASE}/jobs/getTask?taskId={task_id}",
79
  f"{BASE}/jobs/get-task?taskId={task_id}",
@@ -96,7 +87,7 @@ def probe_status_url(headers, task_id: str):
96
  return url
97
  except Exception:
98
  pass
99
- # Beberapa layanan pakai POST getTask
100
  post_url = f"{BASE}/jobs/getTask"
101
  try:
102
  r = requests.post(post_url, headers=headers, json={"taskId": task_id}, timeout=30)
@@ -195,7 +186,6 @@ def runtime_info(user_api_key):
195
  masked = (key[:4] + "..." + key[-4:]) if key else "(none)"
196
  return f"Key source: {src}\nKey hint: {masked}\nBase: {BASE}"
197
 
198
- # ============ Gradio UI (Pure Gradio) ============
199
  with gr.Blocks(title="Kie AI — Sora‑2 Text‑to‑Video (Polling)") as demo:
200
  gr.Markdown("## Kie AI — Sora‑2 Text‑to‑Video\n- Gunakan API key Kie AI (bukan OpenAI).\n- remove_watermark dimatikan (False).")
201
  api = gr.Textbox(label="KIE API Key (opsional, BYOK)", type="password", placeholder="kie-...")
@@ -220,4 +210,6 @@ with gr.Blocks(title="Kie AI — Sora‑2 Text‑to‑Video (Polling)") as demo:
220
  run.click(run_job, inputs=[api, prompt, aspect, duration], outputs=[status, video])
221
  check.click(runtime_info, inputs=[api], outputs=[info])
222
 
223
- # Penting untuk Spaces: expose variable 'demo'
 
 
 
12
  api_key = (user_key or os.getenv("KIE_API_KEY") or "").strip()
13
  if not api_key:
14
  raise gr.Error("Masukkan KIE API key atau set KIE_API_KEY di Secrets.")
15
+ return {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
 
 
 
16
 
17
  def create_task(headers, prompt: str, aspect_ratio: str, duration: str | None):
18
  payload = {
 
20
  "input": {
21
  "prompt": prompt,
22
  "aspect_ratio": aspect_ratio,
23
+ "remove_watermark": False,
24
  },
25
  }
26
  if duration:
 
54
  def infer_status_and_result(obj: dict):
55
  status = obj.get("status") or obj.get("state") or (obj.get("data") or {}).get("status")
56
  progress = obj.get("progress") or (obj.get("data") or {}).get("progress")
57
+ for key in ["result_url","output_url","video_url","download_url"]:
 
 
 
58
  if key in obj and isinstance(obj[key], str) and obj[key].startswith("http"):
59
  return status, progress, obj[key]
60
  data = obj.get("data") or {}
61
+ for key in ["result_url","output_url","video_url","download_url"]:
 
 
62
  v = data.get(key)
63
  if isinstance(v, str) and v.startswith("http"):
64
  return status, progress, v
65
  return status, progress, None
66
 
67
  def probe_status_url(headers, task_id: str):
 
68
  candidates = [
69
  f"{BASE}/jobs/getTask?taskId={task_id}",
70
  f"{BASE}/jobs/get-task?taskId={task_id}",
 
87
  return url
88
  except Exception:
89
  pass
90
+ # POST variant
91
  post_url = f"{BASE}/jobs/getTask"
92
  try:
93
  r = requests.post(post_url, headers=headers, json={"taskId": task_id}, timeout=30)
 
186
  masked = (key[:4] + "..." + key[-4:]) if key else "(none)"
187
  return f"Key source: {src}\nKey hint: {masked}\nBase: {BASE}"
188
 
 
189
  with gr.Blocks(title="Kie AI — Sora‑2 Text‑to‑Video (Polling)") as demo:
190
  gr.Markdown("## Kie AI — Sora‑2 Text‑to‑Video\n- Gunakan API key Kie AI (bukan OpenAI).\n- remove_watermark dimatikan (False).")
191
  api = gr.Textbox(label="KIE API Key (opsional, BYOK)", type="password", placeholder="kie-...")
 
210
  run.click(run_job, inputs=[api, prompt, aspect, duration], outputs=[status, video])
211
  check.click(runtime_info, inputs=[api], outputs=[info])
212
 
213
+ # Tambahkan launch jika Space type = Python
214
+ if __name__ == "__main__":
215
+ demo.queue().launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))