Spaces:
Runtime error
Runtime error
| import os | |
| import json | |
| from PyPDF2 import PdfReader | |
| # Funktion zum Extrahieren von Text aus PDF-Dateien | |
| def extract_text_from_pdf(pdf_path): | |
| reader = PdfReader(pdf_path) | |
| text = "" | |
| for page in reader.pages: | |
| text += page.extract_text() | |
| return text | |
| # Funktion zum Extrahieren von Text aus TXT-Dateien | |
| def extract_text_from_txt(txt_path): | |
| with open(txt_path, "r", encoding="utf-8") as file: | |
| return file.read() | |
| # Pfad zu den Dateien im Hugging Face Space | |
| pdf_files = ["2024InformationPaducation.pdf"] | |
| txt_files = ["transkript_ki.txt"] | |
| # Liste zur Speicherung der Dokumente | |
| documents = [] | |
| # PDF-Dateien verarbeiten | |
| for pdf_file in pdf_files: | |
| if os.path.exists(pdf_file): | |
| content = extract_text_from_pdf(pdf_file) | |
| documents.append({"id": len(documents) + 1, "content": content}) | |
| # TXT-Dateien verarbeiten | |
| for txt_file in txt_files: | |
| if os.path.exists(txt_file): | |
| content = extract_text_from_txt(txt_file) | |
| documents.append({"id": len(documents) + 1, "content": content}) | |
| # Dokumente in eine JSON-Datei speichern | |
| with open("documents.json", "w", encoding="utf-8") as json_file: | |
| json.dump(documents, json_file, ensure_ascii=False, indent=4) | |
| print("documents.json wurde erfolgreich erstellt.") |