MySafeCode commited on
Commit
2743007
·
verified ·
1 Parent(s): bc714d3

Create curlapp.py

Browse files
Files changed (1) hide show
  1. curlapp.py +92 -0
curlapp.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import time
5
+ import json
6
+
7
+ # Your API key from Hugging Face secret "key"
8
+ API_KEY = os.environ.get("key", "")
9
+
10
+ def generate_video(prompt_text, image_url):
11
+ """Exactly like the curl command"""
12
+
13
+ # 1. Create task - EXACT curl command in Python
14
+ create_url = "https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks"
15
+
16
+ headers = {
17
+ "Content-Type": "application/json",
18
+ "Authorization": f"Bearer {API_KEY}" # Exactly like curl: -H "Authorization: Bearer $ARK_API_KEY"
19
+ }
20
+
21
+ # Exactly the curl data
22
+ data = {
23
+ "model": "seedance-1-5-pro-251215",
24
+ "content": [
25
+ {
26
+ "type": "text",
27
+ "text": prompt_text
28
+ },
29
+ {
30
+ "type": "image_url",
31
+ "image_url": {
32
+ "url": image_url
33
+ }
34
+ }
35
+ ]
36
+ }
37
+
38
+ # Make the request (like curl -X POST)
39
+ response = requests.post(create_url, headers=headers, json=data)
40
+
41
+ if response.status_code != 200:
42
+ return f"Error: {response.text}", None
43
+
44
+ task_id = response.json()["id"]
45
+
46
+ # 2. Poll for results (like the while loop)
47
+ status_url = f"https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks/{task_id}"
48
+
49
+ for _ in range(60): # 60 second timeout
50
+ status_response = requests.get(status_url, headers=headers)
51
+ result = status_response.json()
52
+
53
+ if result.get("status") == "succeeded":
54
+ # Get video URL from response
55
+ video_url = result.get("output", [{}])[0].get("video_url")
56
+ return "Success!", video_url
57
+ elif result.get("status") == "failed":
58
+ return f"Failed: {result.get('error')}", None
59
+
60
+ time.sleep(1)
61
+
62
+ return "Timeout", None
63
+
64
+ # Simple Gradio interface
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("# BytePlus Video Generator")
67
+ gr.Markdown(f"API Key loaded: {'✅' if API_KEY else '❌'}")
68
+
69
+ with gr.Row():
70
+ with gr.Column():
71
+ prompt = gr.Textbox(
72
+ label="Prompt",
73
+ lines=3,
74
+ value="At breakneck speed, drones fly through obstacles --duration 5 --camerafixed false"
75
+ )
76
+ image_url = gr.Textbox(
77
+ label="Image URL",
78
+ value="https://ark-doc.tos-ap-southeast-1.bytepluses.com/seepro_i2v%20.png"
79
+ )
80
+ btn = gr.Button("Generate", variant="primary")
81
+
82
+ with gr.Column():
83
+ status = gr.Textbox(label="Status")
84
+ video = gr.Video(label="Result")
85
+
86
+ btn.click(
87
+ fn=generate_video,
88
+ inputs=[prompt, image_url],
89
+ outputs=[status, video]
90
+ )
91
+
92
+ demo.launch()