asad commited on
Commit
9e215b3
·
verified ·
1 Parent(s): 731fe3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -1,12 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
- """S-D-XL-pipeline.ipynb
3
-
4
- Automatically generated by Colaboratory.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1iDFfGVa5XpZSZ1yzv-HeFpiB763xt67D
8
- """
9
-
10
  import requests
11
  import gradio as gr
12
  from PIL import Image
@@ -15,7 +6,6 @@ from transformers import utils
15
  utils.move_cache()
16
  import os
17
 
18
-
19
  hf_token = os.getenv('HF_TOKEN')
20
 
21
  API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
@@ -23,12 +13,24 @@ headers = {"Authorization": f"Bearer {hf_token}"}
23
 
24
  def query(payload):
25
  response = requests.post(API_URL, headers=headers, json=payload)
 
 
 
 
26
  return response.content
27
 
28
  def generate_image(prompt):
29
  image_bytes = query({"inputs": prompt})
30
- image = Image.open(io.BytesIO(image_bytes))
31
- return image
 
 
 
 
 
 
 
 
32
 
33
  iface = gr.Interface(
34
  fn=generate_image,
@@ -38,4 +40,3 @@ iface = gr.Interface(
38
  )
39
 
40
  iface.launch()
41
-
 
 
 
 
 
 
 
 
 
 
1
  import requests
2
  import gradio as gr
3
  from PIL import Image
 
6
  utils.move_cache()
7
  import os
8
 
 
9
  hf_token = os.getenv('HF_TOKEN')
10
 
11
  API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
 
13
 
14
  def query(payload):
15
  response = requests.post(API_URL, headers=headers, json=payload)
16
+ if response.status_code != 200:
17
+ # If the response is not successful, return None or handle it accordingly
18
+ print("Error from the API:", response.text) # For debugging
19
+ return None
20
  return response.content
21
 
22
  def generate_image(prompt):
23
  image_bytes = query({"inputs": prompt})
24
+ if image_bytes is None:
25
+ # Handle the case where the API did not return image data
26
+ return "The API call was unsuccessful. Check the logs for details."
27
+
28
+ try:
29
+ image = Image.open(io.BytesIO(image_bytes))
30
+ return image
31
+ except IOError:
32
+ # Handle cases where PIL cannot open the bytes received
33
+ return "The returned data could not be recognized as an image."
34
 
35
  iface = gr.Interface(
36
  fn=generate_image,
 
40
  )
41
 
42
  iface.launch()