-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (63 loc) · 2.48 KB
/
main.py
File metadata and controls
84 lines (63 loc) · 2.48 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sys
from var import input_path, output_path, lang, tesseract_path, model
import os
from pdf2image import convert_from_path
import pytesseract
from PIL import Image
from ollama import chat
def image_extraction():
if(not os.path.isdir(output_path+folder)):
os.makedirs(output_path + folder)
for i in range(len(images)):
images[i].save(output_path + folder + '\\page' + str(i) + '.jpg', 'JPEG')
def ocr(input_folder, output_file, language):
supported_extensions = (".jpg", ".jpeg", ".png", ".bmp", ".tiff")
pytesseract.pytesseract.tesseract_cmd = tesseract_path
try:
with open(output_file, "w", encoding="utf-8") as out_f:
for filename in os.listdir(input_folder):
if filename.lower().endswith(supported_extensions):
image_path = os.path.join(input_folder, filename)
try:
img = Image.open(image_path)
text = pytesseract.image_to_string(img, lang=language)
if text.strip():
out_f.write(f"\n\n─── {filename} ───\n")
out_f.write(text)
print(f"Text extracted from {filename}.")
else:
print(f"No Text Recognized in {filename}.")
except Exception as e:
print(f"Error at {filename}: {e}")
print(f"\nText saved in {output_file}")
except Exception as e:
print(e)
def feedllm(data):
prompt = input("Prompt: ")
if(prompt.lower() == "stop"):
sys.exit()
stream = chat(
model=model,
messages=[{'role': 'user', 'content': prompt + "Use the following Data: " + data}],
stream=True,
)
for chunk in stream:
print(chunk['message']['content'], end='', flush=True)
feedllm(data)
if __name__ == "__main__":
# Image Output Folder
splitpath = input_path.split("\\")
images = convert_from_path(input_path)
folder = splitpath[-1]
splitpath = folder.split(".")
folder = splitpath[0]
image_extraction()
#args OCR
out_txt = output_path + folder + '\\out.txt'
input_folder = output_path + folder
ocr(input_folder, out_txt, lang)
doc = open(out_txt)
data = doc.read()
doc.close()
print("The Document's Data is loaded.")
feedllm(data)