IMAGE_CHANGE_THRESHOLD exception handled + japanese romanisation fixed and unicode range updated
This commit is contained in:
+3
-2
@@ -195,11 +195,12 @@ Expected format:
|
||||
["translation1", "translation2", ...]
|
||||
|
||||
Translation:"""
|
||||
response = await self._request(prompt)
|
||||
|
||||
try:
|
||||
response = await self._request(prompt)
|
||||
response_list = ast.literal_eval(response.strip())
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to evaluate response from {self.model} from {self.site}. Error: {e}. Response: {response}")
|
||||
logger.error(f"Failed to evaluate response from {self.model} from {self.site}. Error: {e}.")
|
||||
return (1, [], 99999)
|
||||
logger.debug(repr(self))
|
||||
logger.info(f'{self.model} translated texts from: {texts_to_translate} to {response_list}.')
|
||||
|
||||
+5
-7
@@ -34,7 +34,8 @@ def _easy_init(easy_languages: list, use_GPU=True, **kwargs):
|
||||
return easyocr.Reader(easy_languages, gpu=use_GPU, **kwargs)
|
||||
|
||||
def _easy_ocr(ocr,image) -> list:
|
||||
return ocr.readtext(image)
|
||||
detected_texts = ocr.readtext(image)
|
||||
return detected_texts
|
||||
|
||||
# RapidOCR mostly for mandarin and some other asian languages
|
||||
# default only supports chinese and english
|
||||
@@ -116,9 +117,6 @@ def get_confidences(ocr_output) -> list:
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# OCR_languages = ['ch_sim','en']
|
||||
# image_old = '/home/James/Pictures/Screenshots/DP-1.jpg'
|
||||
# reader = easyocr.Reader(OCR_languages, gpu=True) # this needs to run only once to load the model into memory
|
||||
# result = reader.readtext(image_old)
|
||||
# print(result)
|
||||
print(id_keep_source_lang(init_OCR(model='paddle', paddle_lang='zh', easy_languages=['en', 'ch_sim']), '/home/James/Pictures/Screenshots/DP-1.jpg', 'ch_sim'))
|
||||
OCR_languages = ['ch_sim','en']
|
||||
reader = easyocr.Reader(OCR_languages, gpu=True)
|
||||
|
||||
@@ -71,6 +71,7 @@ async def translate_API_LLM(texts_to_translate: List[str],
|
||||
result = await task
|
||||
logger.debug(f'Result: {result}')
|
||||
if result is not None:
|
||||
tasks.discard(task)
|
||||
translation_attempts += 1
|
||||
status_code, translations, translation_mismatches = result
|
||||
if status_code == 0:
|
||||
@@ -79,14 +80,14 @@ async def translate_API_LLM(texts_to_translate: List[str],
|
||||
t.cancel()
|
||||
return translations
|
||||
else:
|
||||
logger.error(f"Model has failed to translate the text.")
|
||||
logger.error(f"Model has failed to translate the text. Result: {result}")
|
||||
if translation_attempts == no_of_models:
|
||||
if best_translation is not None:
|
||||
return translations
|
||||
else:
|
||||
logger.error("All models have failed to translate the text.")
|
||||
raise TypeError("Models have likely all outputted garbage translations or rate limited.")
|
||||
elif status_code == 1:
|
||||
elif status_code == 2:
|
||||
if best_translation is None:
|
||||
best_translation = (translations, translation_mismatches)
|
||||
else:
|
||||
|
||||
+12
-9
@@ -28,7 +28,7 @@ def is_wayland():
|
||||
|
||||
# please install grim otherwise this is way too slow for wayland
|
||||
def printsc_wayland(region: tuple, path: str):
|
||||
subprocess.run(['grim','-g', f'{region[0]},{region[1]} {region[2]-region[0]}x{region[3]-region[1]}', '-t', 'jpeg', '-q','95', path])
|
||||
subprocess.run(['grim','-g', f'{region[0]},{region[1]} {region[2]-region[0]}x{region[3]-region[1]}', '-t', 'jpeg', '-q','100', path])
|
||||
|
||||
def printsc_non_wayland(region: tuple, path: str):
|
||||
# use mss to capture the screen
|
||||
@@ -94,7 +94,7 @@ def romanize(text, lang):
|
||||
if lang in ['zh','ch_sim','ch_tra']:
|
||||
return ' '.join([ py[0] for py in pinyin(text, heteronym=True)])
|
||||
if lang == 'ja':
|
||||
return kks.convert(text)[0]['hepburn']
|
||||
return ' '.join([romaji['hepburn'] for romaji in kks.convert(text)])
|
||||
return uroman.romanize_string(text)
|
||||
|
||||
# check if a string contains words from a language
|
||||
@@ -103,7 +103,7 @@ def contains_lang(text, lang):
|
||||
if lang == 'zh':
|
||||
return bool(re.search(r'[\u4e00-\u9fff]', text))
|
||||
elif lang == 'ja':
|
||||
return bool(re.search(r'[\u3040-\u30ff]', text))
|
||||
return bool(re.search(r'[\u3040-\u30ff]', text)) or bool(re.search(r'[\u4e00-\u9fff]', text))
|
||||
elif lang == 'ko':
|
||||
return bool(re.search(r'[\uac00-\ud7af]', text))
|
||||
elif lang == 'en':
|
||||
@@ -132,7 +132,7 @@ def standardize_lang(lang):
|
||||
translation_model_lang = 'ja'
|
||||
id_model_lang = 'ja'
|
||||
elif lang == 'ko':
|
||||
easyocr_lang = 'korean'
|
||||
easyocr_lang = 'ko'
|
||||
paddleocr_lang = 'korean'
|
||||
rapidocr_lang = 'ko'
|
||||
translation_model_lang = 'ko'
|
||||
@@ -161,7 +161,7 @@ def which_ocr_lang(model):
|
||||
else:
|
||||
raise ValueError("Invalid OCR model. Please use one of 'easy', 'paddle', or 'rapid'.")
|
||||
|
||||
def similar_tfidf(list1,list2,threshold) -> float:
|
||||
def similar_tfidf(list1,list2) -> float:
|
||||
"""Calculate cosine similarity using TF-IDF vectors."""
|
||||
if not list1 or not list2:
|
||||
return 0.0
|
||||
@@ -174,7 +174,7 @@ def similar_tfidf(list1,list2,threshold) -> float:
|
||||
vec1 = np.mean(tfidf_matrix[:len(list1)].toarray(), axis=0).reshape(1, -1)
|
||||
vec2 = np.mean(tfidf_matrix[len(list1):].toarray(), axis=0).reshape(1, -1)
|
||||
|
||||
return float(cosine_similarity(vec1, vec2)[0, 0]) > threshold
|
||||
return cosine_similarity(vec1, vec2)[0, 0]
|
||||
|
||||
def similar_jacard(list1, list2) -> float:
|
||||
if not list1 or not list2:
|
||||
@@ -183,7 +183,11 @@ def similar_jacard(list1, list2) -> float:
|
||||
|
||||
def check_similarity(list1, list2, threshold, method = 'tfidf'):
|
||||
if method == 'tfidf':
|
||||
return True if similar_tfidf(list1, list2) >= threshold else False
|
||||
try:
|
||||
confidence = similar_tfidf(list1, list2)
|
||||
except ValueError:
|
||||
return True
|
||||
return True if confidence > threshold else False
|
||||
elif method == 'jacard':
|
||||
return True if similar_jacard(list1, list2) >= threshold else False
|
||||
else:
|
||||
@@ -191,5 +195,4 @@ def check_similarity(list1, list2, threshold, method = 'tfidf'):
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Example usage
|
||||
x = printsc_wayland((0,0,1920,1080), save = False)
|
||||
print(x)
|
||||
print(romanize(lang='ja', text='世界はひろい'))
|
||||
Reference in New Issue
Block a user