-
Notifications
You must be signed in to change notification settings - Fork 2
/
llm-ocr-gui.py
203 lines (174 loc) · 6.84 KB
/
llm-ocr-gui.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import sys
import base64
import requests
import os
import io
from typing import Optional, List
from PyQt6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton,
QLineEdit, QLabel, QFileDialog, QProgressBar, QTextEdit
)
from PyQt6.QtCore import Qt, QThread, pyqtSignal
class LLMProcessor:
def __init__(self, api_url: str, api_password: str, instruction: str):
self.api_url = api_url
self.instruction = instruction
self.headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_password}",
}
def encode_file_to_base64(self, file_path: str) -> Optional[str]:
try:
with open(file_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
except Exception as e:
print(f"Error encoding file {file_path}: {e}")
return None
def send_image_to_llm(self, base64_image: str) -> Optional[str]:
prompt = f"<|im_start|>user\n{self.instruction}<|im_end|>\n<|im_start|>assistant\n"
payload = {
"prompt": prompt,
"max_context": 4096,
"max_length": 1024,
"images": [base64_image],
"top_p": 1,
"top_k": 0,
"temp": 0,
"rep_pen": 1.05,
"min_p": 0.02,
}
try:
response = requests.post(
f"{self.api_url}/api/v1/generate",
json=payload,
headers=self.headers
)
response.raise_for_status()
return response.json()["results"][0].get("text")
except Exception as e:
print(f"Error in LLM request: {e}")
return None
def process_file(self, file_path: str) -> tuple[Optional[str], str]:
"""Process an image file and return (result, output_path)"""
file_ext = os.path.splitext(file_path)[1].lower()
if file_ext in ('.png', '.jpg', '.jpeg'):
result = self.encode_file_to_base64(file_path)
if result:
result = self.send_image_to_llm(result)
output_path = os.path.splitext(file_path)[0] + ".txt"
return result, output_path
else:
return None, ""
def save_result(self, result: str, output_path: str) -> bool:
"""Save the processing result to a file"""
try:
with open(output_path, "w", encoding="utf-8") as output_file:
output_file.write(result)
return True
except Exception as e:
print(f"Error saving to {output_path}: {e}")
return False
class ProcessingThread(QThread):
progress = pyqtSignal(int, int) # current, total
finished = pyqtSignal()
error = pyqtSignal(str)
def __init__(self, processor: LLMProcessor, files: List[str]):
super().__init__()
self.processor = processor
self.files = files
def run(self):
try:
for i, file_path in enumerate(self.files):
result, output_path = self.processor.process_file(file_path)
if result:
self.processor.save_result(result, output_path)
self.progress.emit(i + 1, len(self.files))
self.finished.emit()
except Exception as e:
self.error.emit(str(e))
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("LLM Image Processor")
self.setMinimumWidth(600)
# Create main widget and layout
main_widget = QWidget()
self.setCentralWidget(main_widget)
layout = QVBoxLayout(main_widget)
# API URL
layout.addWidget(QLabel("API URL:"))
self.api_url = QLineEdit("http://localhost:5001")
layout.addWidget(self.api_url)
# API Password
layout.addWidget(QLabel("API Password:"))
self.api_password = QLineEdit()
self.api_password.setEchoMode(QLineEdit.EchoMode.Password)
layout.addWidget(self.api_password)
# Instruction
layout.addWidget(QLabel("Instruction:"))
self.instruction = QTextEdit()
self.instruction.setPlaceholderText("Enter instruction for the LLM...")
self.instruction.setMaximumHeight(100)
self.instruction.setText("Repeat verbatim all text on the image.")
layout.addWidget(self.instruction)
# File selection
self.file_button = QPushButton("Select Images")
self.file_button.clicked.connect(self.select_files)
layout.addWidget(self.file_button)
# Selected files display
self.files_label = QLabel("No files selected")
layout.addWidget(self.files_label)
# Progress bar
self.progress = QProgressBar()
layout.addWidget(self.progress)
# Process button
self.process_button = QPushButton("Process Images")
self.process_button.clicked.connect(self.process_files)
self.process_button.setEnabled(False)
layout.addWidget(self.process_button)
self.selected_files = []
def select_files(self):
files, _ = QFileDialog.getOpenFileNames(
self,
"Select Images",
"",
"Image Files (*.png *.jpg *.jpeg)"
)
if files:
self.selected_files = files
self.files_label.setText(f"Selected {len(files)} images")
self.process_button.setEnabled(True)
def process_files(self):
if not self.selected_files:
return
self.process_button.setEnabled(False)
self.file_button.setEnabled(False)
processor = LLMProcessor(
self.api_url.text(),
self.api_password.text(),
self.instruction.toPlainText()
)
self.thread = ProcessingThread(processor, self.selected_files)
self.thread.progress.connect(self.update_progress)
self.thread.finished.connect(self.processing_finished)
self.thread.error.connect(self.processing_error)
self.thread.start()
def update_progress(self, current, total):
self.progress.setValue(int((current / total) * 100))
def processing_finished(self):
self.process_button.setEnabled(True)
self.file_button.setEnabled(True)
self.files_label.setText("Processing completed")
self.progress.setValue(0)
def processing_error(self, error_msg):
self.process_button.setEnabled(True)
self.file_button.setEnabled(True)
self.files_label.setText(f"Error: {error_msg}")
self.progress.setValue(0)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()