Update app.py
Browse files
app.py
CHANGED
@@ -42,7 +42,7 @@ def handle_image_submission(_chatbot, task_history, file):
|
|
42 |
if file is None:
|
43 |
print("No file uploaded")
|
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)
|
@@ -104,14 +104,18 @@ def _parse_text(text):
|
|
104 |
lines[i] = "<br>" + line
|
105 |
text = "".join(lines)
|
106 |
return text
|
107 |
-
|
108 |
def save_image(image_file, upload_dir):
|
109 |
Path(upload_dir).mkdir(parents=True, exist_ok=True)
|
110 |
filename = secrets.token_hex(10) + Path(image_file.name).suffix
|
111 |
file_path = Path(upload_dir) / filename
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
114 |
return str(file_path)
|
|
|
115 |
|
116 |
def add_file(history, task_history, file):
|
117 |
if file is None:
|
|
|
42 |
if file is None:
|
43 |
print("No file uploaded")
|
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)
|
|
|
104 |
lines[i] = "<br>" + line
|
105 |
text = "".join(lines)
|
106 |
return text
|
107 |
+
|
108 |
def save_image(image_file, upload_dir):
|
109 |
Path(upload_dir).mkdir(parents=True, exist_ok=True)
|
110 |
filename = secrets.token_hex(10) + Path(image_file.name).suffix
|
111 |
file_path = Path(upload_dir) / filename
|
112 |
+
if isinstance(image_file, gr.NamedString):
|
113 |
+
shutil.copy(image_file.name, file_path)
|
114 |
+
else:
|
115 |
+
with open(file_path, "wb") as f:
|
116 |
+
f.write(image_file.read())
|
117 |
return str(file_path)
|
118 |
+
|
119 |
|
120 |
def add_file(history, task_history, file):
|
121 |
if file is None:
|