Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -58,19 +58,109 @@ def query_api(payload, progress_callback=None):
|
|
58 |
|
59 |
# Check if job was queued
|
60 |
if json_response.get("status") == "IN_QUEUE":
|
61 |
-
#
|
62 |
-
|
63 |
-
|
64 |
-
progress_callback(0.5, "Processing in queue...")
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
time.sleep(10) # Wait 10 seconds for processing
|
69 |
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
elif json_response.get("status") == "COMPLETED":
|
76 |
# Job completed immediately
|
@@ -80,11 +170,17 @@ def query_api(payload, progress_callback=None):
|
|
80 |
if progress_callback:
|
81 |
progress_callback(0.9, "Downloading result...")
|
82 |
img_response = requests.get(image_info['url'])
|
|
|
|
|
83 |
return img_response.content
|
84 |
elif isinstance(image_info, str):
|
85 |
# Base64 encoded image
|
|
|
|
|
86 |
return base64.b64decode(image_info)
|
87 |
elif 'image' in json_response:
|
|
|
|
|
88 |
return base64.b64decode(json_response['image'])
|
89 |
else:
|
90 |
raise gr.Error(f"No images found in response: {json_response}")
|
|
|
58 |
|
59 |
# Check if job was queued
|
60 |
if json_response.get("status") == "IN_QUEUE":
|
61 |
+
# Get the status and response URLs provided by the API
|
62 |
+
status_url = json_response.get("status_url")
|
63 |
+
response_url = json_response.get("response_url")
|
|
|
64 |
|
65 |
+
if not status_url or not response_url:
|
66 |
+
raise gr.Error("Missing status_url or response_url in queue response")
|
|
|
67 |
|
68 |
+
# For fal.ai queue, we need to use direct authentication
|
69 |
+
fal_headers = {
|
70 |
+
"Authorization": f"Key {os.getenv('HF_TOKEN')}",
|
71 |
+
"Content-Type": "application/json"
|
72 |
+
}
|
73 |
+
|
74 |
+
# Poll for completion using the provided URLs
|
75 |
+
max_attempts = 60 # Wait up to 5 minutes
|
76 |
+
attempt = 0
|
77 |
+
|
78 |
+
while attempt < max_attempts:
|
79 |
+
if progress_callback:
|
80 |
+
progress_callback(0.1 + (attempt / max_attempts) * 0.8, f"Processing... (attempt {attempt + 1}/60)")
|
81 |
+
|
82 |
+
time.sleep(5) # Wait 5 seconds between polls
|
83 |
+
|
84 |
+
# Check status using the provided status_url
|
85 |
+
status_response = requests.get(status_url, headers=fal_headers)
|
86 |
+
|
87 |
+
if status_response.status_code != 200:
|
88 |
+
print(f"Status response: {status_response.status_code} - {status_response.text}")
|
89 |
+
attempt += 1
|
90 |
+
continue
|
91 |
+
|
92 |
+
try:
|
93 |
+
status_data = status_response.json()
|
94 |
+
print(f"Status check {attempt + 1}: {status_data}")
|
95 |
+
|
96 |
+
if status_data.get("status") == "COMPLETED":
|
97 |
+
# Job completed, get the result using response_url
|
98 |
+
result_response = requests.get(response_url, headers=fal_headers)
|
99 |
+
|
100 |
+
if result_response.status_code != 200:
|
101 |
+
print(f"Result response: {result_response.status_code} - {result_response.text}")
|
102 |
+
raise gr.Error(f"Failed to get result: {result_response.status_code}")
|
103 |
+
|
104 |
+
# Check if result is direct image bytes or JSON
|
105 |
+
result_content_type = result_response.headers.get('content-type', '').lower()
|
106 |
+
if 'image/' in result_content_type:
|
107 |
+
# Direct image bytes
|
108 |
+
if progress_callback:
|
109 |
+
progress_callback(1.0, "Complete!")
|
110 |
+
return result_response.content
|
111 |
+
else:
|
112 |
+
# Try to parse as JSON for image URL or base64
|
113 |
+
try:
|
114 |
+
result_data = result_response.json()
|
115 |
+
print(f"Result data: {result_data}")
|
116 |
+
|
117 |
+
# Look for images in various formats
|
118 |
+
if 'images' in result_data and len(result_data['images']) > 0:
|
119 |
+
image_info = result_data['images'][0]
|
120 |
+
if isinstance(image_info, dict) and 'url' in image_info:
|
121 |
+
# Download the image
|
122 |
+
if progress_callback:
|
123 |
+
progress_callback(0.9, "Downloading result...")
|
124 |
+
img_response = requests.get(image_info['url'])
|
125 |
+
if progress_callback:
|
126 |
+
progress_callback(1.0, "Complete!")
|
127 |
+
return img_response.content
|
128 |
+
elif isinstance(image_info, str):
|
129 |
+
# Base64 encoded
|
130 |
+
if progress_callback:
|
131 |
+
progress_callback(1.0, "Complete!")
|
132 |
+
return base64.b64decode(image_info)
|
133 |
+
elif 'image' in result_data:
|
134 |
+
# Single image field
|
135 |
+
if isinstance(result_data['image'], str):
|
136 |
+
if progress_callback:
|
137 |
+
progress_callback(1.0, "Complete!")
|
138 |
+
return base64.b64decode(result_data['image'])
|
139 |
+
else:
|
140 |
+
# Maybe it's direct image bytes
|
141 |
+
if progress_callback:
|
142 |
+
progress_callback(1.0, "Complete!")
|
143 |
+
return result_response.content
|
144 |
+
|
145 |
+
except requests.exceptions.JSONDecodeError:
|
146 |
+
# Result might be direct image bytes
|
147 |
+
if progress_callback:
|
148 |
+
progress_callback(1.0, "Complete!")
|
149 |
+
return result_response.content
|
150 |
+
|
151 |
+
elif status_data.get("status") == "FAILED":
|
152 |
+
error_msg = status_data.get("error", "Unknown error")
|
153 |
+
raise gr.Error(f"Job failed: {error_msg}")
|
154 |
+
|
155 |
+
# Still processing, continue polling
|
156 |
+
attempt += 1
|
157 |
+
|
158 |
+
except requests.exceptions.JSONDecodeError:
|
159 |
+
print("Failed to parse status response, continuing...")
|
160 |
+
attempt += 1
|
161 |
+
continue
|
162 |
+
|
163 |
+
raise gr.Error("Job timed out after 5 minutes")
|
164 |
|
165 |
elif json_response.get("status") == "COMPLETED":
|
166 |
# Job completed immediately
|
|
|
170 |
if progress_callback:
|
171 |
progress_callback(0.9, "Downloading result...")
|
172 |
img_response = requests.get(image_info['url'])
|
173 |
+
if progress_callback:
|
174 |
+
progress_callback(1.0, "Complete!")
|
175 |
return img_response.content
|
176 |
elif isinstance(image_info, str):
|
177 |
# Base64 encoded image
|
178 |
+
if progress_callback:
|
179 |
+
progress_callback(1.0, "Complete!")
|
180 |
return base64.b64decode(image_info)
|
181 |
elif 'image' in json_response:
|
182 |
+
if progress_callback:
|
183 |
+
progress_callback(1.0, "Complete!")
|
184 |
return base64.b64decode(json_response['image'])
|
185 |
else:
|
186 |
raise gr.Error(f"No images found in response: {json_response}")
|