Update a.py
Browse files
a.py
CHANGED
|
@@ -142,6 +142,72 @@ def generate_video(image_path, prompt_text, progress=gr.Progress()):
|
|
| 142 |
else:
|
| 143 |
yield f"❌ Error: {str(e)}", None
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
def update_prompt(choice):
|
| 146 |
"""Update prompt when shot type changes"""
|
| 147 |
return DEFAULT_PROMPTS.get(choice, "")
|
|
|
|
| 142 |
else:
|
| 143 |
yield f"❌ Error: {str(e)}", None
|
| 144 |
|
| 145 |
+
# Add this new function for JSON polling fallback
|
| 146 |
+
def poll_via_json(task_id):
|
| 147 |
+
"""Fallback method: poll io.json for results"""
|
| 148 |
+
json_url = "https://1hit.no/proxy/io.json"
|
| 149 |
+
try:
|
| 150 |
+
response = requests.get(json_url)
|
| 151 |
+
data = response.json()
|
| 152 |
+
if task_id in data:
|
| 153 |
+
task_data = data[task_id]
|
| 154 |
+
if task_data.get('status') == 'succeeded':
|
| 155 |
+
return task_data.get('video_url')
|
| 156 |
+
elif task_data.get('status') == 'failed':
|
| 157 |
+
return None
|
| 158 |
+
except:
|
| 159 |
+
pass
|
| 160 |
+
return None
|
| 161 |
+
|
| 162 |
+
# Then MODIFY just the polling section in generate_video (around line 100-130)
|
| 163 |
+
# Replace the existing polling while loop with this:
|
| 164 |
+
|
| 165 |
+
# Poll for results - TRY SDK FIRST, then fallback to JSON
|
| 166 |
+
attempts = 0
|
| 167 |
+
max_attempts = 120
|
| 168 |
+
used_fallback = False
|
| 169 |
+
|
| 170 |
+
while attempts < max_attempts:
|
| 171 |
+
try:
|
| 172 |
+
# Try SDK method first
|
| 173 |
+
get_result = client.content_generation.tasks.get(task_id=task_id)
|
| 174 |
+
status = get_result.status
|
| 175 |
+
|
| 176 |
+
if status == "succeeded":
|
| 177 |
+
progress(1.0, desc="Complete!")
|
| 178 |
+
video_url = get_result.content.video_url if hasattr(get_result, 'content') else None
|
| 179 |
+
print(f"Video URL: {video_url}")
|
| 180 |
+
yield "✅ Video generated successfully!", video_url
|
| 181 |
+
return
|
| 182 |
+
|
| 183 |
+
elif status == "failed":
|
| 184 |
+
error_msg = get_result.error if hasattr(get_result, 'error') else "Unknown error"
|
| 185 |
+
yield f"❌ Failed: {error_msg}", None
|
| 186 |
+
return
|
| 187 |
+
else:
|
| 188 |
+
progress(0.3 + (attempts/max_attempts)*0.7, desc=f"Status: {status}")
|
| 189 |
+
yield f"⏳ Status: {status}... (attempt {attempts + 1})", None
|
| 190 |
+
time.sleep(1)
|
| 191 |
+
attempts += 1
|
| 192 |
+
|
| 193 |
+
except Exception as e:
|
| 194 |
+
# If SDK fails, try JSON fallback
|
| 195 |
+
if not used_fallback:
|
| 196 |
+
print("SDK polling failed, trying JSON fallback...")
|
| 197 |
+
used_fallback = True
|
| 198 |
+
|
| 199 |
+
video_url = poll_via_json(task_id)
|
| 200 |
+
if video_url:
|
| 201 |
+
yield "✅ Video generated successfully! (via JSON)", video_url
|
| 202 |
+
return
|
| 203 |
+
elif video_url is None: # Failed
|
| 204 |
+
yield "❌ Task failed (via JSON)", None
|
| 205 |
+
return
|
| 206 |
+
else: # Still processing
|
| 207 |
+
yield f"⏳ Status: processing... (JSON fallback)", None
|
| 208 |
+
time.sleep(5) # Poll JSON less frequently
|
| 209 |
+
attempts += 1
|
| 210 |
+
|
| 211 |
def update_prompt(choice):
|
| 212 |
"""Update prompt when shot type changes"""
|
| 213 |
return DEFAULT_PROMPTS.get(choice, "")
|