Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,47 +5,37 @@ import base64
|
|
5 |
from io import BytesIO
|
6 |
|
7 |
def query_hf_image_generation(api_key, prompt):
|
8 |
-
API_URL =
|
9 |
headers = {
|
10 |
"Authorization": f"Bearer {api_key}",
|
11 |
"Content-Type": "application/json"
|
12 |
}
|
13 |
-
data = {
|
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
|
23 |
|
24 |
-
# Try parsing JSON response
|
25 |
try:
|
26 |
result = response.json()
|
27 |
-
except ValueError
|
28 |
-
return f"Error decoding JSON: {
|
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 |
-
|
41 |
-
base64_data =
|
42 |
-
|
43 |
-
image = Image.open(BytesIO(
|
44 |
return image
|
45 |
except Exception as e:
|
46 |
return f"Error processing image data: {e}"
|
47 |
else:
|
48 |
-
return "Error: 'data'
|
49 |
|
50 |
iface = gr.Interface(
|
51 |
fn=query_hf_image_generation,
|
@@ -58,4 +48,4 @@ iface = gr.Interface(
|
|
58 |
description="Enter your API Key and a prompt to generate an image using the Stable Diffusion XL model from Hugging Face."
|
59 |
)
|
60 |
|
61 |
-
iface.launch()
|
|
|
5 |
from io import BytesIO
|
6 |
|
7 |
def query_hf_image_generation(api_key, prompt):
|
8 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
9 |
headers = {
|
10 |
"Authorization": f"Bearer {api_key}",
|
11 |
"Content-Type": "application/json"
|
12 |
}
|
13 |
+
data = {"inputs": prompt}
|
|
|
|
|
14 |
|
|
|
15 |
response = requests.post(API_URL, headers=headers, json=data)
|
16 |
|
|
|
17 |
if response.status_code != 200:
|
18 |
+
return f"Error: Received HTTP {response.status_code} - {response.text}"
|
19 |
|
|
|
20 |
try:
|
21 |
result = response.json()
|
22 |
+
except ValueError:
|
23 |
+
return f"Error decoding JSON: Unexpected response format {response.text}"
|
|
|
|
|
|
|
24 |
|
|
|
25 |
if 'error' in result:
|
26 |
return f"Error: {result['error']}"
|
27 |
|
|
|
28 |
if 'data' in result:
|
29 |
try:
|
30 |
+
base64_string = result['data'][0]
|
31 |
+
base64_data = base64_string.split(",")[1] if "," in base64_string else base64_string
|
32 |
+
image_data = base64.b64decode(base64_data)
|
33 |
+
image = Image.open(BytesIO(image_data))
|
34 |
return image
|
35 |
except Exception as e:
|
36 |
return f"Error processing image data: {e}"
|
37 |
else:
|
38 |
+
return "Error: Missing 'data' in the response."
|
39 |
|
40 |
iface = gr.Interface(
|
41 |
fn=query_hf_image_generation,
|
|
|
48 |
description="Enter your API Key and a prompt to generate an image using the Stable Diffusion XL model from Hugging Face."
|
49 |
)
|
50 |
|
51 |
+
iface.launch(share=True)
|