Spaces:
Running
Running
Commit
β’
48bf402
1
Parent(s):
987ae26
updates
Browse files- app.py +43 -42
- requirements.txt +0 -0
- vouchervision/OCR_google_cloud_vision.py +31 -14
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
|
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 |
-
|
897 |
-
|
898 |
-
|
899 |
-
|
900 |
-
|
901 |
-
|
902 |
-
|
903 |
-
|
904 |
-
|
905 |
-
|
906 |
-
|
907 |
-
|
908 |
-
|
909 |
-
|
910 |
-
|
911 |
-
|
912 |
-
|
913 |
-
|
914 |
-
|
915 |
-
|
916 |
-
|
917 |
-
|
918 |
-
|
919 |
-
|
920 |
-
|
921 |
-
|
922 |
-
|
923 |
-
|
924 |
-
|
925 |
-
|
926 |
-
|
927 |
-
|
928 |
-
|
929 |
-
|
930 |
-
|
931 |
-
|
932 |
-
|
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 |
-
|
796 |
-
|
797 |
-
|
798 |
-
|
799 |
-
|
800 |
-
|
801 |
-
|
802 |
-
|
803 |
-
|
804 |
-
|
805 |
-
|
806 |
-
|
807 |
-
|
|
|
|
|
|
|
808 |
|
809 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|