Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -26,13 +26,16 @@ import os
|
|
26 |
from PIL import Image, UnidentifiedImageError
|
27 |
import secrets
|
28 |
|
|
|
29 |
def save_image(image_file) -> str:
|
30 |
upload_dir = "uploaded_images"
|
31 |
print("Creating upload directory if it doesn't exist.")
|
32 |
os.makedirs(upload_dir, exist_ok=True)
|
|
|
33 |
try:
|
|
|
34 |
print(f"Attempting to open the image file: {image_file.name}")
|
35 |
-
image = Image.open(
|
36 |
file_name = secrets.token_hex(10) + ".png"
|
37 |
file_path = os.path.join(upload_dir, file_name)
|
38 |
print(f"Generated file path: {file_path}")
|
@@ -46,8 +49,6 @@ def save_image(image_file) -> str:
|
|
46 |
except Exception as e:
|
47 |
print(f"An unexpected error occurred: {e}")
|
48 |
return None
|
49 |
-
|
50 |
-
|
51 |
def clean_response(response: str) -> str:
|
52 |
response = re.sub(r'<ref>(.*?)</ref>(?:<box>.*?</box>)*(?:<quad>.*?</quad>)*', r'\1', response).strip()
|
53 |
return response
|
|
|
26 |
from PIL import Image, UnidentifiedImageError
|
27 |
import secrets
|
28 |
|
29 |
+
|
30 |
def save_image(image_file) -> str:
|
31 |
upload_dir = "uploaded_images"
|
32 |
print("Creating upload directory if it doesn't exist.")
|
33 |
os.makedirs(upload_dir, exist_ok=True)
|
34 |
+
|
35 |
try:
|
36 |
+
image_buffer = image_file.read()
|
37 |
print(f"Attempting to open the image file: {image_file.name}")
|
38 |
+
image = Image.open(BytesIO(image_buffer)).convert("RGB")
|
39 |
file_name = secrets.token_hex(10) + ".png"
|
40 |
file_path = os.path.join(upload_dir, file_name)
|
41 |
print(f"Generated file path: {file_path}")
|
|
|
49 |
except Exception as e:
|
50 |
print(f"An unexpected error occurred: {e}")
|
51 |
return None
|
|
|
|
|
52 |
def clean_response(response: str) -> str:
|
53 |
response = re.sub(r'<ref>(.*?)</ref>(?:<box>.*?</box>)*(?:<quad>.*?</quad>)*', r'\1', response).strip()
|
54 |
return response
|