Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -14,26 +14,36 @@ def query_hf_image_generation(api_key, prompt):
|
|
14 |
"inputs": prompt
|
15 |
}
|
16 |
|
|
|
17 |
response = requests.post(API_URL, headers=headers, json=data)
|
|
|
|
|
18 |
if response.status_code != 200:
|
19 |
-
return "Error:
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
# Debug output to help figure out what result looks like
|
24 |
print("DEBUG:", result)
|
25 |
|
26 |
-
# Check if the API response contains an error.
|
27 |
if 'error' in result:
|
28 |
-
return "Error:
|
29 |
|
30 |
# Assuming the API returns an image in base64 format.
|
31 |
if 'data' in result:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
else:
|
38 |
return "Error: 'data' not found in response."
|
39 |
|
|
|
14 |
"inputs": prompt
|
15 |
}
|
16 |
|
17 |
+
# Make the request
|
18 |
response = requests.post(API_URL, headers=headers, json=data)
|
19 |
+
|
20 |
+
# Check if the response was successful
|
21 |
if response.status_code != 200:
|
22 |
+
return f"Error: Received status code {response.status_code} with message: {response.text}"
|
23 |
+
|
24 |
+
# Try parsing JSON response
|
25 |
+
try:
|
26 |
+
result = response.json()
|
27 |
+
except ValueError as e:
|
28 |
+
return f"Error decoding JSON: {e}"
|
29 |
|
30 |
+
# Debug output to diagnose the structure of the returned 'result'
|
|
|
|
|
31 |
print("DEBUG:", result)
|
32 |
|
33 |
+
# Check if the API response contains an error message.
|
34 |
if 'error' in result:
|
35 |
+
return f"Error: {result['error']}"
|
36 |
|
37 |
# Assuming the API returns an image in base64 format.
|
38 |
if 'data' in result:
|
39 |
+
try:
|
40 |
+
base64_image = result['data'][0]
|
41 |
+
base64_data = base64_image.split(',')[1] if ',' in base64_image else base64_image
|
42 |
+
image_bytes = base64.b64decode(base64_data)
|
43 |
+
image = Image.open(BytesIO(image_bytes))
|
44 |
+
return image
|
45 |
+
except Exception as e:
|
46 |
+
return f"Error processing image data: {e}"
|
47 |
else:
|
48 |
return "Error: 'data' not found in response."
|
49 |
|