phyloforfun commited on
Commit
48bf402
β€’
1 Parent(s): 987ae26
app.py CHANGED
@@ -5,7 +5,7 @@ import plotly.graph_objs as go
5
  from PIL import Image
6
  import pandas as pd
7
  from io import BytesIO
8
- from streamlit_extras.let_it_rain import rain
9
  from annotated_text import annotated_text
10
 
11
  from vouchervision.LeafMachine2_Config_Builder import write_config_file
@@ -17,7 +17,7 @@ from vouchervision.API_validation import APIvalidation
17
  from vouchervision.utils_hf import setup_streamlit_config, save_uploaded_file, save_uploaded_local, save_uploaded_file_local
18
  from vouchervision.data_project import convert_pdf_to_jpg
19
  from vouchervision.utils_LLM import check_system_gpus
20
- from vouchervision.OCR_google_cloud_vision import check_for_inappropriate_content
21
 
22
  import cProfile
23
  import pstats
@@ -255,6 +255,7 @@ def load_gallery(converted_files, uploaded_file):
255
 
256
  @st.cache_data
257
  def handle_image_upload_and_gallery_hf(uploaded_files):
 
258
  if uploaded_files:
259
 
260
  # Clear input image gallery and input list
@@ -263,7 +264,7 @@ def handle_image_upload_and_gallery_hf(uploaded_files):
263
  ind_small = 0
264
  for uploaded_file in uploaded_files:
265
 
266
- if check_for_inappropriate_content(uploaded_file):
267
  clear_image_uploads()
268
  st.error("Warning: You have uploaded an inappropriate image")
269
  return True
@@ -883,7 +884,7 @@ def display_test_results(test_results, JSON_results, llm_version):
883
  # success_count = sum(1 for result in test_results.values() if result)
884
  # failure_count = len(test_results) - success_count
885
  # proportional_rain("πŸ₯‡", success_count, "πŸ’”", failure_count, font_size=72, falling_speed=5, animation_length="infinite")
886
- rain_emojis(test_results)
887
 
888
 
889
 
@@ -892,44 +893,44 @@ def add_emoji_delay():
892
 
893
 
894
 
895
- def rain_emojis(test_results):
896
- # test_results = {
897
- # 'test1': True, # Test passed
898
- # 'test2': True, # Test passed
899
- # 'test3': True, # Test passed
900
- # 'test4': False, # Test failed
901
- # 'test5': False, # Test failed
902
- # 'test6': False, # Test failed
903
- # 'test7': False, # Test failed
904
- # 'test8': False, # Test failed
905
- # 'test9': False, # Test failed
906
- # 'test10': False, # Test failed
907
- # }
908
- success_emojis = ["πŸ₯‡", "πŸ†", "🍾", "πŸ™Œ"]
909
- failure_emojis = ["πŸ’”", "😭"]
910
-
911
- success_count = sum(1 for result in test_results.values() if result)
912
- failure_count = len(test_results) - success_count
913
-
914
- chosen_emoji = random.choice(success_emojis)
915
- for _ in range(success_count):
916
- rain(
917
- emoji=chosen_emoji,
918
- font_size=72,
919
- falling_speed=4,
920
- animation_length=2,
921
- )
922
- add_emoji_delay()
923
-
924
- chosen_emoji = random.choice(failure_emojis)
925
- for _ in range(failure_count):
926
- rain(
927
- emoji=chosen_emoji,
928
- font_size=72,
929
- falling_speed=5,
930
- animation_length=1,
931
- )
932
- add_emoji_delay()
933
 
934
 
935
 
 
5
  from PIL import Image
6
  import pandas as pd
7
  from io import BytesIO
8
+ # from streamlit_extras.let_it_rain import rain
9
  from annotated_text import annotated_text
10
 
11
  from vouchervision.LeafMachine2_Config_Builder import write_config_file
 
17
  from vouchervision.utils_hf import setup_streamlit_config, save_uploaded_file, save_uploaded_local, save_uploaded_file_local
18
  from vouchervision.data_project import convert_pdf_to_jpg
19
  from vouchervision.utils_LLM import check_system_gpus
20
+ from vouchervision.OCR_google_cloud_vision import SafetyCheck
21
 
22
  import cProfile
23
  import pstats
 
255
 
256
  @st.cache_data
257
  def handle_image_upload_and_gallery_hf(uploaded_files):
258
+ SAFE = SafetyCheck(st.session_state['is_hf'])
259
  if uploaded_files:
260
 
261
  # Clear input image gallery and input list
 
264
  ind_small = 0
265
  for uploaded_file in uploaded_files:
266
 
267
+ if SAFE.check_for_inappropriate_content(uploaded_file):
268
  clear_image_uploads()
269
  st.error("Warning: You have uploaded an inappropriate image")
270
  return True
 
884
  # success_count = sum(1 for result in test_results.values() if result)
885
  # failure_count = len(test_results) - success_count
886
  # proportional_rain("πŸ₯‡", success_count, "πŸ’”", failure_count, font_size=72, falling_speed=5, animation_length="infinite")
887
+ # rain_emojis(test_results)
888
 
889
 
890
 
 
893
 
894
 
895
 
896
+ # def rain_emojis(test_results):
897
+ # # test_results = {
898
+ # # 'test1': True, # Test passed
899
+ # # 'test2': True, # Test passed
900
+ # # 'test3': True, # Test passed
901
+ # # 'test4': False, # Test failed
902
+ # # 'test5': False, # Test failed
903
+ # # 'test6': False, # Test failed
904
+ # # 'test7': False, # Test failed
905
+ # # 'test8': False, # Test failed
906
+ # # 'test9': False, # Test failed
907
+ # # 'test10': False, # Test failed
908
+ # # }
909
+ # success_emojis = ["πŸ₯‡", "πŸ†", "🍾", "πŸ™Œ"]
910
+ # failure_emojis = ["πŸ’”", "😭"]
911
+
912
+ # success_count = sum(1 for result in test_results.values() if result)
913
+ # failure_count = len(test_results) - success_count
914
+
915
+ # chosen_emoji = random.choice(success_emojis)
916
+ # for _ in range(success_count):
917
+ # rain(
918
+ # emoji=chosen_emoji,
919
+ # font_size=72,
920
+ # falling_speed=4,
921
+ # animation_length=2,
922
+ # )
923
+ # add_emoji_delay()
924
+
925
+ # chosen_emoji = random.choice(failure_emojis)
926
+ # for _ in range(failure_count):
927
+ # rain(
928
+ # emoji=chosen_emoji,
929
+ # font_size=72,
930
+ # falling_speed=5,
931
+ # animation_length=1,
932
+ # )
933
+ # add_emoji_delay()
934
 
935
 
936
 
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
 
vouchervision/OCR_google_cloud_vision.py CHANGED
@@ -792,18 +792,35 @@ class OCREngine:
792
  except:
793
  pass
794
 
795
- def check_for_inappropriate_content(file_stream):
796
- client = vision.ImageAnnotatorClient()
797
-
798
- content = file_stream.read()
799
- image = vision.Image(content=content)
800
- response = client.safe_search_detection(image=image)
801
- safe = response.safe_search_annotation
802
-
803
- # Check the levels of adult, violence, racy, etc. content.
804
- if (safe.adult > vision.Likelihood.POSSIBLE or
805
- safe.violence > vision.Likelihood.POSSIBLE or
806
- safe.racy > vision.Likelihood.POSSIBLE):
807
- return True # The image violates safe search guidelines.
 
 
 
808
 
809
- return False # The image is considered safe.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
792
  except:
793
  pass
794
 
795
+ class SafetyCheck():
796
+ def __init__(self, is_hf) -> None:
797
+ self.is_hf = is_hf
798
+ self.set_client()
799
+
800
+ def set_client(self):
801
+ if self.is_hf:
802
+ self.client = vision.ImageAnnotatorClient(credentials=self.get_google_credentials())
803
+ else:
804
+ self.client = vision.ImageAnnotatorClient(credentials=self.get_google_credentials())
805
+
806
+
807
+ def get_google_credentials(self):
808
+ creds_json_str = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
809
+ credentials = service_account.Credentials.from_service_account_info(json.loads(creds_json_str))
810
+ return credentials
811
 
812
+ def check_for_inappropriate_content(self, file_stream):
813
+ self.client = vision.ImageAnnotatorClient()
814
+
815
+ content = file_stream.read()
816
+ image = vision.Image(content=content)
817
+ response = self.client.safe_search_detection(image=image)
818
+ safe = response.safe_search_annotation
819
+
820
+ # Check the levels of adult, violence, racy, etc. content.
821
+ if (safe.adult > vision.Likelihood.POSSIBLE or
822
+ safe.violence > vision.Likelihood.POSSIBLE or
823
+ safe.racy > vision.Likelihood.POSSIBLE):
824
+ return True # The image violates safe search guidelines.
825
+
826
+ return False # The image is considered safe.