Mr-Vicky-01 commited on
Commit
6a6d286
·
verified ·
1 Parent(s): 1e8f60b

Update image_engine.py

Browse files
Files changed (1) hide show
  1. 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
- from dotenv import load_dotenv
5
- import os
6
-
7
- load_dotenv()
8
-
9
- # Define the ImageGenerator class for Hugging Face API
10
- class ImageGenerator:
11
- def __init__(self) -> None:
12
- self.api_url = "https://api-inference.huggingface.co/models/sd-community/sdxl-flash"
13
- self.headers = {"Authorization": f"Bearer {os.getenv('HUGGING_FACE')}"}
14
-
15
- def generate_image(self, prompt):
16
- payload = {
17
- "inputs": prompt,
18
- # "negative_prompt": "ugly, distorted, low quality"
19
- }
20
- response = requests.post(self.api_url, headers=self.headers, json=payload)
21
-
22
- # Debugging: Print response status and content type
23
- print(f"Response Status Code: {response.status_code}")
24
- print(f"Response Content Type: {response.headers.get('Content-Type')}")
25
-
26
- # Check for valid response
27
- if response.status_code == 200 and response.headers.get('Content-Type').startswith('image'):
28
- image_bytes = response.content
29
- image = Image.open(io.BytesIO(image_bytes))
30
- return image
31
- else:
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")