phyloforfun commited on
Commit
93ee5fc
·
1 Parent(s): 58ad63b

fix safety check

Browse files
Files changed (2) hide show
  1. app.py +2 -2
  2. vouchervision/utils_hf.py +65 -58
app.py CHANGED
@@ -296,8 +296,8 @@ def handle_image_upload_and_gallery_hf(uploaded_files):
296
  else:
297
  ind_small += 1
298
  # Handle JPG/JPEG files (existing process)
299
- # file_path = save_uploaded_file(st.session_state['dir_uploaded_images'], uploaded_file) ######### Yale TODO
300
- file_path = os.path.join(st.session_state['dir_uploaded_images'], uploaded_file.name)
301
 
302
  st.session_state['input_list'].append(file_path)
303
  if ind_small < MAX_GALLERY_IMAGES +5:
 
296
  else:
297
  ind_small += 1
298
  # Handle JPG/JPEG files (existing process)
299
+ file_path = save_uploaded_file(st.session_state['dir_uploaded_images'], uploaded_file, img=None) ######### Yale TODO
300
+ # file_path = os.path.join(st.session_state['dir_uploaded_images'], uploaded_file.name)
301
 
302
  st.session_state['input_list'].append(file_path)
303
  if ind_small < MAX_GALLERY_IMAGES +5:
vouchervision/utils_hf.py CHANGED
@@ -69,78 +69,85 @@ def save_uploaded_file_local(directory_in, directory_out, img_file_name, image=N
69
  pass
70
 
71
 
72
- # def save_uploaded_file(directory, img_file, image=None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  # if not os.path.exists(directory):
74
  # os.makedirs(directory)
75
 
76
- # full_path = os.path.join(directory, img_file.name)
77
 
78
- # # Assuming the uploaded file is an image
79
- # if img_file.name.lower().endswith('.pdf'):
80
  # with open(full_path, 'wb') as out_file:
81
- # # If img_file is a file-like object (e.g., Django's UploadedFile),
82
- # # you can use copyfileobj or read chunks.
83
- # # If it's a path, you'd need to open and then save it.
84
- # if hasattr(img_file, 'read'):
85
- # # This is a file-like object
86
- # copyfileobj(img_file, out_file)
87
  # else:
88
- # # If img_file is a path string
89
- # with open(img_file, 'rb') as fd:
90
- # copyfileobj(fd, out_file)
91
- # return full_path
92
  # else:
93
  # if image is None:
94
  # try:
95
- # with Image.open(img_file) as image:
96
- # full_path = os.path.join(directory, img_file.name)
97
- # image.save(full_path, "JPEG")
98
- # # Return the full path of the saved image
99
- # return full_path
100
  # except:
101
- # with Image.open(os.path.join(directory,img_file)) as image:
102
- # full_path = os.path.join(directory, img_file)
103
- # image.save(full_path, "JPEG")
104
- # # Return the full path of the saved image
105
- # return full_path
106
  # else:
107
  # try:
108
- # full_path = os.path.join(directory, img_file.name)
109
  # image.save(full_path, "JPEG")
110
- # return full_path
111
  # except:
112
- # full_path = os.path.join(directory, img_file)
113
- # image.save(full_path, "JPEG")
114
- # return full_path
115
- def save_uploaded_file(directory, uploaded_file, image=None):
116
- if not os.path.exists(directory):
117
- os.makedirs(directory)
118
-
119
- full_path = os.path.join(directory, uploaded_file.name)
120
-
121
- # Handle PDF files
122
- if uploaded_file.name.lower().endswith('.pdf'):
123
- with open(full_path, 'wb') as out_file:
124
- if hasattr(uploaded_file, 'read'):
125
- copyfileobj(uploaded_file, out_file)
126
- else:
127
- with open(uploaded_file, 'rb') as fd:
128
- copyfileobj(fd, out_file)
129
- return full_path
130
- else:
131
- if image is None:
132
- try:
133
- with Image.open(uploaded_file) as img:
134
- img.save(full_path, "JPEG")
135
- except Exception:
136
- with Image.open(os.path.join(directory, uploaded_file.name)) as img:
137
- img.save(full_path, "JPEG")
138
- else:
139
- try:
140
- image.save(full_path, "JPEG")
141
- except Exception:
142
- image.save(os.path.join(directory, uploaded_file.name), "JPEG")
143
- return full_path
144
 
145
  def save_uploaded_local(directory, img_file, image=None):
146
  name = img_file.split(os.path.sep)[-1]
 
69
  pass
70
 
71
 
72
+ def save_uploaded_file(directory, img_file, image=None):
73
+ if not os.path.exists(directory):
74
+ os.makedirs(directory)
75
+
76
+ full_path = os.path.join(directory, img_file.name)
77
+
78
+ # Assuming the uploaded file is an image
79
+ if img_file.name.lower().endswith('.pdf'):
80
+ with open(full_path, 'wb') as out_file:
81
+ # If img_file is a file-like object (e.g., Django's UploadedFile),
82
+ # you can use copyfileobj or read chunks.
83
+ # If it's a path, you'd need to open and then save it.
84
+ if hasattr(img_file, 'read'):
85
+ # This is a file-like object
86
+ copyfileobj(img_file, out_file)
87
+ else:
88
+ # If img_file is a path string
89
+ with open(img_file, 'rb') as fd:
90
+ copyfileobj(fd, out_file)
91
+ return full_path
92
+ else:
93
+ if image is None:
94
+ try:
95
+ with Image.open(img_file) as image:
96
+ full_path = os.path.join(directory, img_file.name)
97
+ image.save(full_path, "JPEG")
98
+ # Return the full path of the saved image
99
+ return full_path
100
+ except:
101
+ try:
102
+ with Image.open(os.path.join(directory,img_file)) as image:
103
+ full_path = os.path.join(directory, img_file)
104
+ image.save(full_path, "JPEG")
105
+ # Return the full path of the saved image
106
+ return full_path
107
+ except:
108
+ with Image.open(os.path.join(directory,img_file.name)) as image:
109
+ full_path = os.path.join(directory, img_file.name)
110
+ image.save(full_path, "JPEG")
111
+ # Return the full path of the saved image
112
+ return full_path
113
+ else:
114
+ try:
115
+ full_path = os.path.join(directory, img_file.name)
116
+ image.save(full_path, "JPEG")
117
+ return full_path
118
+ except:
119
+ full_path = os.path.join(directory, img_file)
120
+ image.save(full_path, "JPEG")
121
+ return full_path
122
+ # def save_uploaded_file(directory, uploaded_file, image=None):
123
  # if not os.path.exists(directory):
124
  # os.makedirs(directory)
125
 
126
+ # full_path = os.path.join(directory, uploaded_file.name)
127
 
128
+ # # Handle PDF files
129
+ # if uploaded_file.name.lower().endswith('.pdf'):
130
  # with open(full_path, 'wb') as out_file:
131
+ # if hasattr(uploaded_file, 'read'):
132
+ # copyfileobj(uploaded_file, out_file)
 
 
 
 
133
  # else:
134
+ # with open(uploaded_file, 'rb') as fd:
135
+ # copyfileobj(fd, out_file)
136
+ # return full_path
 
137
  # else:
138
  # if image is None:
139
  # try:
140
+ # with Image.open(uploaded_file) as img:
141
+ # img.save(full_path, "JPEG")
 
 
 
142
  # except:
143
+ # with Image.open(full_path) as img:
144
+ # img.save(full_path, "JPEG")
 
 
 
145
  # else:
146
  # try:
 
147
  # image.save(full_path, "JPEG")
 
148
  # except:
149
+ # image.save(os.path.join(directory, uploaded_file.name), "JPEG")
150
+ # return full_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
  def save_uploaded_local(directory, img_file, image=None):
153
  name = img_file.split(os.path.sep)[-1]