Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ import os
|
|
8 |
# Replace with your NVIDIA API key
|
9 |
API_KEY = os.getenv("NVIDIA_API_KEY") # Ensure the key is set in Space secrets
|
10 |
invoke_url = "https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo"
|
11 |
-
|
12 |
headers = {
|
13 |
"Authorization": f"Bearer {API_KEY}",
|
14 |
"Accept": "application/json",
|
@@ -22,34 +21,34 @@ def generate_kindle_cover(prompt):
|
|
22 |
"steps": 2
|
23 |
}
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
28 |
response_body = response.json()
|
29 |
print("Response Body:", response_body) # Debugging line to inspect the response
|
30 |
-
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
image_bytes = base64.b64decode(image_data)
|
36 |
image = Image.open(BytesIO(image_bytes))
|
37 |
return image
|
38 |
-
|
39 |
-
return
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
return "No image data found in response."
|
51 |
-
else:
|
52 |
-
return f"Error: {response.status_code} - {response.text}"
|
53 |
|
54 |
# Create the Gradio interface
|
55 |
iface = gr.Interface(
|
@@ -61,4 +60,4 @@ iface = gr.Interface(
|
|
61 |
)
|
62 |
|
63 |
# Launch the Gradio interface
|
64 |
-
iface.launch()
|
|
|
8 |
# Replace with your NVIDIA API key
|
9 |
API_KEY = os.getenv("NVIDIA_API_KEY") # Ensure the key is set in Space secrets
|
10 |
invoke_url = "https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo"
|
|
|
11 |
headers = {
|
12 |
"Authorization": f"Bearer {API_KEY}",
|
13 |
"Accept": "application/json",
|
|
|
21 |
"steps": 2
|
22 |
}
|
23 |
|
24 |
+
try:
|
25 |
+
response = requests.post(invoke_url, headers=headers, json=payload)
|
26 |
+
response.raise_for_status() # Raise an exception for non-200 status codes
|
27 |
+
|
28 |
response_body = response.json()
|
29 |
print("Response Body:", response_body) # Debugging line to inspect the response
|
30 |
+
|
31 |
+
# Check for 'results' key in response
|
32 |
+
if 'results' in response_body and len(response_body['results']) > 0:
|
33 |
+
result = response_body['results'][0]
|
34 |
+
|
35 |
+
# Check for 'image' key in result
|
36 |
+
if 'image' in result:
|
37 |
+
image_data = result['image']
|
38 |
image_bytes = base64.b64decode(image_data)
|
39 |
image = Image.open(BytesIO(image_bytes))
|
40 |
return image
|
41 |
+
else:
|
42 |
+
return "No 'image' key found in the result."
|
43 |
+
else:
|
44 |
+
return "No 'results' found in the response."
|
45 |
+
|
46 |
+
except requests.exceptions.RequestException as e:
|
47 |
+
return f"Error making request: {str(e)}"
|
48 |
+
except ValueError as e:
|
49 |
+
return f"Error parsing JSON response: {str(e)}"
|
50 |
+
except Exception as e:
|
51 |
+
return f"Unexpected error: {str(e)}"
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Create the Gradio interface
|
54 |
iface = gr.Interface(
|
|
|
60 |
)
|
61 |
|
62 |
# Launch the Gradio interface
|
63 |
+
iface.launch()
|