Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,49 @@ def query_api(payload):
|
|
| 31 |
if response.status_code != 200:
|
| 32 |
raise gr.Error(f"API request failed with status {response.status_code}: {response.text}")
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# --- Core Inference Function for ChatInterface ---
|
| 37 |
def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps, progress=gr.Progress()):
|
|
@@ -93,12 +135,29 @@ def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps,
|
|
| 93 |
# Make API request
|
| 94 |
image_bytes = query_api(payload)
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
progress(1.0, desc="Complete!")
|
| 100 |
return gr.Image(value=image)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
raise gr.Error(f"Failed to generate image: {str(e)}")
|
| 104 |
|
|
|
|
| 31 |
if response.status_code != 200:
|
| 32 |
raise gr.Error(f"API request failed with status {response.status_code}: {response.text}")
|
| 33 |
|
| 34 |
+
# Debug: Check response content type and first few bytes
|
| 35 |
+
print(f"Response status: {response.status_code}")
|
| 36 |
+
print(f"Response headers: {dict(response.headers)}")
|
| 37 |
+
print(f"Response content type: {response.headers.get('content-type', 'unknown')}")
|
| 38 |
+
print(f"Response content length: {len(response.content)}")
|
| 39 |
+
print(f"First 200 chars of response: {response.content[:200]}")
|
| 40 |
+
|
| 41 |
+
# Check if response is JSON (error case) or binary (image case)
|
| 42 |
+
content_type = response.headers.get('content-type', '').lower()
|
| 43 |
+
|
| 44 |
+
if 'application/json' in content_type:
|
| 45 |
+
# Response is JSON, might contain base64 image or error
|
| 46 |
+
try:
|
| 47 |
+
json_response = response.json()
|
| 48 |
+
print(f"JSON response: {json_response}")
|
| 49 |
+
|
| 50 |
+
# Check if there's a base64 image in the response
|
| 51 |
+
if 'image' in json_response:
|
| 52 |
+
# Decode base64 image
|
| 53 |
+
image_data = base64.b64decode(json_response['image'])
|
| 54 |
+
return image_data
|
| 55 |
+
elif 'images' in json_response and len(json_response['images']) > 0:
|
| 56 |
+
# Multiple images, take the first one
|
| 57 |
+
image_data = base64.b64decode(json_response['images'][0])
|
| 58 |
+
return image_data
|
| 59 |
+
else:
|
| 60 |
+
raise gr.Error(f"Unexpected JSON response format: {json_response}")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
raise gr.Error(f"Failed to parse JSON response: {str(e)}")
|
| 63 |
+
|
| 64 |
+
elif 'image/' in content_type:
|
| 65 |
+
# Response is direct image bytes
|
| 66 |
+
return response.content
|
| 67 |
+
|
| 68 |
+
else:
|
| 69 |
+
# Try to decode as base64 first, then as direct bytes
|
| 70 |
+
try:
|
| 71 |
+
# Maybe the entire response is base64 encoded
|
| 72 |
+
image_data = base64.b64decode(response.content)
|
| 73 |
+
return image_data
|
| 74 |
+
except:
|
| 75 |
+
# Return as-is and let PIL try to handle it
|
| 76 |
+
return response.content
|
| 77 |
|
| 78 |
# --- Core Inference Function for ChatInterface ---
|
| 79 |
def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps, progress=gr.Progress()):
|
|
|
|
| 135 |
# Make API request
|
| 136 |
image_bytes = query_api(payload)
|
| 137 |
|
| 138 |
+
# Try to convert response bytes to PIL Image with better error handling
|
| 139 |
+
try:
|
| 140 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 141 |
+
except Exception as img_error:
|
| 142 |
+
print(f"Failed to open image directly: {img_error}")
|
| 143 |
+
# Maybe it's a different format, try to save and examine
|
| 144 |
+
with open('/tmp/debug_response.bin', 'wb') as f:
|
| 145 |
+
f.write(image_bytes)
|
| 146 |
+
print(f"Saved response to /tmp/debug_response.bin for debugging")
|
| 147 |
+
|
| 148 |
+
# Try to decode as base64 if direct opening failed
|
| 149 |
+
try:
|
| 150 |
+
decoded_bytes = base64.b64decode(image_bytes)
|
| 151 |
+
image = Image.open(io.BytesIO(decoded_bytes))
|
| 152 |
+
except:
|
| 153 |
+
raise gr.Error(f"Could not process API response as image. Response type: {type(image_bytes)}, Length: {len(image_bytes) if isinstance(image_bytes, (bytes, str)) else 'unknown'}")
|
| 154 |
|
| 155 |
progress(1.0, desc="Complete!")
|
| 156 |
return gr.Image(value=image)
|
| 157 |
|
| 158 |
+
except gr.Error:
|
| 159 |
+
# Re-raise gradio errors as-is
|
| 160 |
+
raise
|
| 161 |
except Exception as e:
|
| 162 |
raise gr.Error(f"Failed to generate image: {str(e)}")
|
| 163 |
|