Didier commited on
Commit
8ba2238
·
verified ·
1 Parent(s): 6717123

Upload module_ocr2.py

Browse files
Files changed (1) hide show
  1. module_ocr2.py +94 -0
module_ocr2.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File: module_mistral_ocr.py
3
+
4
+ Description: Gradio module to interact the Mistral_OCR model.
5
+
6
+ Author: Didier Guillevic
7
+ Date: 2025-04-07
8
+ """
9
+
10
+ import gradio as gr
11
+ import os
12
+ import magic
13
+
14
+ import ocr2 # OCR with software 2.0 models
15
+
16
+ #
17
+ # Get file type: PDF or Image or something else
18
+ #
19
+ def get_file_type(file_path):
20
+ # Check file extension
21
+ file_extension = os.path.splitext(file_path)[1].lower()
22
+
23
+ # Check MIME type
24
+ mime = magic.Magic(mime=True)
25
+ mime_type = mime.from_file(file_path)
26
+
27
+ # Determine file type
28
+ if file_extension == '.pdf' or mime_type == 'application/pdf':
29
+ return 'PDF'
30
+ elif file_extension in ['.jpg', '.jpeg', '.png', '.gif'] or mime_type.startswith('image/'):
31
+ return 'Image'
32
+ elif file_extension == '.pptx' or mime_type == 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
33
+ return 'PowerPoint'
34
+ else:
35
+ return 'Other'
36
+
37
+ #
38
+ # Process one file
39
+ #
40
+ def process(input_file: str):
41
+ """Process given file with OCR using given languages."
42
+ """
43
+ file_type = get_file_type(input_file)
44
+ if file_type == 'PDF':
45
+ return ocr2.process_pdf(input_file)
46
+ elif file_type == 'Image':
47
+ return ocr2.process_image(input_file)
48
+ else:
49
+ return "Unsupported file type. Please upload a PDF, or an image file."
50
+ return ocr2.process(input_file)
51
+
52
+
53
+ #
54
+ # User interface
55
+ #
56
+ with gr.Blocks() as demo:
57
+
58
+ # Upload file to process
59
+ with gr.Row():
60
+ input_file = gr.File(label="Upload a PDF file", scale=1)
61
+ output_text = gr.Textbox(label="OCR output", scale=2)
62
+
63
+ # Buttons
64
+ with gr.Row():
65
+ ocr_btn = gr.Button(value="OCR", variant="primary")
66
+ clear_btn = gr.Button("Clear", variant="secondary")
67
+
68
+ # Examples
69
+ with gr.Accordion("Examples", open=False):
70
+ examples = gr.Examples(
71
+ [
72
+ ['./scanned_doc.pdf',],
73
+ ],
74
+ inputs=[input_file,],
75
+ outputs=[output_text,],
76
+ fn=process,
77
+ cache_examples=False,
78
+ label="Examples"
79
+ )
80
+
81
+ # Functions
82
+ ocr_btn.click(
83
+ fn=process,
84
+ inputs=[input_file,],
85
+ outputs=[output_text,]
86
+ )
87
+ clear_btn.click(
88
+ fn=lambda : (None, ''),
89
+ inputs=[],
90
+ outputs=[input_file, output_text] # input_file, output_text
91
+ )
92
+
93
+ if __name__ == '__main__':
94
+ demo.launch()