Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,7 @@ from io import BytesIO
|
|
| 5 |
import base64
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
|
| 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}",
|
|
@@ -14,6 +13,8 @@ headers = {
|
|
| 14 |
}
|
| 15 |
|
| 16 |
def generate_kindle_cover(prompt):
|
|
|
|
|
|
|
| 17 |
payload = {
|
| 18 |
"text_prompts": [{"text": prompt}],
|
| 19 |
"seed": 0,
|
|
@@ -22,35 +23,38 @@ def generate_kindle_cover(prompt):
|
|
| 22 |
}
|
| 23 |
|
| 24 |
try:
|
|
|
|
| 25 |
response = requests.post(invoke_url, headers=headers, json=payload)
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
response_body = response.json()
|
| 29 |
-
print("Response Body:", response_body)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 52 |
|
| 53 |
-
# Create the Gradio interface
|
| 54 |
iface = gr.Interface(
|
| 55 |
fn=generate_kindle_cover,
|
| 56 |
inputs="text",
|
|
@@ -59,5 +63,4 @@ iface = gr.Interface(
|
|
| 59 |
description="Generate high-quality covers for Amazon Kindle books using the NVIDIA SDXL-Turbo model."
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
iface.launch()
|
|
|
|
| 5 |
import base64
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
API_KEY = os.getenv("NVIDIA_API_KEY")
|
|
|
|
| 9 |
invoke_url = "https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo"
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {API_KEY}",
|
|
|
|
| 13 |
}
|
| 14 |
|
| 15 |
def generate_kindle_cover(prompt):
|
| 16 |
+
print(f"Generating cover for prompt: {prompt}")
|
| 17 |
+
|
| 18 |
payload = {
|
| 19 |
"text_prompts": [{"text": prompt}],
|
| 20 |
"seed": 0,
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
try:
|
| 26 |
+
print("Sending request to NVIDIA API...")
|
| 27 |
response = requests.post(invoke_url, headers=headers, json=payload)
|
| 28 |
+
print(f"Received response with status code: {response.status_code}")
|
| 29 |
+
|
| 30 |
+
if response.status_code != 200:
|
| 31 |
+
return f"Error: Received status code {response.status_code} from API"
|
| 32 |
|
| 33 |
response_body = response.json()
|
| 34 |
+
print("Response Body:", response_body)
|
| 35 |
+
|
| 36 |
+
if 'results' not in response_body or not response_body['results']:
|
| 37 |
+
return "Error: No results found in the response"
|
| 38 |
+
|
| 39 |
+
result = response_body['results'][0]
|
| 40 |
+
if 'image' not in result:
|
| 41 |
+
return "Error: No image data found in the result"
|
| 42 |
+
|
| 43 |
+
image_data = result['image']
|
| 44 |
+
print("Image data length:", len(image_data))
|
| 45 |
+
|
| 46 |
+
image_bytes = base64.b64decode(image_data)
|
| 47 |
+
print("Decoded image bytes length:", len(image_bytes))
|
| 48 |
+
|
| 49 |
+
image = Image.open(BytesIO(image_bytes))
|
| 50 |
+
print("Image successfully created")
|
| 51 |
+
|
| 52 |
+
return image
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
+
print(f"An error occurred: {str(e)}")
|
| 56 |
+
return f"Error: {str(e)}"
|
| 57 |
|
|
|
|
| 58 |
iface = gr.Interface(
|
| 59 |
fn=generate_kindle_cover,
|
| 60 |
inputs="text",
|
|
|
|
| 63 |
description="Generate high-quality covers for Amazon Kindle books using the NVIDIA SDXL-Turbo model."
|
| 64 |
)
|
| 65 |
|
| 66 |
+
iface.launch(debug=True)
|
|
|