MySafeCode commited on
Commit
73367f6
·
verified ·
1 Parent(s): c09f712

Update app6.py

Browse files
Files changed (1) hide show
  1. app6.py +102 -30
app6.py CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
2
  import os
3
  import time
4
  import logging
 
 
5
  from byteplussdkarkruntime import Ark
6
  import byteplussdkcore
7
 
@@ -11,26 +13,74 @@ logger = logging.getLogger(__name__)
11
 
12
  # Get API key
13
  API_KEY = os.environ.get("key", "")
 
 
14
 
15
- # Configure SDK exactly as per the notice
16
- configuration = byteplussdkcore.Configuration()
17
- configuration.client_side_validation = True
18
- configuration.schema = "http" # Explicitly set to HTTP as per notice
19
- configuration.debug = False
20
- configuration.logger_file = "sdk.log"
21
- byteplussdkcore.Configuration.set_default(configuration)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  # Initialize client
24
- client = Ark(
25
- base_url="https://ark.ap-southeast.bytepluses.com/api/v3",
26
- api_key=API_KEY,
27
- )
 
 
 
 
 
28
 
29
- def generate_video(prompt_text, image_url):
30
- """Generate video using properly configured SDK"""
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  try:
33
- # Create task exactly like original code
 
 
 
 
 
 
34
  create_result = client.content_generation.tasks.create(
35
  model="seedance-1-5-pro-251215",
36
  content=[
@@ -48,31 +98,53 @@ def generate_video(prompt_text, image_url):
48
  )
49
 
50
  task_id = create_result.id
 
51
  yield f"Task created: {task_id}", None
52
 
 
 
53
  # Poll for results
54
- while True:
55
- get_result = client.content_generation.tasks.get(task_id=task_id)
56
- status = get_result.status
57
-
58
- if status == "succeeded":
59
- video_url = get_result.output[0].get('video_url') if get_result.output else None
60
- yield "Success!", video_url
61
- break
62
- elif status == "failed":
63
- yield f"Failed: {get_result.error}", None
64
- break
65
- else:
66
- yield f"Status: {status}...", None
67
- time.sleep(1)
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  except Exception as e:
 
70
  yield f"Error: {str(e)}", None
71
 
72
  # Simple interface
73
  with gr.Blocks() as demo:
74
  gr.Markdown("# BytePlus Video Generator")
75
 
 
 
 
 
 
 
76
  with gr.Row():
77
  with gr.Column():
78
  prompt = gr.Textbox(
@@ -87,7 +159,7 @@ with gr.Blocks() as demo:
87
  btn = gr.Button("Generate", variant="primary")
88
 
89
  with gr.Column():
90
- status = gr.Textbox(label="Status")
91
  video = gr.Video(label="Generated Video")
92
 
93
  btn.click(
@@ -96,4 +168,4 @@ with gr.Blocks() as demo:
96
  outputs=[status, video]
97
  )
98
 
99
- demo.launch()
 
2
  import os
3
  import time
4
  import logging
5
+ import socket
6
+ import requests
7
  from byteplussdkarkruntime import Ark
8
  import byteplussdkcore
9
 
 
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=[
 
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(
 
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(
 
168
  outputs=[status, video]
169
  )
170
 
171
+ demo.launch(server_name="0.0.0.0")