Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from paddleocr import PaddleOCR
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Initialize PaddleOCR
|
| 6 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 7 |
+
|
| 8 |
+
# Load GPT-NeoX model and tokenizer
|
| 9 |
+
model_name = "EleutherAI/gpt-neox-20b"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
def ocr_and_correct(text):
|
| 14 |
+
# Function to correct text using GPT-NeoX
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 16 |
+
outputs = model.generate(**inputs)
|
| 17 |
+
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 18 |
+
return corrected_text
|
| 19 |
+
|
| 20 |
+
def process_image(image):
|
| 21 |
+
# Perform OCR on the image
|
| 22 |
+
result = ocr.ocr(image, cls=True)
|
| 23 |
+
|
| 24 |
+
# Extract and join the text from OCR results
|
| 25 |
+
extracted_text = " ".join([line[1][0] for line in result])
|
| 26 |
+
|
| 27 |
+
# Correct the extracted text
|
| 28 |
+
corrected_text = ocr_and_correct(extracted_text)
|
| 29 |
+
|
| 30 |
+
return corrected_text
|
| 31 |
+
|
| 32 |
+
# Gradio interface
|
| 33 |
+
iface = gr.Interface(fn=process_image, inputs="image", outputs="text")
|
| 34 |
+
|
| 35 |
+
iface.launch()
|