Spaces:
Sleeping
Sleeping
update app.py - error handling
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import openai
|
|
| 8 |
def generate_ideogram_image(api_key, prompt, aspect_ratio="ASPECT_1_1"):
|
| 9 |
"""Generate image using Ideogram API"""
|
| 10 |
if not api_key or not prompt:
|
| 11 |
-
return None, "Please provide both API key and prompt"
|
| 12 |
|
| 13 |
url = "https://api.ideogram.ai/generate"
|
| 14 |
headers = {
|
|
@@ -31,21 +31,20 @@ def generate_ideogram_image(api_key, prompt, aspect_ratio="ASPECT_1_1"):
|
|
| 31 |
result = response.json()
|
| 32 |
if result.get("data"):
|
| 33 |
image_url = result["data"][0]["url"]
|
| 34 |
-
# Download and return the image
|
| 35 |
img_response = requests.get(image_url)
|
| 36 |
image = Image.open(io.BytesIO(img_response.content))
|
| 37 |
-
return image, "Image generated successfully!"
|
| 38 |
else:
|
| 39 |
-
return None, "No image data received"
|
| 40 |
else:
|
| 41 |
-
return None, f"Error: {response.status_code} - {response.text}"
|
| 42 |
except Exception as e:
|
| 43 |
-
return None, f"Error generating image: {str(e)}"
|
| 44 |
|
| 45 |
def generate_dalle_image(api_key, prompt, size="1024x1024"):
|
| 46 |
"""Generate image using DALL-E 3 API"""
|
| 47 |
if not api_key or not prompt:
|
| 48 |
-
return None, "Please provide both API key and prompt"
|
| 49 |
|
| 50 |
try:
|
| 51 |
client = openai.OpenAI(api_key=api_key)
|
|
@@ -57,20 +56,19 @@ def generate_dalle_image(api_key, prompt, size="1024x1024"):
|
|
| 57 |
n=1,
|
| 58 |
)
|
| 59 |
|
| 60 |
-
# Download the image from URL
|
| 61 |
image_url = response.data[0].url
|
| 62 |
img_response = requests.get(image_url)
|
| 63 |
image = Image.open(io.BytesIO(img_response.content))
|
| 64 |
-
return image, "Image generated successfully with DALL-E 3!"
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
-
return None, f"Error generating image: {str(e)}"
|
| 68 |
|
| 69 |
def process_uploaded_image(image):
|
| 70 |
"""Process uploaded image"""
|
| 71 |
if image is None:
|
| 72 |
-
return None, "No image uploaded"
|
| 73 |
-
return image, "Image uploaded successfully!"
|
| 74 |
|
| 75 |
# Text Overlay Functions
|
| 76 |
def add_text_to_image(img, pattern, line1, line2, line3, font_size, color):
|
|
@@ -224,17 +222,17 @@ def add_text_to_image(img, pattern, line1, line2, line3, font_size, color):
|
|
| 224 |
|
| 225 |
if not text_drawn:
|
| 226 |
print("No text was drawn")
|
| 227 |
-
return img, "No text to add (all lines were empty)"
|
| 228 |
|
| 229 |
print("=== Text drawing completed ===")
|
| 230 |
print(f"Returning image with mode: {image.mode}, size: {image.size}")
|
| 231 |
-
return image, f"Text added! Pattern: {pattern}, Font: {font_size}px, Color: {original_color}, Lines drawn: {sum(1 for line in lines if line and line.strip())}"
|
| 232 |
|
| 233 |
except Exception as e:
|
| 234 |
print(f"CRITICAL ERROR in add_text_to_image: {e}")
|
| 235 |
import traceback
|
| 236 |
traceback.print_exc()
|
| 237 |
-
return None, f"Error adding text: {str(e)}"
|
| 238 |
|
| 239 |
def test_text_overlay():
|
| 240 |
"""Test function to verify text overlay works"""
|
|
@@ -250,16 +248,16 @@ def test_text_overlay():
|
|
| 250 |
font = ImageFont.load_default()
|
| 251 |
draw.text((200, 150), "TEST", fill='white', font=font, anchor="mm")
|
| 252 |
print("Basic text drawing works!")
|
| 253 |
-
return test_img, "Test successful"
|
| 254 |
except Exception as e:
|
| 255 |
print(f"Basic text drawing failed: {e}")
|
| 256 |
draw.text((50, 100), "TEST", fill='white')
|
| 257 |
print("Fallback text drawing works!")
|
| 258 |
-
return test_img, "Test with fallback"
|
| 259 |
|
| 260 |
except Exception as e:
|
| 261 |
print(f"Test failed completely: {e}")
|
| 262 |
-
return None, f"Test failed: {e}"
|
| 263 |
|
| 264 |
# Create the Gradio Interface
|
| 265 |
with gr.Blocks(title="AI Image Generator & Text Overlay") as demo:
|
|
|
|
| 8 |
def generate_ideogram_image(api_key, prompt, aspect_ratio="ASPECT_1_1"):
|
| 9 |
"""Generate image using Ideogram API"""
|
| 10 |
if not api_key or not prompt:
|
| 11 |
+
return None, "Please provide both API key and prompt", None
|
| 12 |
|
| 13 |
url = "https://api.ideogram.ai/generate"
|
| 14 |
headers = {
|
|
|
|
| 31 |
result = response.json()
|
| 32 |
if result.get("data"):
|
| 33 |
image_url = result["data"][0]["url"]
|
|
|
|
| 34 |
img_response = requests.get(image_url)
|
| 35 |
image = Image.open(io.BytesIO(img_response.content))
|
| 36 |
+
return image, "Image generated successfully!", image
|
| 37 |
else:
|
| 38 |
+
return None, "No image data received", None
|
| 39 |
else:
|
| 40 |
+
return None, f"Error: {response.status_code} - {response.text}", None
|
| 41 |
except Exception as e:
|
| 42 |
+
return None, f"Error generating image: {str(e)}", None
|
| 43 |
|
| 44 |
def generate_dalle_image(api_key, prompt, size="1024x1024"):
|
| 45 |
"""Generate image using DALL-E 3 API"""
|
| 46 |
if not api_key or not prompt:
|
| 47 |
+
return None, "Please provide both API key and prompt", None
|
| 48 |
|
| 49 |
try:
|
| 50 |
client = openai.OpenAI(api_key=api_key)
|
|
|
|
| 56 |
n=1,
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
image_url = response.data[0].url
|
| 60 |
img_response = requests.get(image_url)
|
| 61 |
image = Image.open(io.BytesIO(img_response.content))
|
| 62 |
+
return image, "Image generated successfully with DALL-E 3!", image
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
+
return None, f"Error generating image: {str(e)}", None
|
| 66 |
|
| 67 |
def process_uploaded_image(image):
|
| 68 |
"""Process uploaded image"""
|
| 69 |
if image is None:
|
| 70 |
+
return None, "No image uploaded", None
|
| 71 |
+
return image, "Image uploaded successfully!", image
|
| 72 |
|
| 73 |
# Text Overlay Functions
|
| 74 |
def add_text_to_image(img, pattern, line1, line2, line3, font_size, color):
|
|
|
|
| 222 |
|
| 223 |
if not text_drawn:
|
| 224 |
print("No text was drawn")
|
| 225 |
+
return img, "No text to add (all lines were empty)", None
|
| 226 |
|
| 227 |
print("=== Text drawing completed ===")
|
| 228 |
print(f"Returning image with mode: {image.mode}, size: {image.size}")
|
| 229 |
+
return image, f"Text added! Pattern: {pattern}, Font: {font_size}px, Color: {original_color}, Lines drawn: {sum(1 for line in lines if line and line.strip())}", None
|
| 230 |
|
| 231 |
except Exception as e:
|
| 232 |
print(f"CRITICAL ERROR in add_text_to_image: {e}")
|
| 233 |
import traceback
|
| 234 |
traceback.print_exc()
|
| 235 |
+
return None, f"Error adding text: {str(e)}", None
|
| 236 |
|
| 237 |
def test_text_overlay():
|
| 238 |
"""Test function to verify text overlay works"""
|
|
|
|
| 248 |
font = ImageFont.load_default()
|
| 249 |
draw.text((200, 150), "TEST", fill='white', font=font, anchor="mm")
|
| 250 |
print("Basic text drawing works!")
|
| 251 |
+
return test_img, "Test successful", None
|
| 252 |
except Exception as e:
|
| 253 |
print(f"Basic text drawing failed: {e}")
|
| 254 |
draw.text((50, 100), "TEST", fill='white')
|
| 255 |
print("Fallback text drawing works!")
|
| 256 |
+
return test_img, "Test with fallback", None
|
| 257 |
|
| 258 |
except Exception as e:
|
| 259 |
print(f"Test failed completely: {e}")
|
| 260 |
+
return None, f"Test failed: {e}", None
|
| 261 |
|
| 262 |
# Create the Gradio Interface
|
| 263 |
with gr.Blocks(title="AI Image Generator & Text Overlay") as demo:
|