Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -57,10 +57,15 @@ def query_api(payload, progress_callback=None):
|
|
57 |
|
58 |
time.sleep(5) # Wait 5 seconds between polls
|
59 |
|
60 |
-
# Check status
|
61 |
-
status_response = requests.get(status_url
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if status_response.status_code != 200:
|
|
|
64 |
raise gr.Error(f"Status check failed: {status_response.status_code}")
|
65 |
|
66 |
try:
|
@@ -73,8 +78,13 @@ def query_api(payload, progress_callback=None):
|
|
73 |
if not response_url:
|
74 |
raise gr.Error("No response URL provided")
|
75 |
|
76 |
-
|
|
|
|
|
|
|
|
|
77 |
if result_response.status_code != 200:
|
|
|
78 |
raise gr.Error(f"Failed to get result: {result_response.status_code}")
|
79 |
|
80 |
# Check if result is JSON with image data
|
@@ -191,25 +201,22 @@ def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps,
|
|
191 |
progress(0.1, desc="Generating image...")
|
192 |
|
193 |
try:
|
194 |
-
# Make API request
|
195 |
-
image_bytes = query_api(payload)
|
196 |
|
197 |
-
# Try to convert response bytes to PIL Image
|
198 |
try:
|
199 |
image = Image.open(io.BytesIO(image_bytes))
|
200 |
except Exception as img_error:
|
201 |
-
print(f"Failed to open image
|
202 |
-
|
203 |
-
with open('/tmp/debug_response.bin', 'wb') as f:
|
204 |
-
f.write(image_bytes)
|
205 |
-
print(f"Saved response to /tmp/debug_response.bin for debugging")
|
206 |
|
207 |
# Try to decode as base64 if direct opening failed
|
208 |
try:
|
209 |
decoded_bytes = base64.b64decode(image_bytes)
|
210 |
image = Image.open(io.BytesIO(decoded_bytes))
|
211 |
except:
|
212 |
-
raise gr.Error(f"Could not process API response as image. Response
|
213 |
|
214 |
progress(1.0, desc="Complete!")
|
215 |
return gr.Image(value=image)
|
|
|
57 |
|
58 |
time.sleep(5) # Wait 5 seconds between polls
|
59 |
|
60 |
+
# Check status - try without auth headers first, then with auth headers
|
61 |
+
status_response = requests.get(status_url)
|
62 |
+
|
63 |
+
# If unauthorized, try with headers
|
64 |
+
if status_response.status_code == 401:
|
65 |
+
status_response = requests.get(status_url, headers=headers)
|
66 |
|
67 |
if status_response.status_code != 200:
|
68 |
+
print(f"Status response: {status_response.status_code} - {status_response.text}")
|
69 |
raise gr.Error(f"Status check failed: {status_response.status_code}")
|
70 |
|
71 |
try:
|
|
|
78 |
if not response_url:
|
79 |
raise gr.Error("No response URL provided")
|
80 |
|
81 |
+
# Try to get result without auth first, then with auth
|
82 |
+
result_response = requests.get(response_url)
|
83 |
+
if result_response.status_code == 401:
|
84 |
+
result_response = requests.get(response_url, headers=headers)
|
85 |
+
|
86 |
if result_response.status_code != 200:
|
87 |
+
print(f"Result response: {result_response.status_code} - {result_response.text}")
|
88 |
raise gr.Error(f"Failed to get result: {result_response.status_code}")
|
89 |
|
90 |
# Check if result is JSON with image data
|
|
|
201 |
progress(0.1, desc="Generating image...")
|
202 |
|
203 |
try:
|
204 |
+
# Make API request with progress callback
|
205 |
+
image_bytes = query_api(payload, progress_callback=progress)
|
206 |
|
207 |
+
# Try to convert response bytes to PIL Image
|
208 |
try:
|
209 |
image = Image.open(io.BytesIO(image_bytes))
|
210 |
except Exception as img_error:
|
211 |
+
print(f"Failed to open image: {img_error}")
|
212 |
+
print(f"Image bytes type: {type(image_bytes)}, length: {len(image_bytes) if hasattr(image_bytes, '__len__') else 'unknown'}")
|
|
|
|
|
|
|
213 |
|
214 |
# Try to decode as base64 if direct opening failed
|
215 |
try:
|
216 |
decoded_bytes = base64.b64decode(image_bytes)
|
217 |
image = Image.open(io.BytesIO(decoded_bytes))
|
218 |
except:
|
219 |
+
raise gr.Error(f"Could not process API response as image. Response length: {len(image_bytes) if hasattr(image_bytes, '__len__') else 'unknown'}")
|
220 |
|
221 |
progress(1.0, desc="Complete!")
|
222 |
return gr.Image(value=image)
|