Ashrafb commited on
Commit
758098d
·
verified ·
1 Parent(s): 1290a75

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -5
main.py CHANGED
@@ -20,11 +20,18 @@ API_URL = "https://api-inference.huggingface.co/models/playgroundai/playground-v
20
  API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
21
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
22
  def generate_image(prompt):
23
- payload = {"inputs": prompt}
24
- response = requests.post(API_URL, headers=headers, json=payload)
25
- image_bytes = response.content
26
- image = Image.open(io.BytesIO(image_bytes))
27
- return image
 
 
 
 
 
 
 
28
 
29
 
30
 
 
20
  API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
21
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
22
  def generate_image(prompt):
23
+ try:
24
+ payload = {"inputs": prompt}
25
+ response = requests.post(API_URL, headers=headers, json=payload)
26
+ response.raise_for_status() # Raise an exception for HTTP errors
27
+ image_bytes = response.content
28
+ image = Image.open(io.BytesIO(image_bytes))
29
+ return image
30
+ except Exception as e:
31
+ # Handle the error gracefully, such as logging the error or returning a default image
32
+ print(f"Error generating image: {e}")
33
+ return None # Return None or a default image in case of error
34
+
35
 
36
 
37