ms1449 commited on
Commit
03b6378
·
verified ·
1 Parent(s): e4a7911

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -5,8 +5,7 @@ from io import BytesIO
5
  import base64
6
  import os
7
 
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}",
@@ -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
- 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(
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
- # Launch the Gradio interface
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)