islasher commited on
Commit
a23dac1
·
verified ·
1 Parent(s): 76890ea

Update ImageGenerator.py

Browse files
Files changed (1) hide show
  1. ImageGenerator.py +61 -6
ImageGenerator.py CHANGED
@@ -18,17 +18,72 @@ import requests
18
  from PIL import Image, UnidentifiedImageError, ImageDraw, ImageFont
19
  from io import BytesIO
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def generate_image(prompt):
22
  """
23
- Generate an image from a prompt via an API.
24
  Returns a PIL.Image object. If the API fails, returns a placeholder image.
25
  """
 
 
 
 
 
 
 
 
 
26
  try:
27
- api_url="https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5"
28
- headers = {"Authorization": f"Bearer {HF_API_KEY}"}
29
- response = requests.post(api_url, headers=headers, json={"inputs": prompt.split('-')[-1]})
30
 
31
- # Check if the response is OK
32
  if response.status_code == 200:
33
  try:
34
  img = Image.open(BytesIO(response.content))
@@ -48,7 +103,6 @@ def generate_image(prompt):
48
  draw = ImageDraw.Draw(placeholder)
49
  text = "Image\nNot\nAvailable"
50
 
51
- # Optional: Use a basic font
52
  try:
53
  font = ImageFont.load_default()
54
  w, h = draw.multiline_textsize(text, font=font)
@@ -59,3 +113,4 @@ def generate_image(prompt):
59
  draw.text((50, 200), text, fill=(50, 50, 50))
60
 
61
  return placeholder
 
 
18
  from PIL import Image, UnidentifiedImageError, ImageDraw, ImageFont
19
  from io import BytesIO
20
 
21
+ # def generate_image(prompt):
22
+ # """
23
+ # Generate an image from a prompt via an API.
24
+ # Returns a PIL.Image object. If the API fails, returns a placeholder image.
25
+ # """
26
+ # try:
27
+ # api_url="https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5"
28
+ # headers = {"Authorization": f"Bearer {HF_API_KEY}"}
29
+ # response = requests.post(api_url, headers=headers, json={"inputs": prompt.split('-')[-1]})
30
+
31
+ # # Check if the response is OK
32
+ # if response.status_code == 200:
33
+ # try:
34
+ # img = Image.open(BytesIO(response.content))
35
+ # return img
36
+ # except UnidentifiedImageError:
37
+ # print("API response is not a valid image.")
38
+ # else:
39
+ # print(f"API returned status {response.status_code}")
40
+ # print("Response content:", response.text[:200])
41
+
42
+ # except requests.RequestException as e:
43
+ # print(f"Request failed: {e}")
44
+
45
+ # # Fallback placeholder image
46
+ # print("Using placeholder image.")
47
+ # placeholder = Image.new("RGB", (512, 512), color=(200, 200, 200))
48
+ # draw = ImageDraw.Draw(placeholder)
49
+ # text = "Image\nNot\nAvailable"
50
+
51
+ # # Optional: Use a basic font
52
+ # try:
53
+ # font = ImageFont.load_default()
54
+ # w, h = draw.multiline_textsize(text, font=font)
55
+ # draw.multiline_text(
56
+ # ((512 - w) / 2, (512 - h) / 2), text, fill=(50, 50, 50), font=font, align="center"
57
+ # )
58
+ # except Exception:
59
+ # draw.text((50, 200), text, fill=(50, 50, 50))
60
+
61
+ # return placeholder
62
+
63
+ import os
64
+ import requests
65
+ from PIL import Image, UnidentifiedImageError, ImageDraw, ImageFont
66
+ from io import BytesIO
67
+
68
+ HF_API_KEY = os.getenv("HF_API_KEY")
69
+
70
  def generate_image(prompt):
71
  """
72
+ Generate an image from the part of the prompt after the dash (-) via Hugging Face API.
73
  Returns a PIL.Image object. If the API fails, returns a placeholder image.
74
  """
75
+ api_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-v1-5"
76
+ headers = {"Authorization": f"Bearer {HF_API_KEY}"}
77
+
78
+ # Get the part after the dash
79
+ if "-" in prompt:
80
+ prompt_to_send = prompt.split("-", 1)[-1].strip()
81
+ else:
82
+ prompt_to_send = prompt.strip()
83
+
84
  try:
85
+ response = requests.post(api_url, headers=headers, json={"inputs": prompt_to_send})
 
 
86
 
 
87
  if response.status_code == 200:
88
  try:
89
  img = Image.open(BytesIO(response.content))
 
103
  draw = ImageDraw.Draw(placeholder)
104
  text = "Image\nNot\nAvailable"
105
 
 
106
  try:
107
  font = ImageFont.load_default()
108
  w, h = draw.multiline_textsize(text, font=font)
 
113
  draw.text((50, 200), text, fill=(50, 50, 50))
114
 
115
  return placeholder
116
+