-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
1504 lines (1245 loc) · 61.5 KB
/
script.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
#os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import gradio as gr
import json
import pyautogui
import screeninfo
from transformers import Owlv2Processor, Owlv2ForObjectDetection, AutoModel, AutoTokenizer, AutoProcessor, PaliGemmaForConditionalGeneration, AutoModelForCausalLM
from PIL import Image, ImageDraw
import torch
import os
import shutil
import re # Import the re module for regular expression operations
import json
import time
import threading
import pdf2image # Add this import for PDF to PNG conversion
import uuid
import tempfile
import time
import shutil
from pathlib import Path
import time
import pypdfium2 # Needs to be at the top to avoid warnings
import os
import gradio as gr
import spaces
from transformers import AutoModel, AutoTokenizer
from PIL import Image
import numpy as np
import base64
import io
import uuid
import tempfile
import subprocess
import time
import shutil
from pathlib import Path
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1" # For some reason, transformers decided to use .isin for a simple op, which is not supported on MPS
import gc
from marker.convert import convert_single_pdf
from marker.logger import configure_logging
from marker.models import load_all_models
from marker.output import save_markdown
configure_logging()
oob_tasks_file = "extensions/Lucid_Autonomy/oob_tasks.json"
# Configurable variables
MONITOR_INDEX = 0 # Index of the monitor to capture (0 for the first monitor, 1 for the second monitor, etc.)
TEXT_QUERIES = "colored hyperlink text,clickable icons,text bar field,clickable UI buttons,UI tabs,Interactive UI Elements,blue text hyperlink" # Comma-separated text queries for object detection
SCORE_THRESHOLD = 0.24 # Score threshold for filtering low-probability predictions
VISION_MODEL_QUESTION = "Keep your response to less than 5 sentences: What is the text in this image or what does this image describe? The AI is aiding in helping those with visual handicaps so the AI will focus on providing all of the relevant information for the listener."
FULL_IMAGE_VISION_MODEL_QUESTION = "This is a computer screenshot, identify all article text if the screen is displaying an article. Describe what you see in this image, if you see UI elements describe them and their general location and if you see text read the text verbatim. Describe the general theme and context of the image. You are aiding in helping those with visual handicaps so you need to try and focus on providing all of the relevant text on a screen and contextualizing it for the listener."
# Use GPU if available
device = 0
# Global variables for models
owlv2_model = None
owlv2_processor = None
minicpm_llama_model = None
minicpm_llama_tokenizer = None
chartgemma_model = None
chartgemma_processor = None
got_ocr_model = None
got_ocr_tokenizer = None
VISION_MODEL_ID = "/home/myself/Desktop/miniCPM_llava3V/MiniCPM-V-2_6/"
GOT_OCR_MODEL_PATH = '/home/myself/Desktop/GOT_OCR/ModelGOT-OCR2_0/'
# Global variable to store the file path of the selected image
selected_image_path = None
selected_markdown_path = None
# Function to set the CUDA_VISIBLE_DEVICES environment variable
def set_cuda_visible_devices(device_id):
os.environ["CUDA_VISIBLE_DEVICES"] = str(device_id)
# Function to reset the CUDA_VISIBLE_DEVICES environment variable
def reset_cuda_visible_devices():
os.environ["CUDA_VISIBLE_DEVICES"] = ""
def process_marker_file(filename, output_folder, max_pages=None, start_page=None, langs=None, batch_multiplier=2, ocr_all_pages=False):
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
torch.cuda.set_device(0) # Explicitly set CUDA device
model_lst = load_all_models(device="cuda:0")
start = time.time()
full_text, images, out_meta = convert_single_pdf(filename, model_lst, max_pages=max_pages, langs=langs, batch_multiplier=batch_multiplier, start_page=start_page, ocr_all_pages=ocr_all_pages)
fname = os.path.basename(filename)
subfolder_path = save_markdown(output_folder, fname, full_text, images, out_meta)
print(f"Saved markdown to the {subfolder_path} folder")
print(f"Total time: {time.time() - start}")
return subfolder_path
def generate_image_list(folder_path):
"""
Generates a list of image files in the specified folder.
Args:
folder_path (str): The path to the folder containing the image files.
Returns:
list: A list of image file paths.
"""
image_list = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".png"):
image_list.append(os.path.join(root, file))
return image_list
def match_image_references(md_file_path, image_list):
"""
Matches image references in the markdown file with the actual image files.
Args:
md_file_path (str): The path to the markdown file.
image_list (list): A list of image file paths.
Returns:
dict: A dictionary mapping image references to their disk locations.
"""
image_references = {}
with open(md_file_path, 'r') as md_file:
content = md_file.read()
for image_path in image_list:
image_name = os.path.basename(image_path)
if image_name in content:
image_references[image_name] = image_path
return image_references
def add_disk_location_info(md_file_path, image_references):
"""
Adds disk location information and vision model response to the markdown file for each image reference.
Args:
md_file_path (str): The path to the markdown file.
image_references (dict): A dictionary mapping image references to their disk locations.
"""
with open(md_file_path, 'r') as md_file:
content = md_file.read()
# Load the miniCPM-lama3-v-2_5 model
load_minicpm_llama_model()
for image_name, image_path in image_references.items():
# Process the image with the vision model to identify the image type
question = "Identify if this image is a chart, table, equation, graph, bar chart, illustration, drawing, or picture. Respond with either 'illustration', 'graph', 'table', 'chart', 'equation' or 'bar chart'."
# Process the entire screenshot with the minicpm model
vision_model_response = process_with_vision_model(image_path, question)
# Replace the image reference with disk location and vision model response
content = content.replace(f"", f"\n\nDisk Location: {image_path}\n\nInitial Image ID: {vision_model_response}\n\n")
with open(md_file_path, 'w') as md_file:
md_file.write(content)
# Unload the miniCPM-lama3-v-2_5 model after processing the full image
unload_minicpm_llama_model()
def load_owlv2_model():
global owlv2_model, owlv2_processor
owlv2_model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-base-patch16-ensemble", trust_remote_code=True).to(device)
owlv2_processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16-ensemble", trust_remote_code=True)
print("Owlv2 model loaded.")
def unload_owlv2_model():
global owlv2_model, owlv2_processor
if owlv2_model is not None:
del owlv2_model
del owlv2_processor
torch.cuda.empty_cache()
print("Owlv2 model unloaded.")
def load_minicpm_llama_model():
global minicpm_llama_model, minicpm_llama_tokenizer
if "int4" in VISION_MODEL_ID:
# Load the 4-bit quantized model and tokenizer
minicpm_llama_model = AutoModel.from_pretrained(
VISION_MODEL_ID,
device_map={"": device},
trust_remote_code=True,
torch_dtype=torch.float16 # Use float16 as per the example code for 4-bit models
).eval()
minicpm_llama_tokenizer = AutoTokenizer.from_pretrained(
VISION_MODEL_ID,
trust_remote_code=True
)
print("MiniCPM-Llama3 4-bit quantized model loaded on-demand.")
else:
# Load the standard model and tokenizer
minicpm_llama_model = AutoModel.from_pretrained(
VISION_MODEL_ID,
device_map={"": device}, # Use the specified CUDA device
trust_remote_code=True,
torch_dtype=torch.bfloat16
).eval()
minicpm_llama_tokenizer = AutoTokenizer.from_pretrained(
VISION_MODEL_ID,
trust_remote_code=True
)
print("MiniCPM-Llama3 standard model loaded on-demand.")
def unload_minicpm_llama_model():
global minicpm_llama_model, minicpm_llama_tokenizer
if minicpm_llama_model is not None:
del minicpm_llama_model
del minicpm_llama_tokenizer
torch.cuda.empty_cache()
print("MiniCPM-Llama3 model unloaded.")
def load_chartgemma_model():
global chartgemma_model, chartgemma_processor
chartgemma_model = PaliGemmaForConditionalGeneration.from_pretrained("ahmed-masry/chartgemma").to(device)
chartgemma_processor = AutoProcessor.from_pretrained("ahmed-masry/chartgemma")
print("ChartGemma model loaded.")
def unload_chartgemma_model():
global chartgemma_model, chartgemma_processor
if chartgemma_model is not None:
del chartgemma_model
del chartgemma_processor
torch.cuda.empty_cache()
print("ChartGemma model unloaded.")
def load_got_ocr_model():
global got_ocr_model, got_ocr_tokenizer
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
torch.cuda.set_device(0) # Explicitly set CUDA device
got_ocr_model = AutoModel.from_pretrained(GOT_OCR_MODEL_PATH, trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda:0', use_safetensors=True).eval().cuda()
got_ocr_tokenizer = AutoTokenizer.from_pretrained(GOT_OCR_MODEL_PATH, trust_remote_code=True)
print("GOT-OCR model loaded.")
def unload_got_ocr_model():
global got_ocr_model, got_ocr_tokenizer
if got_ocr_model is not None:
del got_ocr_model
del got_ocr_tokenizer
torch.cuda.empty_cache()
print("GOT-OCR model unloaded.")
def take_screenshot(monitor_index, text_queries, score_threshold, vision_model_question, full_image_vision_model_question, output_filename="screenshot.png"):
"""
Takes a screenshot of a specific monitor in a multi-monitor setup.
Args:
monitor_index (int): The index of the monitor to capture.
text_queries (str): Comma-separated text queries for object detection.
score_threshold (float): The score threshold for filtering low-probability predictions.
vision_model_question (str): The question to ask the vision model for cropped images.
full_image_vision_model_question (str): The question to ask the vision model for the full image.
output_filename (str): The filename to save the screenshot as (default is "screenshot.png").
"""
monitor_index = int(monitor_index)
score_threshold = float(score_threshold)
# Get information about all monitors
monitors = screeninfo.get_monitors()
if monitor_index >= len(monitors):
raise ValueError(f"Monitor index {monitor_index} is out of range. There are only {len(monitors)} monitors.")
# Get the region of the specified monitor
monitor = monitors[monitor_index]
region = (monitor.x, monitor.y, monitor.width, monitor.height)
# Take the screenshot
output_dir = "extensions/Lucid_Autonomy"
os.makedirs(output_dir, exist_ok=True)
screenshot_path = os.path.join(output_dir, output_filename)
screenshot = pyautogui.screenshot(region=region)
screenshot.save(screenshot_path)
print(f"Screenshot saved as {screenshot_path}")
# Process the image using the Owlv2 model
annotated_image_path, human_annotated_image_path, json_output_path = query_image(screenshot_path, text_queries, score_threshold)
# Load the miniCPM-lama3-v-2_5 model
load_minicpm_llama_model()
# Process the entire screenshot with the minicpm model
full_image_response = process_with_vision_model(annotated_image_path, full_image_vision_model_question)
# Unload the miniCPM-lama3-v-2_5 model after processing the full image
unload_minicpm_llama_model()
# Create the output directory if it doesn't exist
output_dir = "extensions/Lucid_Autonomy/ImageOutputTest"
os.makedirs(output_dir, exist_ok=True)
# Clear the output directory
for filename in os.listdir(output_dir):
file_path = os.path.join(output_dir, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print(f"Failed to delete {file_path}. Reason: {e}")
# Crop the detected objects and save them to the output directory
crop_images(annotated_image_path, json_output_path, output_dir, full_image_response)
print("Processing complete. Check the output directory for the cropped images and results.")
def query_image(img_path, text_queries, score_threshold):
"""
Processes the image using the Owlv2 model to detect objects based on text queries.
Args:
img_path (str): The path to the input image.
text_queries (str): Comma-separated text queries for object detection.
score_threshold (float): The score threshold for filtering low-probability predictions.
"""
load_owlv2_model()
img = Image.open(img_path)
text_queries = text_queries.split(",")
size = max(img.size)
target_sizes = torch.Tensor([[size, size]])
inputs = owlv2_processor(text=text_queries, images=img, return_tensors="pt").to(device)
with torch.no_grad():
outputs = owlv2_model(**inputs)
outputs.logits = outputs.logits.cpu()
outputs.pred_boxes = outputs.pred_boxes.cpu()
results = owlv2_processor.post_process_object_detection(outputs=outputs, target_sizes=target_sizes)
boxes, scores, labels = results[0]["boxes"], results[0]["scores"], results[0]["labels"]
result_labels = []
img_pil = img.copy()
draw = ImageDraw.Draw(img_pil)
human_img_pil = img.copy()
human_draw = ImageDraw.Draw(human_img_pil)
for box, score, label in zip(boxes, scores, labels):
box = [int(i) for i in box.tolist()]
if score < score_threshold:
continue
result_labels.append([box, text_queries[label.item()]])
# Do not draw the text label within the bounding box
# draw.text((box[0], box[1]), text_queries[label.item()], fill="red")
# Draw the boxes for the human-annotated image
human_draw.rectangle(box, outline="red", width=2)
# Save the annotated image for debugging
annotated_image_path = "extensions/Lucid_Autonomy/screenshot.png"
img_pil.save(annotated_image_path)
# Save the human-annotated image for human inspection
human_annotated_image_path = "extensions/Lucid_Autonomy/Human_annotated_image.png"
human_img_pil.save(human_annotated_image_path)
# Save the JSON output to a file
json_output_path = "extensions/Lucid_Autonomy/output.json"
with open(json_output_path, 'w') as f:
json.dump(result_labels, f)
unload_owlv2_model()
return annotated_image_path, human_annotated_image_path, json_output_path
def crop_images(image_path, json_output_path, output_dir="extensions/Lucid_Autonomy/ImageOutputTest", full_image_response=""):
"""
Crops out the detected objects from the input image based on the bounding box coordinates.
Args:
image_path (str): The path to the input image.
json_output_path (str): The path to the JSON file containing the bounding box coordinates.
output_dir (str): The directory to save the cropped images (default is "extensions/Lucid_Autonomy/ImageOutputTest").
full_image_response (str): The response from the vision model for the full image.
"""
with open(json_output_path, 'r') as f:
data = json.load(f)
img = Image.open(image_path)
results = []
# Load the miniCPM-lama3-v-2_5 model once before processing the images
load_minicpm_llama_model()
for i, item in enumerate(data):
box = item[0]
label = item[1]
cropped_img = img.crop(box)
cropped_img_path = f"{output_dir}/{label}_{i}.png"
cropped_img.save(cropped_img_path)
# Process the cropped image with the miniCPM-lama3-v-2_5 model
vision_model_response = process_with_vision_model(cropped_img_path, VISION_MODEL_QUESTION)
# Store the results in a structured format
results.append({
"image_name": f"{label}_{i}.png",
"coordinates": box,
"description": vision_model_response,
"children": []
})
# Unload the miniCPM-lama3-v-2_5 model after processing all images
unload_minicpm_llama_model()
# Determine parent-child relationships
for i, result in enumerate(results):
for j, other_result in enumerate(results):
if i != j:
box1 = result["coordinates"]
box2 = other_result["coordinates"]
center_x1 = (box1[0] + box1[2]) // 2
center_y1 = (box1[1] + box1[3]) // 2
if box2[0] <= center_x1 <= box2[2] and box2[1] <= center_y1 <= box2[3]:
other_result["children"].append(result["image_name"])
# Save the results to a JSON file
results_json_path = f"{output_dir}/results.json"
with open(results_json_path, 'w') as f:
json.dump(results, f, indent=4)
# Add the full image response to the results
if full_image_response:
results.append({
"image_name": "full_image.png",
"coordinates": [],
"description": full_image_response,
"children": []
})
# Save the updated results to the JSON file
with open(results_json_path, 'w') as f:
json.dump(results, f, indent=4)
def process_with_vision_model(image_path, question):
"""
Processes the image with the miniCPM-lama3-v-2_5 model and returns the response.
Args:
image_path (str): The path to the input image.
question (str): The question to ask the vision model.
Returns:
str: The response from the vision model.
"""
image = Image.open(image_path).convert("RGB")
messages = [
{"role": "user", "content": f"{question}"}
]
# Define the generation arguments
generation_args = {
"max_new_tokens": 1024,
"repetition_penalty": 1.05,
"num_beams": 3,
"top_p": 0.9,
"top_k": 1,
"temperature": 0.1,
"sampling": True,
}
if "int4" in VISION_MODEL_ID:
# Disable streaming for the 4-bit model
generation_args["stream"] = False
# Use the model.chat method with streaming enabled
vision_model_response = ""
for new_text in minicpm_llama_model.chat(
image=image,
msgs=messages,
tokenizer=minicpm_llama_tokenizer,
**generation_args
):
vision_model_response += new_text
print(new_text, flush=True, end='')
else:
minicpm_llama_model.to(device)
vision_model_response = minicpm_llama_model.chat(
image=image,
msgs=messages,
tokenizer=minicpm_llama_tokenizer,
**generation_args
)
return vision_model_response
def process_with_chartgemma_model(image_path, question):
"""
Processes the image with the ChartGemma model and returns the response.
Args:
image_path (str): The path to the input image.
question (str): The question to ask the vision model.
Returns:
str: The response from the vision model.
"""
image = Image.open(image_path).convert("RGB")
inputs = chartgemma_processor(text=question, images=image, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}
prompt_length = inputs['input_ids'].shape[1]
# Generate
generate_ids = chartgemma_model.generate(**inputs, max_new_tokens=512)
output_text = chartgemma_processor.batch_decode(generate_ids[:, prompt_length:], skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
return output_text
def convert_pdf_to_png(pdf_path, output_folder):
"""
Converts each page of the input PDF to a PNG file and saves them in the specified output folder.
Args:
pdf_path (str): The path to the input PDF file.
output_folder (str): The path to the output folder where the PNG files will be saved.
"""
os.makedirs(output_folder, exist_ok=True)
images = pdf2image.convert_from_path(pdf_path)
for i, image in enumerate(images):
image.save(os.path.join(output_folder, f"page_{i+1}.png"), "PNG")
# Define the base directories
BASE_UPLOAD_FOLDER = "extensions/Lucid_Autonomy/MarkerOutput"
BASE_RESULTS_FOLDER = "extensions/Lucid_Autonomy/MarkerOutput"
def run_got_ocr(image_path, got_mode, fine_grained_mode="", ocr_color="", ocr_box=""):
# Extract the PDF name from the image path
pdf_name = os.path.basename(os.path.dirname(image_path))
# Define the upload and results folders based on the PDF name
UPLOAD_FOLDER = os.path.join(BASE_UPLOAD_FOLDER, pdf_name)
RESULTS_FOLDER = os.path.join(BASE_RESULTS_FOLDER, pdf_name)
# Ensure the directories exist
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(RESULTS_FOLDER, exist_ok=True)
unique_id = str(uuid.uuid4())
temp_image_path = os.path.join(UPLOAD_FOLDER, f"{unique_id}.png")
result_path = os.path.join(RESULTS_FOLDER, f"{unique_id}.html")
shutil.copy(image_path, temp_image_path)
try:
if got_mode == "plain texts OCR":
res = got_ocr_model.chat(got_ocr_tokenizer, temp_image_path, ocr_type='ocr')
return res, None
elif got_mode == "format texts OCR":
res = got_ocr_model.chat(got_ocr_tokenizer, temp_image_path, ocr_type='format', render=True, save_render_file=result_path)
elif got_mode == "plain multi-crop OCR":
res = got_ocr_model.chat_crop(got_ocr_tokenizer, temp_image_path, ocr_type='ocr')
return res, None
elif got_mode == "format multi-crop OCR":
res = got_ocr_model.chat_crop(got_ocr_tokenizer, temp_image_path, ocr_type='format', render=True, save_render_file=result_path)
elif got_mode == "plain fine-grained OCR":
res = got_ocr_model.chat(got_ocr_tokenizer, temp_image_path, ocr_type='ocr', ocr_box=ocr_box, ocr_color=ocr_color)
return res, None
elif got_mode == "format fine-grained OCR":
res = got_ocr_model.chat(got_ocr_tokenizer, temp_image_path, ocr_type='format', ocr_box=ocr_box, ocr_color=ocr_color, render=True, save_render_file=result_path)
res_markdown = res
if "format" in got_mode and os.path.exists(result_path):
with open(result_path, 'r') as f:
html_content = f.read()
encoded_html = base64.b64encode(html_content.encode('utf-8')).decode('utf-8')
iframe_src = f"data:text/html;base64,{encoded_html}"
iframe = f'<iframe src="{iframe_src}" width="100%" height="600px"></iframe>'
download_link = f'<a href="data:text/html;base64,{encoded_html}" download="result_{unique_id}.html">Download Full Result</a>'
return res_markdown, f"{download_link}<br>{iframe}"
else:
return res_markdown, None
except Exception as e:
return f"Error: {str(e)}", None
finally:
if os.path.exists(temp_image_path):
os.remove(temp_image_path)
def compare_and_merge_texts(file1_path, file2_path, group_size, output_path):
# Read and clean both files
with open(file1_path, 'r') as f1, open(file2_path, 'r') as f2:
text1 = clean_text_doc2(f1.read())
text2 = clean_text_doc2(f2.read())
# Get word groups (case-insensitive for comparison)
groups1 = [group.lower() for group in get_word_groups(text1, group_size)]
groups2 = [group.lower() for group in get_word_groups(text2, group_size)]
# Find common groups
common_groups = set(groups1) & set(groups2)
# Remove common groups from text1 (case-sensitive removal)
for group in common_groups:
text1 = re.sub(re.escape(group), '', text1, flags=re.IGNORECASE)
# Merge unique parts of text1 into text2
merged_text = text2 + ' ' + text1.strip()
# Write the result to the output file
with open(output_path, 'w') as f_out:
f_out.write(merged_text)
print(f"Merged text has been written to {output_path}")
def clean_text_doc2(text):
# Preserve all special characters, punctuation, and capitalization...just dont ahhah
return text
def get_word_groups(text, group_size):
words = re.findall(r'\S+', text)
return [' '.join(words[i:i+group_size]) for i in range(len(words) - group_size + 1)]
# Global variables for Gradio inputs
global_vars = {
"global_monitor_index": "global_monitor_index",
"global_text_queries": TEXT_QUERIES,
"global_score_threshold": SCORE_THRESHOLD,
"global_vision_model_question": VISION_MODEL_QUESTION,
"global_full_image_vision_model_question": FULL_IMAGE_VISION_MODEL_QUESTION
}
def ui():
"""
Creates custom gradio elements when the UI is launched.
"""
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
monitor_index = gr.Textbox(label="Monitor Index", value=str(MONITOR_INDEX))
text_queries = gr.Textbox(label="Text Queries", value=TEXT_QUERIES)
score_threshold = gr.Textbox(label="Score Threshold", value=str(SCORE_THRESHOLD))
vision_model_question = gr.Textbox(label="Vision Model Question", value=VISION_MODEL_QUESTION)
full_image_vision_model_question = gr.Textbox(label="Full Image Vision Model Question", value=FULL_IMAGE_VISION_MODEL_QUESTION)
take_screenshot_button = gr.Button(value="Take Screenshot")
process_screenshot_button = gr.Button(value="Process Screenshot With GOT-OCR")
# File upload component
file_upload = gr.File(label="Upload PDF File")
# Radio checkbox for GOT-OCR
use_got_ocr = gr.Checkbox(label="Use GOT-OCR", value=False)
# Text box for group_size
group_size = gr.Textbox(label="Group Size", value="5")
# Checkbox for using results.json
use_results_json = gr.Checkbox(label="Use results.json", value=True)
# Button to clear results.json
clear_results_button = gr.Button(value="Clear results.json")
# New button to unload all models
unload_models_button = gr.Button(value="Unload All Models")
# Update global variables when the user changes the input fields
monitor_index.change(lambda x: global_vars.update({"global_monitor_index": int(x)}), inputs=monitor_index, outputs=None)
text_queries.change(lambda x: global_vars.update({"global_text_queries": x}), inputs=text_queries, outputs=None)
score_threshold.change(lambda x: global_vars.update({"global_score_threshold": float(x)}), inputs=score_threshold, outputs=None)
vision_model_question.change(lambda x: global_vars.update({"global_vision_model_question": x}), inputs=vision_model_question, outputs=None)
full_image_vision_model_question.change(lambda x: global_vars.update({"global_full_image_vision_model_question": x}), inputs=full_image_vision_model_question, outputs=None)
take_screenshot_button.click(
fn=take_screenshot,
inputs=[monitor_index, text_queries, score_threshold, vision_model_question, full_image_vision_model_question],
outputs=None
)
process_screenshot_button.click(
fn=process_screenshot_with_got_ocr,
inputs=[monitor_index],
outputs=None
)
# Handle file upload event
file_upload.upload(
fn=handle_file_upload_and_copy,
inputs=[file_upload, use_got_ocr, group_size],
outputs=None
)
# Add the checkbox state to the global variables
use_results_json.change(lambda x: global_vars.update({"use_results_json": x}), inputs=use_results_json, outputs=None)
# Handle clear results.json button click
clear_results_button.click(
fn=clear_results_json,
inputs=None,
outputs=None
)
# Click event handler for the new button
unload_models_button.click(
fn=unload_all_models,
inputs=None,
outputs=None
)
return demo
def handle_file_upload_and_copy(file, use_got_ocr, group_size):
"""
Handles the file upload event and creates a copy of the PDF.
"""
if file is not None:
file_path = file.name
output_folder = "extensions/Lucid_Autonomy/MarkerOutput"
os.makedirs(output_folder, exist_ok=True)
# Create a subfolder with the same name as the PDF
pdf_name = os.path.splitext(os.path.basename(file_path))[0]
pdf_subfolder = os.path.join(output_folder, pdf_name)
os.makedirs(pdf_subfolder, exist_ok=True)
# Copy the uploaded PDF to the subfolder
pdf_copy_path = os.path.join(pdf_subfolder, os.path.basename(file_path))
shutil.copy2(file_path, pdf_copy_path)
print(f"Created a copy of the PDF: {pdf_copy_path}")
# Continue with the rest of the file processing
handle_file_upload(file, use_got_ocr, group_size)
# Define the function to unload all models
def unload_all_models():
unload_owlv2_model()
unload_minicpm_llama_model()
unload_chartgemma_model()
unload_got_ocr_model()
torch.cuda.empty_cache()
print("All models unloaded.")
# Define the function to process the screenshot with GOT-OCR
def process_screenshot_with_got_ocr(monitor_index):
monitor_index = int(monitor_index)
# Get information about all monitors
monitors = screeninfo.get_monitors()
if monitor_index >= len(monitors):
raise ValueError(f"Monitor index {monitor_index} is out of range. There are only {len(monitors)} monitors.")
# Get the region of the specified monitor
monitor = monitors[monitor_index]
region = (monitor.x, monitor.y, monitor.width, monitor.height)
# Take the screenshot
output_dir = "extensions/Lucid_Autonomy"
os.makedirs(output_dir, exist_ok=True)
output_filename = "screenshot.png"
screenshot_path = os.path.join(output_dir, output_filename)
screenshot = pyautogui.screenshot(region=region)
screenshot.save(screenshot_path)
print(f"Screenshot saved as {screenshot_path}")
# Load the GOT-OCR model
load_got_ocr_model()
print("GOT-OCR model loaded.")
# Process the screenshot with GOT-OCR
got_ocr_result, _ = run_got_ocr(screenshot_path, got_mode="plain texts OCR")
print(f"GOT-OCR result: {got_ocr_result}")
# Save the GOT-OCR result to results.json
results_json_path = "extensions/Lucid_Autonomy/ImageOutputTest/results.json"
with open(results_json_path, 'w') as f:
json.dump([{"image_name": "screenshot.png", "description": got_ocr_result}], f, indent=4)
# Unload the GOT-OCR model
unload_got_ocr_model()
print("GOT-OCR model unloaded.")
print("Screenshot processed with GOT-OCR and results saved to results.json")
# Function to clear the results.json file
def clear_results_json():
"""
Clears the contents of the results.json file and replaces it with an empty list.
"""
results_json_path = "extensions/Lucid_Autonomy/ImageOutputTest/results.json"
with open(results_json_path, 'w') as f:
json.dump([], f)
print(f"Cleared the contents of {results_json_path}")
def handle_file_upload(file, use_got_ocr, group_size):
"""
Handles the file upload event.
"""
global selected_image_path
global selected_markdown_path
if file is not None:
file_path = file.name
output_folder = "extensions/Lucid_Autonomy/MarkerOutput"
os.makedirs(output_folder, exist_ok=True)
# Convert PDF to PNG
png_output_folder = os.path.join(output_folder, os.path.splitext(os.path.basename(file_path))[0])
os.makedirs(png_output_folder, exist_ok=True)
convert_pdf_to_png(file_path, png_output_folder)
if use_got_ocr:
# Process the PDF with Marker
process_marker_file(file_path, output_folder)
print(f"Processed file: {file_path}")
# Generate a list of images
image_list = generate_image_list(output_folder)
# Locate the markdown file within the subfolder
pdf_name = os.path.splitext(os.path.basename(file_path))[0]
md_file_path = os.path.join(output_folder, pdf_name, f"{pdf_name}.md")
# Match image references in the markdown file
image_references = match_image_references(md_file_path, image_list)
# Add disk location information and vision model response to the markdown file
add_disk_location_info(md_file_path, image_references)
print(f"Updated markdown file with disk location information and vision model response: {md_file_path}")
# Load GOT-OCR model
load_got_ocr_model()
# Process each PNG file with GOT-OCR, only selecting files with the format "page_#.png"
png_files = [os.path.join(png_output_folder, f) for f in os.listdir(png_output_folder) if re.match(r'^page_\d+\.png$', f)]
# Sort the PNG files based on the page number
png_files.sort(key=lambda x: int(re.search(r'page_(\d+)\.png', os.path.basename(x)).group(1)))
got_ocr_results = []
for png_file in png_files:
got_ocr_result, _ = run_got_ocr(png_file, got_mode="format texts OCR")
got_ocr_results.append(got_ocr_result)
# Unload GOT-OCR model
unload_got_ocr_model()
# Stitch GOT-OCR outputs into a single markdown file
got_ocr_output_path = os.path.join(png_output_folder, "got_ocr_output.md")
with open(got_ocr_output_path, 'w') as f_out:
for result in got_ocr_results:
f_out.write(result + "\n\n")
# Merge GOT-OCR output with Marker output
compare_and_merge_texts(got_ocr_output_path, md_file_path, group_size=int(group_size), output_path=md_file_path)
# Set the global variable for the selected image path
selected_image_path = md_file_path
selected_markdown_path = md_file_path
else:
# Process the PDF with Marker without GOT-OCR
process_marker_file(file_path, output_folder)
print(f"Processed file: {file_path}")
# Generate a list of images
image_list = generate_image_list(output_folder)
# Locate the markdown file within the subfolder
pdf_name = os.path.splitext(os.path.basename(file_path))[0]
md_file_path = os.path.join(output_folder, pdf_name, f"{pdf_name}.md")
# Match image references in the markdown file
image_references = match_image_references(md_file_path, image_list)
# Add disk location information and vision model response to the markdown file
add_disk_location_info(md_file_path, image_references)
print(f"Updated markdown file with disk location information and vision model response: {md_file_path}")
# Set the global variable for the selected image path
selected_image_path = md_file_path
selected_markdown_path = md_file_path
# Modify the input_modifier function
def input_modifier(user_input, state):
"""
Modifies the user input before it is processed by the LLM.
"""
global selected_image_path
global selected_markdown_path
# Check if a markdown file has been selected and stored in the global variable
if selected_markdown_path:
# Read the contents of the markdown file
with open(selected_markdown_path, 'r') as md_file:
markdown_content = md_file.read()
# Construct the message with the markdown content
combined_input = f"{user_input}\n\n{markdown_content}"
# Reset the selected markdown path to None after processing
selected_markdown_path = None
return combined_input
# If no markdown file is selected, return the user input as is
return user_input
aria_model = None
aria_processor = None
def load_aria_model():
global aria_model, aria_processor
if aria_model is None:
print("Loading ARIA model...")
aria_model = AutoModelForCausalLM.from_pretrained(
"/home/myself/Desktop/Aria/",
device_map="auto",
torch_dtype=torch.bfloat16,
trust_remote_code=True
)
aria_processor = AutoProcessor.from_pretrained(
"/home/myself/Desktop/Aria/",
trust_remote_code=True
)
print("ARIA model loaded successfully.")
else:
print("ARIA model already loaded.")
def unload_aria_model():
global aria_model, aria_processor
if aria_model is not None:
print("Unloading ARIA model...")
# Move model to CPU before deletion
aria_model.cpu()
# Delete the model and processor
del aria_model
del aria_processor
# Set to None to indicate they're unloaded
aria_model = None
aria_processor = None
# Clear CUDA cache
if torch.cuda.is_available():
torch.cuda.empty_cache()
# Run garbage collection
gc.collect()
print("ARIA model unloaded successfully.")
else:
print("ARIA model not loaded, nothing to unload.")
# Print current GPU memory usage
if torch.cuda.is_available():
print(f"Current GPU memory allocated: {torch.cuda.memory_allocated() / 1e9:.2f} GB")
print(f"Current GPU memory reserved: {torch.cuda.memory_reserved() / 1e9:.2f} GB")
def process_with_aria_model(image_path, question):
global aria_model, aria_processor
if aria_model is None:
load_aria_model()
print("Processing image with ARIA model...")
image = Image.open(image_path).convert("RGB")
messages = [
{"role": "user", "content": [
{"text": None, "type": "image"},
{"text": question, "type": "text"},
]}
]
text = aria_processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = aria_processor(text=text, images=image, return_tensors="pt")
inputs["pixel_values"] = inputs["pixel_values"].to(aria_model.dtype)
inputs = {k: v.to(aria_model.device) for k, v in inputs.items()}
with torch.inference_mode(), torch.cuda.amp.autocast(dtype=torch.bfloat16):
output = aria_model.generate(
**inputs,
max_new_tokens=1024,
stop_strings=["<|im_end|>"],
tokenizer=aria_processor.tokenizer,
do_sample=True,
temperature=0.9,
)
output_ids = output[0][inputs["input_ids"].shape[1]:]
result = aria_processor.decode(output_ids, skip_special_tokens=True)