MySafeCode commited on
Commit
5ccfc8e
·
verified ·
1 Parent(s): 9c05089

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +231 -0
app.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import time
4
+ from byteplussdkarkruntime import Ark
5
+ import tempfile
6
+ import requests
7
+ from PIL import Image
8
+ from io import BytesIO
9
+
10
+ # Initialize the client (will be set when API key is provided)
11
+ client = None
12
+
13
+ def initialize_client(api_key):
14
+ """Initialize the Ark client with the provided API key"""
15
+ global client
16
+ try:
17
+ client = Ark(
18
+ base_url="https://ark.ap-southeast.bytepluses.com/api/v3",
19
+ api_key=api_key,
20
+ )
21
+ return "✅ Client initialized successfully!"
22
+ except Exception as e:
23
+ return f"❌ Error initializing client: {str(e)}"
24
+
25
+ def generate_video(api_key, prompt_text, image_url, model_id="seedance-1-5-pro-251215"):
26
+ """Generate video using the Ark API"""
27
+ global client
28
+
29
+ # Initialize client if not already done or if API key changed
30
+ if client is None:
31
+ init_result = initialize_client(api_key)
32
+ if "❌" in init_result:
33
+ return init_result, None
34
+ else:
35
+ # Update client with new API key (optional - you might want to recreate)
36
+ try:
37
+ client = Ark(base_url="https://ark.ap-southeast.bytepluses.com/api/v3", api_key=api_key)
38
+ except:
39
+ pass
40
+
41
+ try:
42
+ print("🚀 Creating video generation request...")
43
+
44
+ # Prepare content
45
+ content = [
46
+ {
47
+ "type": "text",
48
+ "text": prompt_text if prompt_text else "At breakneck speed, drones thread through intricate obstacles --duration 5 --camerafixed false"
49
+ },
50
+ {
51
+ "type": "image_url",
52
+ "image_url": {
53
+ "url": image_url if image_url else "https://ark-doc.tos-ap-southeast-1.bytepluses.com/seepro_i2v%20.png"
54
+ }
55
+ }
56
+ ]
57
+
58
+ # Create task
59
+ create_result = client.content_generation.tasks.create(
60
+ model=model_id,
61
+ content=content
62
+ )
63
+
64
+ task_id = create_result.id
65
+ print(f"📋 Task ID: {task_id}")
66
+
67
+ # Polling for result
68
+ yield f"⏳ Task created with ID: {task_id}. Waiting for completion...", None
69
+
70
+ max_attempts = 60 # Maximum polling attempts (60 seconds)
71
+ attempts = 0
72
+
73
+ while attempts < max_attempts:
74
+ get_result = client.content_generation.tasks.get(task_id=task_id)
75
+ status = get_result.status
76
+
77
+ if status == "succeeded":
78
+ print("✅ Task succeeded!")
79
+ # Extract video URL from result
80
+ # Note: Adjust this based on actual response structure
81
+ video_url = None
82
+ if hasattr(get_result, 'output') and get_result.output:
83
+ if isinstance(get_result.output, list) and len(get_result.output) > 0:
84
+ video_url = get_result.output[0].get('video_url')
85
+
86
+ if video_url:
87
+ yield f"✅ Video generated successfully!", video_url
88
+ else:
89
+ yield f"✅ Task completed, but no video URL found in response. Full response: {get_result}", None
90
+ break
91
+
92
+ elif status == "failed":
93
+ error_msg = get_result.error if hasattr(get_result, 'error') else "Unknown error"
94
+ yield f"❌ Task failed: {error_msg}", None
95
+ break
96
+ else:
97
+ progress_msg = f"⏳ Current status: {status}, waiting... (attempt {attempts + 1}/{max_attempts})"
98
+ print(progress_msg)
99
+ yield progress_msg, None
100
+ time.sleep(1)
101
+ attempts += 1
102
+
103
+ if attempts >= max_attempts:
104
+ yield "⏰ Timeout: Task took too long to complete. Please try again.", None
105
+
106
+ except Exception as e:
107
+ yield f"❌ Error: {str(e)}", None
108
+
109
+ def upload_image(image_file):
110
+ """Handle image upload and return URL"""
111
+ if image_file is None:
112
+ return "https://ark-doc.tos-ap-southeast-1.bytepluses.com/seepro_i2v%20.png"
113
+
114
+ # For Hugging Face Spaces, we can use a temporary file approach
115
+ # In production, you might want to upload to a hosting service
116
+ return image_file.name
117
+
118
+ # Create Gradio interface
119
+ with gr.Blocks(title="BytePlus Video Generation", theme=gr.themes.Soft()) as demo:
120
+ gr.Markdown("""
121
+ # 🎥 BytePlus Video Generation
122
+ Generate stunning videos from images and text prompts using the BytePlus API.
123
+ """)
124
+
125
+ with gr.Row():
126
+ with gr.Column(scale=1):
127
+ # API Key input
128
+ api_key = gr.Textbox(
129
+ label="🔑 API Key",
130
+ placeholder="Enter your BytePlus API key here",
131
+ type="password",
132
+ value="key" # This will be "key" as per your instruction
133
+ )
134
+
135
+ # Model selection
136
+ model_id = gr.Textbox(
137
+ label="🤖 Model ID",
138
+ value="seedance-1-5-pro-251215",
139
+ info="Default model for video generation"
140
+ )
141
+
142
+ # Image upload
143
+ image_input = gr.Image(
144
+ label="🖼️ Upload Starting Image (Optional)",
145
+ type="filepath"
146
+ )
147
+
148
+ # Manual image URL input
149
+ image_url_input = gr.Textbox(
150
+ label="🔗 Or Enter Image URL",
151
+ placeholder="https://example.com/image.jpg",
152
+ value="https://ark-doc.tos-ap-southeast-1.bytepluses.com/seepro_i2v%20.png"
153
+ )
154
+
155
+ # Text prompt
156
+ prompt_input = gr.Textbox(
157
+ label="📝 Text Prompt",
158
+ placeholder="Describe your video...",
159
+ value="At breakneck speed, drones thread through intricate obstacles or stunning natural wonders, delivering an immersive, heart-pounding flying experience. --duration 5 --camerafixed false",
160
+ lines=4
161
+ )
162
+
163
+ # Generate button
164
+ generate_btn = gr.Button("🚀 Generate Video", variant="primary")
165
+
166
+ with gr.Column(scale=1):
167
+ # Status output
168
+ status_output = gr.Textbox(
169
+ label="📊 Status",
170
+ placeholder="Generation status will appear here...",
171
+ lines=3
172
+ )
173
+
174
+ # Video output
175
+ video_output = gr.Video(
176
+ label="🎬 Generated Video",
177
+ interactive=False
178
+ )
179
+
180
+ # Direct video URL
181
+ video_url_output = gr.Textbox(
182
+ label="🔗 Video URL",
183
+ placeholder="Video URL will appear here...",
184
+ lines=2,
185
+ interactive=False
186
+ )
187
+
188
+ # Example section
189
+ gr.Markdown("""
190
+ ## 📋 Examples
191
+ Try these example prompts:
192
+ """)
193
+
194
+ with gr.Row():
195
+ example1 = gr.Button("🌄 Nature drone shot")
196
+ example2 = gr.Button("🏙️ City flythrough")
197
+ example3 = gr.Button("🌊 Ocean waves")
198
+
199
+ def set_example1():
200
+ return "Aerial drone shot flying over majestic mountains at sunrise, cinematic lighting, smooth motion --duration 5 --camerafixed false"
201
+
202
+ def set_example2():
203
+ return "Fast-paced drone racing through futuristic city streets with neon lights, dynamic angles --duration 5 --camerafixed false"
204
+
205
+ def set_example3():
206
+ return "Drone following surfers riding massive waves, slow motion, dramatic ocean views --duration 5 --camerafixed false"
207
+
208
+ example1.click(fn=set_example1, outputs=prompt_input)
209
+ example2.click(fn=set_example2, outputs=prompt_input)
210
+ example3.click(fn=set_example3, outputs=prompt_input)
211
+
212
+ # Handle generation
213
+ generate_btn.click(
214
+ fn=generate_video,
215
+ inputs=[api_key, prompt_input, image_url_input, model_id],
216
+ outputs=[status_output, video_output]
217
+ ).then(
218
+ fn=lambda url: url if url else "No URL available",
219
+ inputs=[video_output],
220
+ outputs=[video_url_output]
221
+ )
222
+
223
+ # When image is uploaded, update the URL input
224
+ image_input.change(
225
+ fn=upload_image,
226
+ inputs=image_input,
227
+ outputs=image_url_input
228
+ )
229
+
230
+ if __name__ == "__main__":
231
+ demo.launch()