File size: 1,252 Bytes
8314c51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00b5b6c
 
8314c51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 = ["paducation.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.")