Update a.py
Browse files
a.py
CHANGED
|
@@ -1,54 +1,62 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
-
import
|
| 5 |
-
import
|
|
|
|
| 6 |
from byteplussdkarkruntime import Ark
|
| 7 |
-
import
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
logging.basicConfig(level=logging.INFO)
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
# Get API key from environment variable "Key"
|
| 14 |
API_KEY = os.environ.get("Key", "")
|
| 15 |
-
logger.info(f"API Key loaded: {'Yes' if API_KEY else 'No'}")
|
| 16 |
-
logger.info(f"Key length: {len(API_KEY)}")
|
| 17 |
-
|
| 18 |
-
# Configure SDK with HTTP scheme (as per the non-compatible upgrade notice)
|
| 19 |
-
configuration = byteplussdkcore.Configuration()
|
| 20 |
-
configuration.client_side_validation = True
|
| 21 |
-
configuration.schema = "http" # Explicitly use HTTP as per notice
|
| 22 |
-
configuration.debug = False
|
| 23 |
-
configuration.logger_file = "sdk.log"
|
| 24 |
-
byteplussdkcore.Configuration.set_default(configuration)
|
| 25 |
|
| 26 |
-
# Initialize client
|
| 27 |
client = Ark(
|
| 28 |
base_url="https://ark.ap-southeast.bytepluses.com/api/v3",
|
| 29 |
api_key=API_KEY,
|
| 30 |
-
timeout=1800, # 30 minute timeout as recommended
|
| 31 |
-
max_retries=3,
|
| 32 |
-
http_client=httpx.Client(
|
| 33 |
-
proxies={
|
| 34 |
-
"http://": None, # Disable proxy
|
| 35 |
-
"https://": None, # Disable proxy
|
| 36 |
-
},
|
| 37 |
-
timeout=httpx.Timeout(1800.0),
|
| 38 |
-
)
|
| 39 |
)
|
| 40 |
|
| 41 |
-
def
|
| 42 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
if not API_KEY:
|
| 45 |
-
yield "❌
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
progress(0, desc="
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
create_result = client.content_generation.tasks.create(
|
| 53 |
model="seedance-1-5-pro-251215",
|
| 54 |
content=[
|
|
@@ -66,72 +74,114 @@ def generate_video(prompt_text, image_url, progress=gr.Progress()):
|
|
| 66 |
)
|
| 67 |
|
| 68 |
task_id = create_result.id
|
| 69 |
-
|
| 70 |
-
yield f"Task created: {task_id}", None
|
| 71 |
|
| 72 |
progress(0.3, desc="Polling for results...")
|
| 73 |
|
| 74 |
-
#
|
| 75 |
attempts = 0
|
| 76 |
-
max_attempts =
|
| 77 |
|
| 78 |
while attempts < max_attempts:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
return
|
| 92 |
-
else:
|
| 93 |
-
progress(0.3 + (attempts/max_attempts)*0.7, desc=f"Status: {status}")
|
| 94 |
-
yield f"⏳ Status: {status}...", None
|
| 95 |
-
time.sleep(1)
|
| 96 |
-
attempts += 1
|
| 97 |
-
|
| 98 |
-
except Exception as e:
|
| 99 |
-
logger.error(f"Polling error: {e}")
|
| 100 |
-
yield f"⚠️ Polling error: {e}", None
|
| 101 |
-
time.sleep(2)
|
| 102 |
attempts += 1
|
| 103 |
-
|
| 104 |
-
yield "⏰ Timeout after
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
-
|
| 108 |
yield f"❌ Error: {str(e)}", None
|
| 109 |
|
| 110 |
-
# Simple interface
|
| 111 |
-
with gr.Blocks(title="BytePlus Video Generator") as demo:
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
with gr.Row():
|
| 115 |
with gr.Column():
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
| 120 |
)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
with gr.Column():
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
|
|
|
| 132 |
fn=generate_video,
|
| 133 |
-
inputs=[
|
| 134 |
-
outputs=[status,
|
| 135 |
)
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import tempfile
|
| 5 |
+
import requests
|
| 6 |
from byteplussdkarkruntime import Ark
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import io
|
| 9 |
|
| 10 |
+
# Get API key from Hugging Face secret "Key"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
API_KEY = os.environ.get("Key", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Initialize client exactly like your working code
|
| 14 |
client = Ark(
|
| 15 |
base_url="https://ark.ap-southeast.bytepluses.com/api/v3",
|
| 16 |
api_key=API_KEY,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
+
def upload_image_to_temp_url(image):
|
| 20 |
+
"""Upload image to temporary hosting and return URL"""
|
| 21 |
+
# For now, we need a public URL for the API
|
| 22 |
+
# Option 1: Use a free image hosting service
|
| 23 |
+
# Option 2: Save temporarily and return path (won't work for API)
|
| 24 |
+
# Option 3: Keep URL input for now with preview
|
| 25 |
+
|
| 26 |
+
# Let's keep it simple - return None and we'll handle it in the UI
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
def generate_video(image, prompt_text, progress=gr.Progress()):
|
| 30 |
+
"""Generate video using your exact working code pattern"""
|
| 31 |
|
| 32 |
if not API_KEY:
|
| 33 |
+
yield "❌ API Key not configured. Please add 'Key' secret.", None
|
| 34 |
+
return
|
| 35 |
+
|
| 36 |
+
if image is None:
|
| 37 |
+
yield "⚠️ Please upload an image first", None
|
| 38 |
return
|
| 39 |
|
| 40 |
try:
|
| 41 |
+
progress(0, desc="Preparing image...")
|
| 42 |
+
|
| 43 |
+
# Save uploaded image temporarily
|
| 44 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
|
| 45 |
+
image.save(tmp_file.name)
|
| 46 |
+
|
| 47 |
+
# Upload to a temporary hosting service
|
| 48 |
+
# For now, we'll use a placeholder - we need to implement this
|
| 49 |
+
# Option: Use imgbb.com API or similar
|
| 50 |
+
image_url = "https://ark-doc.tos-ap-southeast-1.bytepluses.com/seepro_i2v%20.png" # Placeholder
|
| 51 |
+
|
| 52 |
+
# TODO: Implement actual image upload to hosting service
|
| 53 |
+
yield "⏳ Note: Direct image upload needs hosting service. Using placeholder for now.", None
|
| 54 |
+
time.sleep(1)
|
| 55 |
|
| 56 |
+
progress(0.2, desc="Creating request...")
|
| 57 |
+
|
| 58 |
+
# Exactly your working code structure
|
| 59 |
+
print("----- create request -----")
|
| 60 |
create_result = client.content_generation.tasks.create(
|
| 61 |
model="seedance-1-5-pro-251215",
|
| 62 |
content=[
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
task_id = create_result.id
|
| 77 |
+
print(f"Task created: {task_id}")
|
| 78 |
+
yield f"✅ Task created: {task_id}", None
|
| 79 |
|
| 80 |
progress(0.3, desc="Polling for results...")
|
| 81 |
|
| 82 |
+
# Polling exactly like your working code
|
| 83 |
attempts = 0
|
| 84 |
+
max_attempts = 120 # 2 minutes max
|
| 85 |
|
| 86 |
while attempts < max_attempts:
|
| 87 |
+
get_result = client.content_generation.tasks.get(task_id=task_id)
|
| 88 |
+
status = get_result.status
|
| 89 |
+
|
| 90 |
+
if status == "succeeded":
|
| 91 |
+
progress(1.0, desc="Complete!")
|
| 92 |
+
# Extract video URL from the response structure we saw
|
| 93 |
+
video_url = get_result.content.video_url if hasattr(get_result, 'content') else None
|
| 94 |
+
print(f"Video URL: {video_url}")
|
| 95 |
+
yield "✅ Video generated successfully!", video_url
|
| 96 |
+
return
|
| 97 |
|
| 98 |
+
elif status == "failed":
|
| 99 |
+
error_msg = get_result.error if hasattr(get_result, 'error') else "Unknown error"
|
| 100 |
+
yield f"❌ Failed: {error_msg}", None
|
| 101 |
+
return
|
| 102 |
+
else:
|
| 103 |
+
progress(0.3 + (attempts/max_attempts)*0.7, desc=f"Status: {status}")
|
| 104 |
+
yield f"⏳ Status: {status}... (attempt {attempts + 1})", None
|
| 105 |
+
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
attempts += 1
|
| 107 |
+
|
| 108 |
+
yield "⏰ Timeout after 2 minutes", None
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
+
print(f"Error: {e}")
|
| 112 |
yield f"❌ Error: {str(e)}", None
|
| 113 |
|
| 114 |
+
# Simple, clean interface
|
| 115 |
+
with gr.Blocks(title="BytePlus Video Generator", theme=gr.themes.Soft()) as demo:
|
| 116 |
+
|
| 117 |
+
gr.Markdown("""
|
| 118 |
+
# 🎥 BytePlus Video Generator
|
| 119 |
+
|
| 120 |
+
Upload an image and describe the video you want to generate.
|
| 121 |
+
""")
|
| 122 |
+
|
| 123 |
+
# Show API key status
|
| 124 |
+
if API_KEY:
|
| 125 |
+
gr.Markdown("✅ **API Key:** Configured")
|
| 126 |
+
else:
|
| 127 |
+
gr.Markdown("❌ **API Key:** Not configured - please add 'Key' secret")
|
| 128 |
|
| 129 |
with gr.Row():
|
| 130 |
with gr.Column():
|
| 131 |
+
# Image upload
|
| 132 |
+
image_input = gr.Image(
|
| 133 |
+
label="Upload Starting Image",
|
| 134 |
+
type="pil",
|
| 135 |
+
height=300
|
| 136 |
)
|
| 137 |
+
|
| 138 |
+
# Text prompt
|
| 139 |
+
prompt = gr.Textbox(
|
| 140 |
+
label="Video Description",
|
| 141 |
+
lines=4,
|
| 142 |
+
placeholder="Describe what you want to see...",
|
| 143 |
+
value="At breakneck speed, drones thread through intricate obstacles or stunning natural wonders, delivering an immersive, heart-pounding flying experience. --duration 5 --camerafixed false"
|
| 144 |
)
|
| 145 |
+
|
| 146 |
+
# Generate button
|
| 147 |
+
generate_btn = gr.Button("🚀 Generate Video", variant="primary", size="lg")
|
| 148 |
|
| 149 |
with gr.Column():
|
| 150 |
+
# Status
|
| 151 |
+
status = gr.Textbox(
|
| 152 |
+
label="Status",
|
| 153 |
+
lines=4,
|
| 154 |
+
interactive=False
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Video output
|
| 158 |
+
video_output = gr.Video(
|
| 159 |
+
label="Generated Video",
|
| 160 |
+
interactive=False
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# Example prompts
|
| 164 |
+
gr.Markdown("---")
|
| 165 |
+
gr.Markdown("### Example Prompts")
|
| 166 |
+
with gr.Row():
|
| 167 |
+
gr.Button("Nature").click(
|
| 168 |
+
fn=lambda: "Aerial drone shot over mountains at sunrise, cinematic --duration 5 --camerafixed false",
|
| 169 |
+
outputs=prompt
|
| 170 |
+
)
|
| 171 |
+
gr.Button("City").click(
|
| 172 |
+
fn=lambda: "Fast drone racing through futuristic city streets --duration 5 --camerafixed false",
|
| 173 |
+
outputs=prompt
|
| 174 |
+
)
|
| 175 |
+
gr.Button("Ocean").click(
|
| 176 |
+
fn=lambda: "Drone following waves at golden hour --duration 5 --camerafixed false",
|
| 177 |
+
outputs=prompt
|
| 178 |
+
)
|
| 179 |
|
| 180 |
+
# Connect the generate button
|
| 181 |
+
generate_event = generate_btn.click(
|
| 182 |
fn=generate_video,
|
| 183 |
+
inputs=[image_input, prompt],
|
| 184 |
+
outputs=[status, video_output]
|
| 185 |
)
|
| 186 |
|
| 187 |
if __name__ == "__main__":
|