Delete app6.py
Browse files
app6.py
DELETED
|
@@ -1,171 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import time
|
| 4 |
-
import logging
|
| 5 |
-
import socket
|
| 6 |
-
import requests
|
| 7 |
-
from byteplussdkarkruntime import Ark
|
| 8 |
-
import byteplussdkcore
|
| 9 |
-
|
| 10 |
-
# Set up logging
|
| 11 |
-
logging.basicConfig(level=logging.INFO)
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
-
|
| 14 |
-
# Get API key
|
| 15 |
-
API_KEY = os.environ.get("key", "")
|
| 16 |
-
logger.info(f"API Key loaded: {'Yes' if API_KEY else 'No'}")
|
| 17 |
-
logger.info(f"Key length: {len(API_KEY)}")
|
| 18 |
-
|
| 19 |
-
# Test basic connectivity first
|
| 20 |
-
def test_connectivity():
|
| 21 |
-
"""Test if we can reach the API server"""
|
| 22 |
-
try:
|
| 23 |
-
# Test DNS resolution
|
| 24 |
-
ip = socket.gethostbyname('ark.ap-southeast.bytepluses.com')
|
| 25 |
-
logger.info(f"✅ DNS resolved: {ip}")
|
| 26 |
-
|
| 27 |
-
# Test basic HTTPS connection
|
| 28 |
-
r = requests.get("https://ark.ap-southeast.bytepluses.com", timeout=5)
|
| 29 |
-
logger.info(f"✅ Base URL reachable (status: {r.status_code})")
|
| 30 |
-
return True
|
| 31 |
-
except Exception as e:
|
| 32 |
-
logger.error(f"❌ Connection test failed: {e}")
|
| 33 |
-
return False
|
| 34 |
-
|
| 35 |
-
# Run connection test on startup
|
| 36 |
-
connectivity_ok = test_connectivity()
|
| 37 |
-
|
| 38 |
-
# Configure SDK
|
| 39 |
-
try:
|
| 40 |
-
configuration = byteplussdkcore.Configuration()
|
| 41 |
-
configuration.client_side_validation = True
|
| 42 |
-
configuration.schema = "http"
|
| 43 |
-
configuration.debug = True # Enable debug logging
|
| 44 |
-
configuration.logger_file = "sdk.log"
|
| 45 |
-
byteplussdkcore.Configuration.set_default(configuration)
|
| 46 |
-
logger.info("✅ SDK configured")
|
| 47 |
-
except Exception as e:
|
| 48 |
-
logger.error(f"❌ SDK config failed: {e}")
|
| 49 |
-
|
| 50 |
-
# Initialize client
|
| 51 |
-
try:
|
| 52 |
-
client = Ark(
|
| 53 |
-
base_url="https://ark.ap-southeast.bytepluses.com/api/v3",
|
| 54 |
-
api_key=API_KEY,
|
| 55 |
-
)
|
| 56 |
-
logger.info("✅ Client initialized")
|
| 57 |
-
except Exception as e:
|
| 58 |
-
logger.error(f"❌ Client init failed: {e}")
|
| 59 |
-
client = None
|
| 60 |
-
|
| 61 |
-
def generate_video(prompt_text, image_url, progress=gr.Progress()):
|
| 62 |
-
"""Generate video with detailed debugging"""
|
| 63 |
-
|
| 64 |
-
if not API_KEY:
|
| 65 |
-
yield "❌ No API key", None
|
| 66 |
-
return
|
| 67 |
-
|
| 68 |
-
if not client:
|
| 69 |
-
yield "❌ Client not initialized", None
|
| 70 |
-
return
|
| 71 |
-
|
| 72 |
-
if not connectivity_ok:
|
| 73 |
-
yield "❌ Cannot reach API server", None
|
| 74 |
-
return
|
| 75 |
-
|
| 76 |
-
try:
|
| 77 |
-
progress(0, desc="Creating task...")
|
| 78 |
-
logger.info("Creating task...")
|
| 79 |
-
|
| 80 |
-
# Log the request details
|
| 81 |
-
logger.info(f"Prompt: {prompt_text[:50]}...")
|
| 82 |
-
logger.info(f"Image URL: {image_url}")
|
| 83 |
-
|
| 84 |
-
create_result = client.content_generation.tasks.create(
|
| 85 |
-
model="seedance-1-5-pro-251215",
|
| 86 |
-
content=[
|
| 87 |
-
{
|
| 88 |
-
"type": "text",
|
| 89 |
-
"text": prompt_text
|
| 90 |
-
},
|
| 91 |
-
{
|
| 92 |
-
"type": "image_url",
|
| 93 |
-
"image_url": {
|
| 94 |
-
"url": image_url
|
| 95 |
-
}
|
| 96 |
-
}
|
| 97 |
-
]
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
task_id = create_result.id
|
| 101 |
-
logger.info(f"✅ Task created: {task_id}")
|
| 102 |
-
yield f"Task created: {task_id}", None
|
| 103 |
-
|
| 104 |
-
progress(0.3, desc="Polling...")
|
| 105 |
-
|
| 106 |
-
# Poll for results
|
| 107 |
-
attempts = 0
|
| 108 |
-
while attempts < 60:
|
| 109 |
-
try:
|
| 110 |
-
get_result = client.content_generation.tasks.get(task_id=task_id)
|
| 111 |
-
status = get_result.status
|
| 112 |
-
logger.info(f"Status: {status}")
|
| 113 |
-
|
| 114 |
-
if status == "succeeded":
|
| 115 |
-
progress(1.0, desc="Complete!")
|
| 116 |
-
video_url = get_result.output[0].get('video_url') if get_result.output else None
|
| 117 |
-
yield "Success!", video_url
|
| 118 |
-
return
|
| 119 |
-
elif status == "failed":
|
| 120 |
-
yield f"Failed: {get_result.error}", None
|
| 121 |
-
return
|
| 122 |
-
else:
|
| 123 |
-
progress(0.3 + (attempts/60)*0.7, desc=f"Status: {status}")
|
| 124 |
-
yield f"Status: {status}...", None
|
| 125 |
-
time.sleep(1)
|
| 126 |
-
attempts += 1
|
| 127 |
-
except Exception as e:
|
| 128 |
-
logger.error(f"Polling error: {e}")
|
| 129 |
-
yield f"Polling error: {e}", None
|
| 130 |
-
return
|
| 131 |
-
|
| 132 |
-
yield "Timeout", None
|
| 133 |
-
|
| 134 |
-
except Exception as e:
|
| 135 |
-
logger.error(f"❌ Error: {e}", exc_info=True)
|
| 136 |
-
yield f"Error: {str(e)}", None
|
| 137 |
-
|
| 138 |
-
# Simple interface
|
| 139 |
-
with gr.Blocks() as demo:
|
| 140 |
-
gr.Markdown("# BytePlus Video Generator")
|
| 141 |
-
|
| 142 |
-
# Show connection status
|
| 143 |
-
if connectivity_ok:
|
| 144 |
-
gr.Markdown("✅ **API Server:** Reachable")
|
| 145 |
-
else:
|
| 146 |
-
gr.Markdown("❌ **API Server:** Cannot connect - check network")
|
| 147 |
-
|
| 148 |
-
with gr.Row():
|
| 149 |
-
with gr.Column():
|
| 150 |
-
prompt = gr.Textbox(
|
| 151 |
-
label="Prompt",
|
| 152 |
-
lines=3,
|
| 153 |
-
value="At breakneck speed, drones fly through obstacles --duration 5 --camerafixed false"
|
| 154 |
-
)
|
| 155 |
-
image_url = gr.Textbox(
|
| 156 |
-
label="Image URL",
|
| 157 |
-
value="https://ark-doc.tos-ap-southeast-1.bytepluses.com/seepro_i2v%20.png"
|
| 158 |
-
)
|
| 159 |
-
btn = gr.Button("Generate", variant="primary")
|
| 160 |
-
|
| 161 |
-
with gr.Column():
|
| 162 |
-
status = gr.Textbox(label="Status", lines=5)
|
| 163 |
-
video = gr.Video(label="Generated Video")
|
| 164 |
-
|
| 165 |
-
btn.click(
|
| 166 |
-
fn=generate_video,
|
| 167 |
-
inputs=[prompt, image_url],
|
| 168 |
-
outputs=[status, video]
|
| 169 |
-
)
|
| 170 |
-
|
| 171 |
-
demo.launch(server_name="0.0.0.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|