Update app.py
Browse files
app.py
CHANGED
|
@@ -40,13 +40,9 @@ def _get_args() -> ArgumentParser:
|
|
| 40 |
return args
|
| 41 |
|
| 42 |
def handle_image_submission(_chatbot, task_history, file) -> tuple:
|
| 43 |
-
print("handle_image_submission called")
|
| 44 |
if file is None:
|
| 45 |
-
print("No file uploaded")
|
| 46 |
return _chatbot, task_history
|
| 47 |
-
print("File received:", file)
|
| 48 |
file_path = save_image(file, uploaded_file_dir)
|
| 49 |
-
print("File saved at:", file_path)
|
| 50 |
history_item = ((file_path,), None)
|
| 51 |
_chatbot.append(history_item)
|
| 52 |
task_history.append(history_item)
|
|
@@ -112,12 +108,10 @@ def _parse_text(text: str) -> str:
|
|
| 112 |
return text
|
| 113 |
|
| 114 |
def save_image(image_file, upload_dir: str) -> str:
|
| 115 |
-
print("save_image called with:", image_file)
|
| 116 |
Path(upload_dir).mkdir(parents=True, exist_ok=True)
|
| 117 |
filename = secrets.token_hex(10) + Path(image_file.name).suffix
|
| 118 |
file_path = Path(upload_dir) / filename
|
| 119 |
-
|
| 120 |
-
with open(image_file.name, "rb") as f_input, open(file_path, "wb") as f_output:
|
| 121 |
f_output.write(f_input.read())
|
| 122 |
return str(file_path)
|
| 123 |
|
|
@@ -134,16 +128,10 @@ def predict(_chatbot, task_history) -> tuple:
|
|
| 134 |
print("predict called")
|
| 135 |
chat_query, chat_response = _chatbot[-1]
|
| 136 |
|
| 137 |
-
# Check if the chat query is a tuple (indicating an image), and if so, extract the image path
|
| 138 |
-
if isinstance(chat_query, tuple):
|
| 139 |
-
chat_query = chat_query[0]
|
| 140 |
-
|
| 141 |
-
# Main logic for processing the query
|
| 142 |
if isinstance(chat_query, tuple):
|
| 143 |
query = [{'image': chat_query[0]}]
|
| 144 |
else:
|
| 145 |
query = [{'text': _parse_text(chat_query)}]
|
| 146 |
-
print("Query for model:", query)
|
| 147 |
|
| 148 |
inputs = tokenizer.from_list_format(query)
|
| 149 |
tokenized_inputs = tokenizer(inputs, return_tensors='pt')
|
|
@@ -151,9 +139,10 @@ def predict(_chatbot, task_history) -> tuple:
|
|
| 151 |
|
| 152 |
pred = model.generate(**tokenized_inputs)
|
| 153 |
response = tokenizer.decode(pred.cpu()[0], skip_special_tokens=False)
|
|
|
|
|
|
|
| 154 |
print("Model response:", response)
|
| 155 |
|
| 156 |
-
# Handling the response based on the type of query (image or text)
|
| 157 |
if 'image' in query[0]:
|
| 158 |
image = tokenizer.draw_bbox_on_latest_picture(response)
|
| 159 |
if image is not None:
|
|
@@ -164,7 +153,6 @@ def predict(_chatbot, task_history) -> tuple:
|
|
| 164 |
else:
|
| 165 |
_chatbot[-1] = (chat_query, response)
|
| 166 |
|
| 167 |
-
# Return the updated chatbot and task history
|
| 168 |
return _chatbot, task_history
|
| 169 |
|
| 170 |
def save_uploaded_image(image_file, upload_dir):
|
|
|
|
| 40 |
return args
|
| 41 |
|
| 42 |
def handle_image_submission(_chatbot, task_history, file) -> tuple:
|
|
|
|
| 43 |
if file is None:
|
|
|
|
| 44 |
return _chatbot, task_history
|
|
|
|
| 45 |
file_path = save_image(file, uploaded_file_dir)
|
|
|
|
| 46 |
history_item = ((file_path,), None)
|
| 47 |
_chatbot.append(history_item)
|
| 48 |
task_history.append(history_item)
|
|
|
|
| 108 |
return text
|
| 109 |
|
| 110 |
def save_image(image_file, upload_dir: str) -> str:
|
|
|
|
| 111 |
Path(upload_dir).mkdir(parents=True, exist_ok=True)
|
| 112 |
filename = secrets.token_hex(10) + Path(image_file.name).suffix
|
| 113 |
file_path = Path(upload_dir) / filename
|
| 114 |
+
with open(image_file, "rb") as f_input, open(file_path, "wb") as f_output:
|
|
|
|
| 115 |
f_output.write(f_input.read())
|
| 116 |
return str(file_path)
|
| 117 |
|
|
|
|
| 128 |
print("predict called")
|
| 129 |
chat_query, chat_response = _chatbot[-1]
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
if isinstance(chat_query, tuple):
|
| 132 |
query = [{'image': chat_query[0]}]
|
| 133 |
else:
|
| 134 |
query = [{'text': _parse_text(chat_query)}]
|
|
|
|
| 135 |
|
| 136 |
inputs = tokenizer.from_list_format(query)
|
| 137 |
tokenized_inputs = tokenizer(inputs, return_tensors='pt')
|
|
|
|
| 139 |
|
| 140 |
pred = model.generate(**tokenized_inputs)
|
| 141 |
response = tokenizer.decode(pred.cpu()[0], skip_special_tokens=False)
|
| 142 |
+
|
| 143 |
+
if 'image' in query[0]:
|
| 144 |
print("Model response:", response)
|
| 145 |
|
|
|
|
| 146 |
if 'image' in query[0]:
|
| 147 |
image = tokenizer.draw_bbox_on_latest_picture(response)
|
| 148 |
if image is not None:
|
|
|
|
| 153 |
else:
|
| 154 |
_chatbot[-1] = (chat_query, response)
|
| 155 |
|
|
|
|
| 156 |
return _chatbot, task_history
|
| 157 |
|
| 158 |
def save_uploaded_image(image_file, upload_dir):
|