Spaces:
Build error
Build error
Download process_docs.py from mukulmitra82/AmazonVM: direct link, hf CLI and curl.
- Browser
- Download file 927 Bytes
-
https://huggingface.co/spaces/mukulmitra82/AmazonVM/resolve/main/process_docs.py
- Command line
-
hf download hf://spaces/mukulmitra82/AmazonVM/process_docs.py
-
curl -L -o process_docs.py https://huggingface.co/spaces/mukulmitra82/AmazonVM/resolve/main/process_docs.py
927 Bytes
| import os | |
| import docx | |
| import glob | |
| def extract_text_from_txt(file_path): | |
| """Extract text from a .txt file.""" | |
| with open(file_path, "r", encoding="utf-8") as f: | |
| return f.read() | |
| def extract_text_from_docx(file_path): | |
| """Extract text from a .docx file.""" | |
| doc = docx.Document(file_path) | |
| return "\n".join([para.text for para in doc.paragraphs]) | |
| def load_documents(data_folder="data"): | |
| """Load and process text from .txt and .docx files.""" | |
| all_text = "" | |
| for file_path in glob.glob(os.path.join(data_folder, "*.txt")): | |
| all_text += extract_text_from_txt(file_path) + "\n\n" | |
| for file_path in glob.glob(os.path.join(data_folder, "*.docx")): | |
| all_text += extract_text_from_docx(file_path) + "\n\n" | |
| return all_text | |
| # Example usage: | |
| if __name__ == "__main__": | |
| text_data = load_documents() | |
| print("Extracted Text:", text_data[:500]) # Show the first 500 characters | |