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