import gradio as gr from paddleocr import PaddleOCR, draw_ocr import asyncio import edge_tts def image2Text(image:str, langChoice:str): ocr = PaddleOCR(use_angle_cls=True, lang=langChoice) # need to run only once to download and load model into memory img_path = image result = ocr.ocr(img_path, cls=True) text = "" for idx in range(len(result)): res = result[idx] for line in res: import re # remove pinyin if it's Chinese if langChoice=="ch": #t = re.sub('[a-z0-9.]', '', line[1][0]) t = re.sub('[a-z]', '', line[1][0]) t = re.sub('[0-9]\.', '', t) t = t.replace(" ", "") t = t.replace("()", "") t = t.replace("()", "") t = t.replace("( )", "") t = t.replace("()", "") if t!="": text +=((t) + "\n") else: t = line[1][0] t = re.sub('Term [0-9] Spelling', '', t) t = re.sub('Page [0-9]', '', t) if t!="": text += (t + "\n") return text def text2Voice(recognized_text, langChoice:str): async def amain() -> None: """Main function""" communicate = edge_tts.Communicate(TEXT, VOICE) await communicate.save(OUTPUT_FILE) TEXT = recognized_text if langChoice == "ch": VOICE = "zh-CN-YunxiaNeural" else: VOICE = "en-GB-LibbyNeural" OUTPUT_FILE = "voice.mp3" #await amain() asyncio.run(amain()) return "voice.mp3" with gr.Blocks() as demo: gr.HTML("""