Tonic commited on
Commit
577a43e
·
1 Parent(s): a7024ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -1
app.py CHANGED
@@ -103,10 +103,25 @@ def _parse_text(text):
103
  lines[i] = "<br>" + line
104
  text = "".join(lines)
105
  return text
 
 
 
 
106
 
 
 
 
 
 
 
 
 
 
 
 
107
  def add_file(history, task_history, file):
108
  if file is None:
109
- return history, task_history # Return if no file is uploaded
110
  file_path = save_image(file)
111
  history = history + [((file_path,), None)]
112
  task_history = task_history + [((file_path,), None)]
 
103
  lines[i] = "<br>" + line
104
  text = "".join(lines)
105
  return text
106
+ def save_image(image_file):
107
+ # Create a directory for uploaded images if it doesn't exist
108
+ upload_dir = Path(uploaded_file_dir) / "uploads"
109
+ upload_dir.mkdir(parents=True, exist_ok=True)
110
 
111
+ # Generate a unique filename for the image
112
+ filename = secrets.token_hex(10) + Path(image_file.name).suffix
113
+
114
+ # Save the image to the filesystem
115
+ file_path = upload_dir / filename
116
+ with open(file_path, "wb") as f:
117
+ f.write(image_file.read())
118
+
119
+ # Return the path to the saved image
120
+ return str(file_path)
121
+
122
  def add_file(history, task_history, file):
123
  if file is None:
124
+ return history, task_history
125
  file_path = save_image(file)
126
  history = history + [((file_path,), None)]
127
  task_history = task_history + [((file_path,), None)]