Spaces:
Runtime error
Runtime error
Download utils/docx_processor.py from ChenyuRabbitLove/junyi_bot_external: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/ChenyuRabbitLove/junyi_bot_external/resolve/main/utils/docx_processor.py
- Command line
-
hf download hf://spaces/ChenyuRabbitLove/junyi_bot_external/utils/docx_processor.py
-
curl -L -o docx_processor.py https://huggingface.co/spaces/ChenyuRabbitLove/junyi_bot_external/resolve/main/utils/docx_processor.py
1.31 kB
| import unicodedata | |
| import re | |
| import logging | |
| import docx2txt | |
| from gpt_processor import Translator | |
| class DOCXProcessor: | |
| def __init__(self, file_path: str) -> None: | |
| self.file_path = file_path | |
| self.file_info = { | |
| "file_name": self.file_path.split("/")[-1], | |
| "file_format": "DOCX", | |
| "file_full_content": "", | |
| } | |
| self.__build_info() | |
| def __build_info(self) -> None: | |
| try: | |
| text = docx2txt.process(self.file_path) | |
| text = unicodedata.normalize("NFKD", text) | |
| text = text.replace("\n", " ").replace("\r", "") | |
| text = re.sub(" +", " ", text) | |
| self.file_info["is_chinese"] = self.__is_chinese(text) | |
| tranlator = Translator() | |
| self.file_info["file_full_content"] = ( | |
| tranlator.translate_to_chinese(text) | |
| if not self.file_info["is_chinese"] | |
| else text | |
| ) | |
| except FileNotFoundError: | |
| print(f"File not found: {self.file_path}") | |
| except Exception as e: | |
| print(f"An error occurred: {str(e)}") | |
| def __is_chinese(self, text: str) -> bool: | |
| for char in text: | |
| if char >= "\u4e00" and char <= "\u9fff": | |
| return True | |
| return False | |