Update main.py
Browse files
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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|