Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,30 +2,25 @@ import gradio as gr
|
|
| 2 |
from pdf2image import convert_from_path
|
| 3 |
from PIL import Image
|
| 4 |
import pytesseract
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
# Set Gujarati as OCR language
|
| 8 |
OCR_LANG = "guj"
|
| 9 |
|
| 10 |
def extract_gujarati_text(pdf_file, page_number):
|
| 11 |
-
# Convert selected page to image
|
| 12 |
images = convert_from_path(pdf_file.name, first_page=page_number, last_page=page_number)
|
| 13 |
image = images[0]
|
| 14 |
-
|
| 15 |
-
image
|
| 16 |
-
|
| 17 |
-
# Run OCR with Gujarati
|
| 18 |
-
text = pytesseract.image_to_string(Image.open(image_path), lang=OCR_LANG)
|
| 19 |
-
return text
|
| 20 |
|
| 21 |
with gr.Blocks() as demo:
|
| 22 |
-
gr.Markdown("## π Gujarati OCR from PDF (
|
| 23 |
-
|
| 24 |
pdf = gr.File(label="π€ Upload Gujarati PDF", file_types=[".pdf"])
|
| 25 |
page = gr.Number(label="π Page Number", minimum=1, value=1, step=1)
|
| 26 |
button = gr.Button("π Extract Text")
|
| 27 |
-
output = gr.Textbox(label="π Extracted Gujarati Text", lines=20)
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
demo.launch()
|
|
|
|
| 2 |
from pdf2image import convert_from_path
|
| 3 |
from PIL import Image
|
| 4 |
import pytesseract
|
|
|
|
| 5 |
|
|
|
|
| 6 |
OCR_LANG = "guj"
|
| 7 |
|
| 8 |
def extract_gujarati_text(pdf_file, page_number):
|
|
|
|
| 9 |
images = convert_from_path(pdf_file.name, first_page=page_number, last_page=page_number)
|
| 10 |
image = images[0]
|
| 11 |
+
text = pytesseract.image_to_string(image, lang=OCR_LANG)
|
| 12 |
+
return text, image # Returning both OCR text and snapshot
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
with gr.Blocks() as demo:
|
| 15 |
+
gr.Markdown("## π Gujarati OCR from PDF (with Page Snapshot)")
|
|
|
|
| 16 |
pdf = gr.File(label="π€ Upload Gujarati PDF", file_types=[".pdf"])
|
| 17 |
page = gr.Number(label="π Page Number", minimum=1, value=1, step=1)
|
| 18 |
button = gr.Button("π Extract Text")
|
|
|
|
| 19 |
|
| 20 |
+
with gr.Row():
|
| 21 |
+
image_output = gr.Image(label="πΌοΈ PDF Page Snapshot")
|
| 22 |
+
text_output = gr.Textbox(label="π Extracted Gujarati Text", lines=20)
|
| 23 |
+
|
| 24 |
+
button.click(fn=extract_gujarati_text, inputs=[pdf, page], outputs=[text_output, image_output])
|
| 25 |
|
| 26 |
demo.launch()
|