Genzo1010 commited on
Commit
ae57e90
·
verified ·
1 Parent(s): 3b0b80c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -100
app.py CHANGED
@@ -1,120 +1,102 @@
1
- # import gradio as gr
2
- # import tensorflow as tf
3
- # import requests
4
- # import os
5
- # import numpy as np
6
- # import pandas as pd
7
- # import huggingface_hub
8
- # from huggingface_hub import Repository
9
- # from datetime import datetime
10
- # import scipy.ndimage.interpolation as inter
11
- # import datasets
12
- # from datasets import load_dataset, Image
13
- # from PIL import Image
14
- # from paddleocr import PaddleOCR
15
- # from doctr.io import DocumentFile
16
 
17
 
18
- # os.environ["CUDA_VISIBLE_DEVICES"] = "0" # Use GPU 0, adjust if needed
19
- # os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"
20
 
21
 
22
- # from doctr.models import ocr_predictor
23
- # model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
24
 
25
 
26
 
27
- # """
28
- # Perform OCR with doctr
29
- # """
30
- # def ocr_with_doctr(file):
31
- # text_output = ''
32
 
33
- # # Load the document
34
- # doc = DocumentFile.from_pdf(file)
35
 
36
- # # Perform OCR
37
- # result = ocr_model(doc)
38
 
39
- # # Extract text from OCR result
40
- # for page in result.pages:
41
- # for block in page.blocks:
42
- # for line in block.lines:
43
- # text_output += " ".join([word.value for word in line.words]) + "\n"
44
 
45
- # return text_output
46
-
47
- # """
48
- # Paddle OCR
49
- # """
50
- # def ocr_with_paddle(img):
51
- # finaltext = ''
52
- # ocr = PaddleOCR(lang='en', use_angle_cls=True, use_gpu=True)
53
- # # img_path = 'exp.jpeg'
54
- # result = ocr.ocr(img)
55
 
56
- # for i in range(len(result[0])):
57
- # text = result[0][i][1][0]
58
- # finaltext += ' '+ text
59
- # return finaltext
60
-
61
- # def generate_ocr(Method, file):
62
- # text_output = ''
63
- # if isinstance(file, bytes): # Handle file uploaded as bytes
64
- # file = io.BytesIO(file)
65
-
66
- # if file.name.endswith('.pdf'):
67
- # # Perform OCR on the PDF using doctr
68
- # text_output = ocr_with_doctr(file)
69
-
70
- # else:
71
- # # Handle image file
72
- # img_np = np.array(Image.open(file))
73
- # text_output = generate_text_from_image(Method, img_np)
74
 
75
- # return text_output
76
-
77
- # def generate_text_from_image(Method, img):
78
- # text_output = ''
79
- # if Method == 'PaddleOCR':
80
- # text_output = ocr_with_paddle(img)
81
- # return text_output
82
-
83
-
84
- # import gradio as gr
85
 
86
- # image_or_pdf = gr.File(label="Upload an image or PDF")
87
- # method = gr.Radio(["PaddleOCR"], value="PaddleOCR")
88
- # output = gr.Textbox(label="Output")
 
 
89
 
90
- # demo = gr.Interface(
91
- # generate_ocr,
92
- # [method, image_or_pdf],
93
- # output,
94
- # title="Optical Character Recognition",
95
- # css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
96
- # article="""<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
97
- # <a href="mailto:[email protected]" target="_blank">[email protected]</a>
98
- # <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
99
- # )
100
 
101
- # demo.launch(share=True)
102
 
103
- import subprocess
104
-
105
- try:
106
- nvcc_version = subprocess.check_output(["nvcc", "--version"]).decode("utf-8")
107
- print("CUDA Version:", nvcc_version)
108
- except FileNotFoundError:
109
- print("nvcc not found. CUDA may not be installed or not in the system's PATH.")
110
-
111
- import paddle
112
- print("PaddlePaddle Version:", paddle.__version__)
113
- print("Is GPU available:", paddle.is_compiled_with_cuda())
114
- import tensorflow as tf
115
- print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
116
- import tensorflow as tf
117
- print("Where tensorflow is built with cuda:",tf.test.is_built_with_cuda())
118
 
 
 
 
 
 
 
 
 
 
 
119
 
 
120
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import requests
4
+ import os
5
+ import numpy as np
6
+ import pandas as pd
7
+ import huggingface_hub
8
+ from huggingface_hub import Repository
9
+ from datetime import datetime
10
+ import scipy.ndimage.interpolation as inter
11
+ import datasets
12
+ from datasets import load_dataset, Image
13
+ from PIL import Image
14
+ from paddleocr import PaddleOCR
15
+ from doctr.io import DocumentFile
16
 
17
 
18
+ os.environ["CUDA_VISIBLE_DEVICES"] = "0" # Use GPU 0, adjust if needed
19
+ os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"
20
 
21
 
22
+ from doctr.models import ocr_predictor
23
+ model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
24
 
25
 
26
 
27
+ """
28
+ Perform OCR with doctr
29
+ """
30
+ def ocr_with_doctr(file):
31
+ text_output = ''
32
 
33
+ # Load the document
34
+ doc = DocumentFile.from_pdf(file)
35
 
36
+ # Perform OCR
37
+ result = ocr_model(doc)
38
 
39
+ # Extract text from OCR result
40
+ for page in result.pages:
41
+ for block in page.blocks:
42
+ for line in block.lines:
43
+ text_output += " ".join([word.value for word in line.words]) + "\n"
44
 
45
+ return text_output
46
+
47
+ """
48
+ Paddle OCR
49
+ """
50
+ def ocr_with_paddle(img):
51
+ finaltext = ''
52
+ ocr = PaddleOCR(lang='en', use_angle_cls=True, use_gpu=True)
53
+ # img_path = 'exp.jpeg'
54
+ result = ocr.ocr(img)
55
 
56
+ for i in range(len(result[0])):
57
+ text = result[0][i][1][0]
58
+ finaltext += ' '+ text
59
+ return finaltext
60
+
61
+ def generate_ocr(Method, file):
62
+ text_output = ''
63
+ if isinstance(file, bytes): # Handle file uploaded as bytes
64
+ file = io.BytesIO(file)
65
+
66
+ if file.name.endswith('.pdf'):
67
+ # Perform OCR on the PDF using doctr
68
+ text_output = ocr_with_doctr(file)
69
+
70
+ else:
71
+ # Handle image file
72
+ img_np = np.array(Image.open(file))
73
+ text_output = generate_text_from_image(Method, img_np)
74
 
75
+ return text_output
 
 
 
 
 
 
 
 
 
76
 
77
+ def generate_text_from_image(Method, img):
78
+ text_output = ''
79
+ if Method == 'PaddleOCR':
80
+ text_output = ocr_with_paddle(img)
81
+ return text_output
82
 
 
 
 
 
 
 
 
 
 
 
83
 
84
+ import gradio as gr
85
 
86
+ image_or_pdf = gr.File(label="Upload an image or PDF")
87
+ method = gr.Radio(["PaddleOCR"], value="PaddleOCR")
88
+ output = gr.Textbox(label="Output")
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
+ demo = gr.Interface(
91
+ generate_ocr,
92
+ [method, image_or_pdf],
93
+ output,
94
+ title="Optical Character Recognition",
95
+ css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
96
+ article="""<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
97
+ <a href="mailto:[email protected]" target="_blank">[email protected]</a>
98
+ <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
99
+ )
100
 
101
+ demo.launch(share=True)
102