Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
import requests
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
|
|
|
|
|
|
| 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"
|
| 8 |
headers = {
|
|
@@ -11,9 +15,6 @@ headers = {
|
|
| 11 |
}
|
| 12 |
|
| 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,
|
|
@@ -22,34 +23,36 @@ def generate_kindle_cover(prompt):
|
|
| 22 |
}
|
| 23 |
|
| 24 |
try:
|
| 25 |
-
print("Sending request to NVIDIA API...")
|
| 26 |
response = requests.post(invoke_url, headers=headers, json=payload)
|
| 27 |
-
|
| 28 |
-
print(f"Response headers: {json.dumps(dict(response.headers), indent=2)}")
|
| 29 |
-
print(f"Response content: {response.text}")
|
| 30 |
|
| 31 |
-
|
| 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 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
-
|
| 45 |
-
return f"Error: {str(e)}"
|
| 46 |
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=generate_kindle_cover,
|
| 49 |
inputs="text",
|
| 50 |
-
outputs="
|
| 51 |
title="Kindle Cover Generator",
|
| 52 |
description="Generate high-quality covers for Amazon Kindle books using the NVIDIA SDXL-Turbo model."
|
| 53 |
)
|
| 54 |
|
| 55 |
-
iface.launch(
|
|
|
|
| 1 |
import requests
|
| 2 |
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import base64
|
| 6 |
import json
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
# Get the API key from environment variable
|
| 10 |
API_KEY = os.getenv("NVIDIA_API_KEY")
|
| 11 |
invoke_url = "https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo"
|
| 12 |
headers = {
|
|
|
|
| 15 |
}
|
| 16 |
|
| 17 |
def generate_kindle_cover(prompt):
|
|
|
|
|
|
|
|
|
|
| 18 |
payload = {
|
| 19 |
"text_prompts": [{"text": prompt}],
|
| 20 |
"seed": 0,
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
try:
|
|
|
|
| 26 |
response = requests.post(invoke_url, headers=headers, json=payload)
|
| 27 |
+
response.raise_for_status()
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
response_body = response.json()
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
if 'results' in response_body and len(response_body['results']) > 0:
|
| 32 |
+
result = response_body['results'][0]
|
| 33 |
+
if 'image' in result:
|
| 34 |
+
image_data = result['image']
|
| 35 |
+
image_bytes = base64.b64decode(image_data)
|
| 36 |
+
image = Image.open(BytesIO(image_bytes))
|
| 37 |
+
return image
|
| 38 |
+
else:
|
| 39 |
+
return "No image data found in the result."
|
| 40 |
+
else:
|
| 41 |
+
return "No results found in the response."
|
| 42 |
|
| 43 |
+
except requests.exceptions.RequestException as e:
|
| 44 |
+
return f"Error making request: {str(e)}"
|
| 45 |
+
except json.JSONDecodeError:
|
| 46 |
+
return "Error parsing JSON response."
|
| 47 |
except Exception as e:
|
| 48 |
+
return f"Unexpected error: {str(e)}"
|
|
|
|
| 49 |
|
| 50 |
iface = gr.Interface(
|
| 51 |
fn=generate_kindle_cover,
|
| 52 |
inputs="text",
|
| 53 |
+
outputs="image",
|
| 54 |
title="Kindle Cover Generator",
|
| 55 |
description="Generate high-quality covers for Amazon Kindle books using the NVIDIA SDXL-Turbo model."
|
| 56 |
)
|
| 57 |
|
| 58 |
+
iface.launch()
|