Files
datenbanken-llm/lib.py
T
2024-12-10 21:04:29 +01:00

43 lines
1.1 KiB
Python

import os, pymupdf, whisper, json
import cv2
import pytesseract
def read_files(path, output, filetypes=None):
if filetypes is None:
filetypes = ["pdf", "txt", "png", "jpg", "mp3"]
for root, dirs, files in os.walk(path):
for file in files:
if (file_type := file.split(".")[-1]) in filetypes:
output.append((file_type, os.path.join(path, file)))
for folder in dirs:
read_files(os.path.join(path, folder), output)
break
def extract_pdf_content(pdf_datei):
doc = pymupdf.open(pdf_datei)
a = ""
for page in doc:
a += page.get_text()
return a
def extract_mp3_content(mp3_datei):
model = whisper.load_model('tiny')
result = model.transcribe(str(mp3_datei), language='de', verbose=True)
# with open('transcript.json', "w") as f:
# json.dump(result['text'], f, indent=4)
return result["text"]
def extract_image_content(path):
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = cv2.imread(path)
return pytesseract.image_to_string(img)