SrirakshaKR commited on
Commit
4efbd40
·
verified ·
1 Parent(s): 1f8d416

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -5
app.py CHANGED
@@ -1,15 +1,37 @@
1
  import requests
 
 
 
2
 
3
  API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
4
  headers = {"Authorization": "Bearer HF_API_KEY"}
5
 
6
  def query(payload):
7
  response = requests.post(API_URL, headers=headers, json=payload)
8
- return response.content
 
 
 
 
 
 
 
 
 
 
 
 
9
  image_bytes = query({
10
  "inputs": "Astronaut riding a horse",
11
  })
12
- # You can access the image with PIL.Image for example
13
- import io
14
- from PIL import Image
15
- image = Image.open(io.BytesIO(image_bytes))
 
 
 
 
 
 
 
 
1
  import requests
2
+ import io
3
+ from PIL import Image
4
+ import json
5
 
6
  API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
7
  headers = {"Authorization": "Bearer HF_API_KEY"}
8
 
9
  def query(payload):
10
  response = requests.post(API_URL, headers=headers, json=payload)
11
+ if response.status_code == 200:
12
+ content_type = response.headers.get("Content-Type", "")
13
+ if "image" in content_type:
14
+ return response.content # Returns raw image data
15
+ else:
16
+ # Log or print the unexpected content (likely JSON error message)
17
+ print("Unexpected response format:", response.text)
18
+ return None
19
+ else:
20
+ print("API Error:", response.status_code, response.text)
21
+ return None
22
+
23
+ # Request image data
24
  image_bytes = query({
25
  "inputs": "Astronaut riding a horse",
26
  })
27
+
28
+ # Check if image_bytes contains valid data before proceeding
29
+ if image_bytes:
30
+ try:
31
+ # Open image if it's valid
32
+ image = Image.open(io.BytesIO(image_bytes))
33
+ image.show() # Display the image (optional)
34
+ except Exception as e:
35
+ print("Error opening image:", e)
36
+ else:
37
+ print("Failed to retrieve image.")