Spaces:
Running
Running
Mr-Vicky-01
commited on
Update image_engine.py
Browse files- image_engine.py +31 -34
image_engine.py
CHANGED
@@ -1,35 +1,32 @@
|
|
1 |
-
from PIL import Image
|
2 |
-
import requests
|
3 |
-
import io
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
# Print the error message if not a valid image
|
33 |
-
print("Error: Invalid response received from API")
|
34 |
-
print(response.text) # Print the text content of the response for debugging
|
35 |
raise Exception("Failed to generate image from API")
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import requests
|
3 |
+
import io
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Define the ImageGenerator class for Hugging Face API
|
7 |
+
class ImageGenerator:
|
8 |
+
def __init__(self) -> None:
|
9 |
+
self.api_url = "https://api-inference.huggingface.co/models/sd-community/sdxl-flash"
|
10 |
+
self.headers = {"Authorization": f"Bearer {os.getenv('HUGGING_FACE')}"}
|
11 |
+
|
12 |
+
def generate_image(self, prompt):
|
13 |
+
payload = {
|
14 |
+
"inputs": prompt,
|
15 |
+
# "negative_prompt": "ugly, distorted, low quality"
|
16 |
+
}
|
17 |
+
response = requests.post(self.api_url, headers=self.headers, json=payload)
|
18 |
+
|
19 |
+
# Debugging: Print response status and content type
|
20 |
+
print(f"Response Status Code: {response.status_code}")
|
21 |
+
print(f"Response Content Type: {response.headers.get('Content-Type')}")
|
22 |
+
|
23 |
+
# Check for valid response
|
24 |
+
if response.status_code == 200 and response.headers.get('Content-Type').startswith('image'):
|
25 |
+
image_bytes = response.content
|
26 |
+
image = Image.open(io.BytesIO(image_bytes))
|
27 |
+
return image
|
28 |
+
else:
|
29 |
+
# Print the error message if not a valid image
|
30 |
+
print("Error: Invalid response received from API")
|
31 |
+
print(response.text) # Print the text content of the response for debugging
|
|
|
|
|
|
|
32 |
raise Exception("Failed to generate image from API")
|