MySafeCode commited on
Commit
f48af90
·
verified ·
1 Parent(s): a057f2d

Update a.py

Browse files
Files changed (1) hide show
  1. a.py +6 -115
a.py CHANGED
@@ -4,137 +4,28 @@ import gradio as gr
4
  from byteplussdkarkruntime import Ark
5
  import httpx
6
  import socket
 
7
 
8
  # Get API key from Hugging Face secret "Key"
9
  API_KEY = os.environ.get("Key", "")
10
- #base_url="https://ark.ap-southeast.bytepluses.com/api/v3"
11
- # Initialize client with better timeout and retry settings
12
- client = Ark(
13
- base_url="https://1hit.no/proxy/proxy.php",
14
- api_key=API_KEY,
15
- timeout=30.0, # Shorter timeout to fail faster
16
- max_retries=3,
17
- )
18
-
19
- # Fresh new prompts
20
- DEFAULT_PROMPTS = {
21
- "Cinematic Nature": "Majestic drone shot soaring above misty mountains at golden hour, sunlight breaking through clouds, cinematic 4k quality, smooth motion --duration 5 --camerafixed false",
22
-
23
- "Urban Exploration": "Dynamic drone flight through narrow alleyways of a neon-lit Tokyo street at night, rain-slicked surfaces reflecting lights, cyberpunk aesthetic --duration 5 --camerafixed false",
24
-
25
- "Action Sequence": "High-speed drone chasing a sports car through a winding coastal road, dramatic cliffside views, action movie style --duration 5 --camerafixed false",
26
-
27
- "Abstract Art": "Surreal drone flight through floating geometric shapes in a dreamlike void, pastel colors, ethereal atmosphere --duration 5 --camerafixed false",
28
-
29
- "Wildlife Documentary": "Drone following a herd of elephants across the African savanna at sunset, National Geographic style, majestic wildlife cinematography --duration 5 --camerafixed false",
30
-
31
- "Sports Highlight": "Drone racing alongside professional skiers carving through fresh powder in the Alps, high-energy sports broadcast style --duration 5 --camerafixed false",
32
-
33
- "Beach Paradise": "Drone gliding over turquoise waters and white sand beaches, palm trees swaying, tropical paradise aerial view --duration 5 --camerafixed false",
34
-
35
- "Ancient Temple": "Drone circling around ancient Angkor Wat temple at dawn, mystical atmosphere, historical documentary style --duration 5 --camerafixed false"
36
- }
37
-
38
- def test_connection():
39
- """Test if we can reach the API before trying"""
40
- try:
41
- socket.gethostbyname('ark.ap-southeast.bytepluses.com')
42
- return True, "DNS resolved"
43
- except Exception as e:
44
- return False, f"DNS failed: {e}"
45
-
46
- def generate_video(image_path, prompt_text, progress=gr.Progress()):
47
- """Generate video with robust error handling"""
48
-
49
- # Test connection first
50
- conn_ok, conn_msg = test_connection()
51
- if not conn_ok:
52
- yield f"❌ Connection Error: {conn_msg}. This might be a network restriction from Hugging Face.", None
53
- return
54
-
55
- if not API_KEY:
56
- yield "❌ API Key not configured. Please add 'Key' secret.", None
57
- return
58
-
59
- if image_path is None:
60
- yield "⚠️ Please upload an image first", None
61
- return
62
-
63
- try:
64
- progress(0, desc="Preparing image...")
65
-
66
- # Get HF temp URL
67
- image_url = f"/file={image_path}"
68
- print(f"Using image URL: {image_url}")
69
-
70
- yield "✅ Image ready! Creating video request...", None
71
-
72
- progress(0.2, desc="Creating request...")
73
-
74
- # Create task with timeout
75
- try:
76
- create_result = client.content_generation.tasks.create(
77
- model="seedance-1-5-pro-251215",
78
- content=[
79
- {
80
- "type": "text",
81
- "text": prompt_text
82
- },
83
- {
84
- "type": "image_url",
85
- "image_url": {
86
- "url": image_url
87
- }
88
- }
89
- ]
90
- )
91
- except Exception as e:
92
- error_str = str(e)
93
- if "Connection error" in error_str or "202602" in error_str:
94
- yield f"❌ Network Error: Cannot reach BytePlus API. This is likely a Hugging Face network restriction.", None
95
- else:
96
- yield f"❌ API Error: {error_str}", None
97
- return
98
-
99
- task_id = create_result.id
100
- print(f"Task created: {task_id}")
101
- yield f"✅ Task created: {task_id}", None
102
-
103
- progress(0.3, desc="Polling for results...")
104
- import time
105
- import gradio as gr
106
- from byteplussdkarkruntime import Ark
107
- import httpx
108
- import socket
109
- import requests # Added for JSON polling
110
 
111
- # Get API key from Hugging Face secret "Key"
112
- API_KEY = os.environ.get("Key", "")
113
- #base_url="https://ark.ap-southeast.bytepluses.com/api/v3"
114
- # Initialize client with better timeout and retry settings
115
  client = Ark(
116
  base_url="https://1hit.no/proxy/proxy.php",
117
  api_key=API_KEY,
118
- timeout=30.0, # Shorter timeout to fail faster
119
  max_retries=3,
120
  )
121
 
122
  # Fresh new prompts
123
  DEFAULT_PROMPTS = {
124
  "Cinematic Nature": "Majestic drone shot soaring above misty mountains at golden hour, sunlight breaking through clouds, cinematic 4k quality, smooth motion --duration 5 --camerafixed false",
125
-
126
  "Urban Exploration": "Dynamic drone flight through narrow alleyways of a neon-lit Tokyo street at night, rain-slicked surfaces reflecting lights, cyberpunk aesthetic --duration 5 --camerafixed false",
127
-
128
  "Action Sequence": "High-speed drone chasing a sports car through a winding coastal road, dramatic cliffside views, action movie style --duration 5 --camerafixed false",
129
-
130
  "Abstract Art": "Surreal drone flight through floating geometric shapes in a dreamlike void, pastel colors, ethereal atmosphere --duration 5 --camerafixed false",
131
-
132
  "Wildlife Documentary": "Drone following a herd of elephants across the African savanna at sunset, National Geographic style, majestic wildlife cinematography --duration 5 --camerafixed false",
133
-
134
  "Sports Highlight": "Drone racing alongside professional skiers carving through fresh powder in the Alps, high-energy sports broadcast style --duration 5 --camerafixed false",
135
-
136
  "Beach Paradise": "Drone gliding over turquoise waters and white sand beaches, palm trees swaying, tropical paradise aerial view --duration 5 --camerafixed false",
137
-
138
  "Ancient Temple": "Drone circling around ancient Angkor Wat temple at dawn, mystical atmosphere, historical documentary style --duration 5 --camerafixed false"
139
  }
140
 
@@ -259,12 +150,12 @@ def generate_video(image_path, prompt_text, progress=gr.Progress()):
259
  if video_url:
260
  yield "✅ Video generated successfully! (via JSON)", video_url
261
  return
262
- elif video_url is None: # Failed
263
  yield "❌ Task failed (via JSON)", None
264
  return
265
- else: # Still processing
266
  yield f"⏳ Status: processing... (JSON fallback)", None
267
- time.sleep(5) # Poll JSON less frequently
268
  attempts += 1
269
 
270
  yield "⏰ Timeout after 2 minutes", None
 
4
  from byteplussdkarkruntime import Ark
5
  import httpx
6
  import socket
7
+ import requests
8
 
9
  # Get API key from Hugging Face secret "Key"
10
  API_KEY = os.environ.get("Key", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Initialize client with proxy
 
 
 
13
  client = Ark(
14
  base_url="https://1hit.no/proxy/proxy.php",
15
  api_key=API_KEY,
16
+ timeout=30.0,
17
  max_retries=3,
18
  )
19
 
20
  # Fresh new prompts
21
  DEFAULT_PROMPTS = {
22
  "Cinematic Nature": "Majestic drone shot soaring above misty mountains at golden hour, sunlight breaking through clouds, cinematic 4k quality, smooth motion --duration 5 --camerafixed false",
 
23
  "Urban Exploration": "Dynamic drone flight through narrow alleyways of a neon-lit Tokyo street at night, rain-slicked surfaces reflecting lights, cyberpunk aesthetic --duration 5 --camerafixed false",
 
24
  "Action Sequence": "High-speed drone chasing a sports car through a winding coastal road, dramatic cliffside views, action movie style --duration 5 --camerafixed false",
 
25
  "Abstract Art": "Surreal drone flight through floating geometric shapes in a dreamlike void, pastel colors, ethereal atmosphere --duration 5 --camerafixed false",
 
26
  "Wildlife Documentary": "Drone following a herd of elephants across the African savanna at sunset, National Geographic style, majestic wildlife cinematography --duration 5 --camerafixed false",
 
27
  "Sports Highlight": "Drone racing alongside professional skiers carving through fresh powder in the Alps, high-energy sports broadcast style --duration 5 --camerafixed false",
 
28
  "Beach Paradise": "Drone gliding over turquoise waters and white sand beaches, palm trees swaying, tropical paradise aerial view --duration 5 --camerafixed false",
 
29
  "Ancient Temple": "Drone circling around ancient Angkor Wat temple at dawn, mystical atmosphere, historical documentary style --duration 5 --camerafixed false"
30
  }
31
 
 
150
  if video_url:
151
  yield "✅ Video generated successfully! (via JSON)", video_url
152
  return
153
+ elif video_url is None:
154
  yield "❌ Task failed (via JSON)", None
155
  return
156
+ else:
157
  yield f"⏳ Status: processing... (JSON fallback)", None
158
+ time.sleep(5)
159
  attempts += 1
160
 
161
  yield "⏰ Timeout after 2 minutes", None