Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,14 +22,27 @@ task_history = []
|
|
| 22 |
BOX_TAG_PATTERN = r"<box>([\s\S]*?)</box>"
|
| 23 |
PUNCTUATION = "!?。"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』​``【oaicite:0】``​〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏."
|
| 24 |
|
| 25 |
-
def save_image(image_file
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
with open(file_path, "wb") as f_output:
|
| 30 |
-
f_output.write(image_file.read())
|
| 31 |
-
return str(file_path)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def clean_response(response: str) -> str:
|
| 35 |
response = re.sub(r'<ref>(.*?)</ref>(?:<box>.*?</box>)*(?:<quad>.*?</quad>)*', r'\1', response).strip()
|
|
@@ -78,14 +91,13 @@ def draw_boxes(image_path, response):
|
|
| 78 |
def process_input(text=None, file=None, task_history=None):
|
| 79 |
if task_history is None:
|
| 80 |
task_history = []
|
| 81 |
-
|
| 82 |
if file is not None:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
response = chat_with_model(image_path=image_path, text_query=text, history=task_history)
|
| 89 |
task_history.append((text, response))
|
| 90 |
|
| 91 |
if "<box>" in response:
|
|
|
|
| 22 |
BOX_TAG_PATTERN = r"<box>([\s\S]*?)</box>"
|
| 23 |
PUNCTUATION = "!?。"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』​``【oaicite:0】``​〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏."
|
| 24 |
|
| 25 |
+
def save_image(image_file) -> str:
|
| 26 |
+
upload_dir = "uploaded_images"
|
| 27 |
+
print("Creating upload directory if it doesn't exist.")
|
| 28 |
+
os.makedirs(upload_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
try:
|
| 31 |
+
print("Attempting to open and convert the image.")
|
| 32 |
+
image = Image.open(image_file).convert("RGB")
|
| 33 |
+
file_name = secrets.token_hex(10) + ".png"
|
| 34 |
+
file_path = os.path.join(upload_dir, file_name)
|
| 35 |
+
print(f"Generated file path: {file_path}")
|
| 36 |
+
print("Saving the image.")
|
| 37 |
+
image.save(file_path, format="PNG")
|
| 38 |
+
print("Image saved successfully.")
|
| 39 |
+
return file_path
|
| 40 |
+
except UnidentifiedImageError:
|
| 41 |
+
print("Error: The file is not a recognizable image.")
|
| 42 |
+
return None
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"An unexpected error occurred: {e}")
|
| 45 |
+
return None
|
| 46 |
|
| 47 |
def clean_response(response: str) -> str:
|
| 48 |
response = re.sub(r'<ref>(.*?)</ref>(?:<box>.*?</box>)*(?:<quad>.*?</quad>)*', r'\1', response).strip()
|
|
|
|
| 91 |
def process_input(text=None, file=None, task_history=None):
|
| 92 |
if task_history is None:
|
| 93 |
task_history = []
|
| 94 |
+
image_buffer = None
|
| 95 |
if file is not None:
|
| 96 |
+
image_buffer = save_image(file)
|
| 97 |
+
if image_buffer is None:
|
| 98 |
+
return [("bot", "Error: Uploaded file is not a recognizable image.")], task_history
|
| 99 |
+
|
| 100 |
+
response = chat_with_model(image=image_buffer, text_query=text, history=task_history)
|
|
|
|
| 101 |
task_history.append((text, response))
|
| 102 |
|
| 103 |
if "<box>" in response:
|