Genzo1010 commited on
Commit
0320019
·
verified ·
1 Parent(s): 4d11781

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -64
app.py CHANGED
@@ -1,80 +1,98 @@
1
- import logging
2
- from fastapi import FastAPI, File, UploadFile, HTTPException
3
- from fastapi.middleware.cors import CORSMiddleware
 
 
 
 
 
 
 
 
 
 
4
  from paddleocr import PaddleOCR
5
  from doctr.io import DocumentFile
6
  from doctr.models import ocr_predictor
7
- import numpy as np
8
- from PIL import Image
9
- import io
10
-
11
- # Set up logging
12
- logging.basicConfig(level=logging.INFO)
13
- logger = logging.getLogger(__name__)
14
 
15
- app = FastAPI()
16
 
17
- app.add_middleware(
18
- CORSMiddleware,
19
- allow_origins=["*"],
20
- allow_credentials=True,
21
- allow_methods=["*"],
22
- allow_headers=["*"],
23
- )
24
 
25
- # Initialize models once at startup
26
  ocr_model = ocr_predictor(pretrained=True)
27
- paddle_ocr = PaddleOCR(lang='en', use_angle_cls=True)
28
 
 
 
 
 
 
29
  def ocr_with_doctr(file):
30
  text_output = ''
31
- try:
32
- logger.info("Processing PDF with Doctr...")
33
- doc = DocumentFile.from_pdf(file)
34
- result = ocr_model(doc)
35
- for page in result.pages:
36
- for block in page.blocks:
37
- for line in block.lines:
38
- text_output += " ".join([word.value for word in line.words]) + "\n"
39
- except Exception as e:
40
- logger.error(f"Error processing PDF: {e}")
41
- raise HTTPException(status_code=500, detail=f"Error processing PDF: {e}")
 
 
42
  return text_output
43
 
 
 
 
44
  def ocr_with_paddle(img):
45
  finaltext = ''
46
- try:
47
- logger.info("Processing image with PaddleOCR...")
48
- result = paddle_ocr.ocr(img)
49
- for i in range(len(result[0])):
50
- text = result[0][i][1][0]
51
- finaltext += ' ' + text
52
- except Exception as e:
53
- logger.error(f"Error processing image: {e}")
54
- raise HTTPException(status_code=500, detail=f"Error processing image: {e}")
55
  return finaltext
56
 
57
- @app.post("/ocr/")
58
- async def perform_ocr(file: UploadFile = File(...)):
59
- try:
60
- logger.info(f"Received file: {file.filename}")
61
- file_bytes = await file.read()
62
-
63
- if file.filename.endswith('.pdf'):
64
- logger.info("Detected PDF file")
65
- text_output = ocr_with_doctr(io.BytesIO(file_bytes))
66
- else:
67
- logger.info("Detected image file")
68
- img = np.array(Image.open(io.BytesIO(file_bytes)))
69
- text_output = ocr_with_paddle(img)
70
-
71
- logger.info("OCR completed successfully")
72
- return {"ocr_text": text_output}
73
-
74
- except Exception as e:
75
- logger.error(f"Internal server error: {e}")
76
- raise HTTPException(status_code=500, detail=f"Internal server error: {e}")
77
-
78
- @app.get("/test/")
79
- async def test_call():
80
- return {"message": "Hi. I'm running"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  from doctr.models import ocr_predictor
 
 
 
 
 
 
 
17
 
 
18
 
 
 
 
 
 
 
 
19
 
 
20
  ocr_model = ocr_predictor(pretrained=True)
 
21
 
22
+
23
+
24
+ """
25
+ Perform OCR with doctr
26
+ """
27
  def ocr_with_doctr(file):
28
  text_output = ''
29
+
30
+ # Load the document
31
+ doc = DocumentFile.from_pdf(file)
32
+
33
+ # Perform OCR
34
+ result = ocr_model(doc)
35
+
36
+ # Extract text from OCR result
37
+ for page in result.pages:
38
+ for block in page.blocks:
39
+ for line in block.lines:
40
+ text_output += " ".join([word.value for word in line.words]) + "\n"
41
+
42
  return text_output
43
 
44
+ """
45
+ Paddle OCR
46
+ """
47
  def ocr_with_paddle(img):
48
  finaltext = ''
49
+ ocr = PaddleOCR(lang='en', use_angle_cls=True)
50
+ # img_path = 'exp.jpeg'
51
+ result = ocr.ocr(img)
52
+
53
+ for i in range(len(result[0])):
54
+ text = result[0][i][1][0]
55
+ finaltext += ' '+ text
 
 
56
  return finaltext
57
 
58
+ def generate_ocr(Method, file):
59
+ text_output = ''
60
+ if isinstance(file, bytes): # Handle file uploaded as bytes
61
+ file = io.BytesIO(file)
62
+
63
+ if file.name.endswith('.pdf'):
64
+ # Perform OCR on the PDF using doctr
65
+ text_output = ocr_with_doctr(file)
66
+
67
+ else:
68
+ # Handle image file
69
+ img_np = np.array(Image.open(file))
70
+ text_output = generate_text_from_image(Method, img_np)
71
+
72
+ return text_output
73
+
74
+ def generate_text_from_image(Method, img):
75
+ text_output = ''
76
+ if Method == 'PaddleOCR':
77
+ text_output = ocr_with_paddle(img)
78
+ return text_output
79
+
80
+
81
+ import gradio as gr
82
+
83
+ image_or_pdf = gr.File(label="Upload an image or PDF")
84
+ method = gr.Radio(["PaddleOCR"], value="PaddleOCR")
85
+ output = gr.Textbox(label="Output")
86
+
87
+ demo = gr.Interface(
88
+ generate_ocr,
89
+ [method, image_or_pdf],
90
+ output,
91
+ title="Optical Character Recognition",
92
+ css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
93
+ article="""<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
94
+ <a href="mailto:[email protected]" target="_blank">[email protected]</a>
95
+ <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
96
+ )
97
+
98
+ demo.launch(show_error=True)