ms1449 commited on
Commit
e4a7911
·
verified ·
1 Parent(s): 56e1aeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -8,7 +8,6 @@ import os
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
-
12
  headers = {
13
  "Authorization": f"Bearer {API_KEY}",
14
  "Accept": "application/json",
@@ -22,34 +21,34 @@ def generate_kindle_cover(prompt):
22
  "steps": 2
23
  }
24
 
25
- response = requests.post(invoke_url, headers=headers, json=payload)
26
-
27
- if response.status_code == 200:
 
28
  response_body = response.json()
29
  print("Response Body:", response_body) # Debugging line to inspect the response
30
-
31
- # Adjust based on actual response structure
32
- image_data = response_body.get('image_base64') # Replace 'image_base64' if necessary
33
- if image_data:
34
- try:
 
 
 
35
  image_bytes = base64.b64decode(image_data)
36
  image = Image.open(BytesIO(image_bytes))
37
  return image
38
- except Exception as e:
39
- return f"Error decoding base64 image: {e}"
40
-
41
- image_url = response_body.get('image_url') # Replace 'image_url' if necessary
42
- if image_url:
43
- try:
44
- image_response = requests.get(image_url)
45
- image = Image.open(BytesIO(image_response.content))
46
- return image
47
- except Exception as e:
48
- return f"Error loading image from URL: {e}"
49
-
50
- return "No image data found in response."
51
- else:
52
- return f"Error: {response.status_code} - {response.text}"
53
 
54
  # Create the Gradio interface
55
  iface = gr.Interface(
@@ -61,4 +60,4 @@ iface = gr.Interface(
61
  )
62
 
63
  # Launch the Gradio interface
64
- iface.launch()
 
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}",
13
  "Accept": "application/json",
 
21
  "steps": 2
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(
 
60
  )
61
 
62
  # Launch the Gradio interface
63
+ iface.launch()