ms1449 commited on
Commit
7746712
·
verified ·
1 Parent(s): 7805a11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -35
app.py CHANGED
@@ -1,10 +1,7 @@
1
  import requests
2
  import gradio as gr
3
- from PIL import Image
4
- from io import BytesIO
5
- import base64
6
  import os
7
- import time
8
 
9
  API_KEY = os.getenv("NVIDIA_API_KEY")
10
  invoke_url = "https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo"
@@ -15,6 +12,7 @@ headers = {
15
 
16
  def generate_kindle_cover(prompt):
17
  print(f"Generating cover for prompt: {prompt}")
 
18
 
19
  payload = {
20
  "text_prompts": [{"text": prompt}],
@@ -27,36 +25,20 @@ def generate_kindle_cover(prompt):
27
  print("Sending request to NVIDIA API...")
28
  response = requests.post(invoke_url, headers=headers, json=payload)
29
  print(f"Received response with status code: {response.status_code}")
30
-
31
- if response.status_code != 200:
32
- return f"Error: Received status code {response.status_code} from API"
33
-
34
- response_body = response.json()
35
- print("Response Body:", response_body)
36
-
37
- if 'results' not in response_body or not response_body['results']:
38
- return "Error: No results found in the response"
39
-
40
- result = response_body['results'][0]
41
- if 'image' not in result:
42
- return "Error: No image data found in the result"
43
-
44
- image_data = result['image']
45
- print("Image data length:", len(image_data))
46
-
47
- image_bytes = base64.b64decode(image_data)
48
- print("Decoded image bytes length:", len(image_bytes))
49
-
50
- image = Image.open(BytesIO(image_bytes))
51
- print("Image successfully created")
52
-
53
- # Save the image
54
- timestamp = int(time.time())
55
- filename = f"kindle_cover_{timestamp}.png"
56
- image.save(filename)
57
- print(f"Image saved as {filename}")
58
-
59
- return filename
60
 
61
  except Exception as e:
62
  print(f"An error occurred: {str(e)}")
@@ -65,7 +47,7 @@ def generate_kindle_cover(prompt):
65
  iface = gr.Interface(
66
  fn=generate_kindle_cover,
67
  inputs="text",
68
- outputs="file",
69
  title="Kindle Cover Generator",
70
  description="Generate high-quality covers for Amazon Kindle books using the NVIDIA SDXL-Turbo model."
71
  )
 
1
  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"
 
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}],
 
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
+ print(f"Response content: {response.text}")
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
+ return f"Success: API responded with status code 200. Check console for full response details."
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)}")
 
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
  )