Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,11 +34,12 @@ def call_openai_image_api(prompt: str, api_key: str, input_image: Image.Image |
|
|
| 34 |
size = "1024x1024"
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
| 42 |
# Convert PIL Image to bytes for the API request
|
| 43 |
byte_stream = io.BytesIO()
|
| 44 |
input_image.save(byte_stream, format="PNG") # Save PIL image to bytes buffer
|
|
@@ -47,12 +48,10 @@ def call_openai_image_api(prompt: str, api_key: str, input_image: Image.Image |
|
|
| 47 |
files = {
|
| 48 |
"image": ("input_image.png", byte_stream, "image/png"),
|
| 49 |
}
|
| 50 |
-
# CORRECTED data dictionary: removed 'response_format'
|
| 51 |
data = {
|
| 52 |
"prompt": prompt,
|
| 53 |
"model": model,
|
| 54 |
"size": size,
|
| 55 |
-
# "response_format": "b64_json", # <-- REMOVE THIS LINE
|
| 56 |
}
|
| 57 |
api_url = "https://api.openai.com/v1/images/edits"
|
| 58 |
print("Calling OpenAI Image Edit API...") # Debug print
|
|
@@ -60,7 +59,7 @@ def call_openai_image_api(prompt: str, api_key: str, input_image: Image.Image |
|
|
| 60 |
|
| 61 |
else:
|
| 62 |
# --- Image Generation ---
|
| 63 |
-
# (This part remains the same
|
| 64 |
headers["Content-Type"] = "application/json"
|
| 65 |
payload = {
|
| 66 |
"prompt": prompt,
|
|
|
|
| 34 |
size = "1024x1024"
|
| 35 |
|
| 36 |
try:
|
| 37 |
+
if input_image:
|
| 38 |
+
# --- Image Editing ---
|
| 39 |
+
if not isinstance(input_image, Image.Image):
|
| 40 |
+
return None, None, "Error: Invalid image provided for editing."
|
| 41 |
|
| 42 |
+
# CORRECTED INDENTATION for this block:
|
| 43 |
# Convert PIL Image to bytes for the API request
|
| 44 |
byte_stream = io.BytesIO()
|
| 45 |
input_image.save(byte_stream, format="PNG") # Save PIL image to bytes buffer
|
|
|
|
| 48 |
files = {
|
| 49 |
"image": ("input_image.png", byte_stream, "image/png"),
|
| 50 |
}
|
|
|
|
| 51 |
data = {
|
| 52 |
"prompt": prompt,
|
| 53 |
"model": model,
|
| 54 |
"size": size,
|
|
|
|
| 55 |
}
|
| 56 |
api_url = "https://api.openai.com/v1/images/edits"
|
| 57 |
print("Calling OpenAI Image Edit API...") # Debug print
|
|
|
|
| 59 |
|
| 60 |
else:
|
| 61 |
# --- Image Generation ---
|
| 62 |
+
# (This part remains the same)
|
| 63 |
headers["Content-Type"] = "application/json"
|
| 64 |
payload = {
|
| 65 |
"prompt": prompt,
|