Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,9 @@ import requests
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import json
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
API_KEY = os.getenv("NVIDIA_API_KEY")
|
| 7 |
invoke_url = "https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo"
|
|
@@ -13,41 +16,48 @@ headers = {
|
|
| 13 |
def generate_kindle_cover(prompt):
|
| 14 |
print(f"Generating cover for prompt: {prompt}")
|
| 15 |
print(f"API Key (first 5 characters): {API_KEY[:5]}..." if API_KEY else "API Key is not set")
|
| 16 |
-
|
| 17 |
payload = {
|
| 18 |
"text_prompts": [{"text": prompt}],
|
| 19 |
"seed": 0,
|
| 20 |
"sampler": "K_EULER_ANCESTRAL",
|
| 21 |
"steps": 2
|
| 22 |
}
|
| 23 |
-
|
| 24 |
try:
|
| 25 |
print("Sending request to NVIDIA API...")
|
| 26 |
response = requests.post(invoke_url, headers=headers, json=payload)
|
| 27 |
print(f"Received response with status code: {response.status_code}")
|
| 28 |
print(f"Response headers: {json.dumps(dict(response.headers), indent=2)}")
|
| 29 |
-
|
| 30 |
-
|
| 31 |
if response.status_code == 401:
|
| 32 |
-
return "Error: Unauthorized access (401). Please check your API key."
|
| 33 |
elif response.status_code != 200:
|
| 34 |
-
return f"Error: Received status code {response.status_code} from API. Response: {response.text}"
|
| 35 |
-
|
| 36 |
try:
|
| 37 |
response_body = response.json()
|
| 38 |
print("Response Body:", json.dumps(response_body, indent=2))
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except json.JSONDecodeError:
|
| 41 |
-
return f"Error: Failed to parse JSON response. Raw response: {response.text}"
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
print(f"An error occurred: {str(e)}")
|
| 45 |
-
return f"Error: {str(e)}"
|
| 46 |
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=generate_kindle_cover,
|
| 49 |
inputs="text",
|
| 50 |
-
outputs="text",
|
| 51 |
title="Kindle Cover Generator",
|
| 52 |
description="Generate high-quality covers for Amazon Kindle books using the NVIDIA SDXL-Turbo model."
|
| 53 |
)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
+
import base64
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
|
| 9 |
API_KEY = os.getenv("NVIDIA_API_KEY")
|
| 10 |
invoke_url = "https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo"
|
|
|
|
| 16 |
def generate_kindle_cover(prompt):
|
| 17 |
print(f"Generating cover for prompt: {prompt}")
|
| 18 |
print(f"API Key (first 5 characters): {API_KEY[:5]}..." if API_KEY else "API Key is not set")
|
| 19 |
+
|
| 20 |
payload = {
|
| 21 |
"text_prompts": [{"text": prompt}],
|
| 22 |
"seed": 0,
|
| 23 |
"sampler": "K_EULER_ANCESTRAL",
|
| 24 |
"steps": 2
|
| 25 |
}
|
| 26 |
+
|
| 27 |
try:
|
| 28 |
print("Sending request to NVIDIA API...")
|
| 29 |
response = requests.post(invoke_url, headers=headers, json=payload)
|
| 30 |
print(f"Received response with status code: {response.status_code}")
|
| 31 |
print(f"Response headers: {json.dumps(dict(response.headers), indent=2)}")
|
| 32 |
+
|
|
|
|
| 33 |
if response.status_code == 401:
|
| 34 |
+
return "Error: Unauthorized access (401). Please check your API key.", None
|
| 35 |
elif response.status_code != 200:
|
| 36 |
+
return f"Error: Received status code {response.status_code} from API. Response: {response.text}", None
|
| 37 |
+
|
| 38 |
try:
|
| 39 |
response_body = response.json()
|
| 40 |
print("Response Body:", json.dumps(response_body, indent=2))
|
| 41 |
+
|
| 42 |
+
# Extract and decode the image data
|
| 43 |
+
image_data = base64.b64decode(response_body['results'][0]['image'])
|
| 44 |
+
image = Image.open(io.BytesIO(image_data))
|
| 45 |
+
|
| 46 |
+
return "Success: Image generated successfully.", image
|
| 47 |
+
|
| 48 |
except json.JSONDecodeError:
|
| 49 |
+
return f"Error: Failed to parse JSON response. Raw response: {response.text}", None
|
| 50 |
+
except KeyError:
|
| 51 |
+
return "Error: Expected image data not found in the response.", None
|
| 52 |
+
|
| 53 |
except Exception as e:
|
| 54 |
print(f"An error occurred: {str(e)}")
|
| 55 |
+
return f"Error: {str(e)}", None
|
| 56 |
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=generate_kindle_cover,
|
| 59 |
inputs="text",
|
| 60 |
+
outputs=["text", gr.Image()],
|
| 61 |
title="Kindle Cover Generator",
|
| 62 |
description="Generate high-quality covers for Amazon Kindle books using the NVIDIA SDXL-Turbo model."
|
| 63 |
)
|