phyloforfun commited on
Commit
6b88c8f
1 Parent(s): 12ea480

fix safety check

Browse files
app.py CHANGED
@@ -272,7 +272,7 @@ def handle_image_upload_and_gallery_hf(uploaded_files):
272
  # Determine the file type
273
  if uploaded_file.name.lower().endswith('.pdf'):
274
  # Handle PDF files
275
- file_path = save_uploaded_file(st.session_state['dir_uploaded_images'], uploaded_file)
276
  # Convert each page of the PDF to an image
277
  n_pages = convert_pdf_to_jpg(file_path, st.session_state['dir_uploaded_images'], dpi=200)#st.session_state.config['leafmachine']['project']['dir_images_local'])
278
  # Update the input list for each page image
@@ -287,16 +287,18 @@ def handle_image_upload_and_gallery_hf(uploaded_files):
287
  # Optionally, create a thumbnail for the gallery
288
  img = Image.open(jpg_file_path)
289
  img.thumbnail((GALLERY_IMAGE_SIZE, GALLERY_IMAGE_SIZE), Image.Resampling.LANCZOS)
290
- try:
291
  file_path_small = save_uploaded_file(st.session_state['dir_uploaded_images_small'], file_name, img)
292
- except:
293
  file_path_small = save_uploaded_file_local(st.session_state['dir_uploaded_images_small'],st.session_state['dir_uploaded_images_small'], file_name, img)
294
  st.session_state['input_list_small'].append(file_path_small)
295
 
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)
 
 
300
  st.session_state['input_list'].append(file_path)
301
  if ind_small < MAX_GALLERY_IMAGES +5:
302
  img = Image.open(file_path)
 
272
  # Determine the file type
273
  if uploaded_file.name.lower().endswith('.pdf'):
274
  # Handle PDF files
275
+ file_path = save_uploaded_file(st.session_state['dir_uploaded_images'], uploaded_file, img=None)
276
  # Convert each page of the PDF to an image
277
  n_pages = convert_pdf_to_jpg(file_path, st.session_state['dir_uploaded_images'], dpi=200)#st.session_state.config['leafmachine']['project']['dir_images_local'])
278
  # Update the input list for each page image
 
287
  # Optionally, create a thumbnail for the gallery
288
  img = Image.open(jpg_file_path)
289
  img.thumbnail((GALLERY_IMAGE_SIZE, GALLERY_IMAGE_SIZE), Image.Resampling.LANCZOS)
290
+ if st.session_state['is_hf']:
291
  file_path_small = save_uploaded_file(st.session_state['dir_uploaded_images_small'], file_name, img)
292
+ else:
293
  file_path_small = save_uploaded_file_local(st.session_state['dir_uploaded_images_small'],st.session_state['dir_uploaded_images_small'], file_name, img)
294
  st.session_state['input_list_small'].append(file_path_small)
295
 
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:
304
  img = Image.open(file_path)
vouchervision/OCR_google_cloud_vision.py CHANGED
@@ -809,42 +809,42 @@ class SafetyCheck():
809
  return credentials
810
 
811
  def check_for_inappropriate_content(self, file_stream):
812
- try:
813
- LEVEL = 2
814
- # content = file_stream.read()
815
- file_stream.seek(0) # Reset file stream position to the beginning
816
- content = file_stream.read()
817
- image = vision.Image(content=content)
818
- response = self.client.safe_search_detection(image=image)
819
- safe = response.safe_search_annotation
820
-
821
- likelihood_name = (
822
- "UNKNOWN",
823
- "VERY_UNLIKELY",
824
- "UNLIKELY",
825
- "POSSIBLE",
826
- "LIKELY",
827
- "VERY_LIKELY",
828
- )
829
- print("Safe search:")
830
-
831
- print(f" adult*: {likelihood_name[safe.adult]}")
832
- print(f" medical*: {likelihood_name[safe.medical]}")
833
- print(f" spoofed: {likelihood_name[safe.spoof]}")
834
- print(f" violence*: {likelihood_name[safe.violence]}")
835
- print(f" racy: {likelihood_name[safe.racy]}")
836
-
837
- # Check the levels of adult, violence, racy, etc. content.
838
- if (safe.adult > LEVEL or
839
- safe.medical > LEVEL or
840
- # safe.spoof > LEVEL or
841
- safe.violence > LEVEL #or
842
- # safe.racy > LEVEL
843
- ):
844
- print("Found violation")
845
- return True # The image violates safe search guidelines.
846
-
847
- print("Found NO violation")
848
- return False # The image is considered safe.
849
- except:
850
- return False # The image is considered safe. TEMPOROARY FIX TODO
 
809
  return credentials
810
 
811
  def check_for_inappropriate_content(self, file_stream):
812
+ # try:
813
+ LEVEL = 2
814
+ # content = file_stream.read()
815
+ file_stream.seek(0) # Reset file stream position to the beginning
816
+ content = file_stream.read()
817
+ image = vision.Image(content=content)
818
+ response = self.client.safe_search_detection(image=image)
819
+ safe = response.safe_search_annotation
820
+
821
+ likelihood_name = (
822
+ "UNKNOWN",
823
+ "VERY_UNLIKELY",
824
+ "UNLIKELY",
825
+ "POSSIBLE",
826
+ "LIKELY",
827
+ "VERY_LIKELY",
828
+ )
829
+ print("Safe search:")
830
+
831
+ print(f" adult*: {likelihood_name[safe.adult]}")
832
+ print(f" medical*: {likelihood_name[safe.medical]}")
833
+ print(f" spoofed: {likelihood_name[safe.spoof]}")
834
+ print(f" violence*: {likelihood_name[safe.violence]}")
835
+ print(f" racy: {likelihood_name[safe.racy]}")
836
+
837
+ # Check the levels of adult, violence, racy, etc. content.
838
+ if (safe.adult > LEVEL or
839
+ safe.medical > LEVEL or
840
+ # safe.spoof > LEVEL or
841
+ safe.violence > LEVEL #or
842
+ # safe.racy > LEVEL
843
+ ):
844
+ print("Found violation")
845
+ return True # The image violates safe search guidelines.
846
+
847
+ print("Found NO violation")
848
+ return False # The image is considered safe.
849
+ # except:
850
+ # return False # The image is considered safe. TEMPOROARY FIX TODO
vouchervision/utils_hf.py CHANGED
@@ -69,49 +69,78 @@ 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
 
116
  def save_uploaded_local(directory, img_file, image=None):
117
  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
+ # 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]