Spaces:
Sleeping
Sleeping
File size: 2,358 Bytes
717b6b1 ebe3e23 b17d86f 717b6b1 b17d86f 717b6b1 b17d86f 521a314 b17d86f 521a314 b17d86f 717b6b1 b17d86f 75183d4 b17d86f 717b6b1 b17d86f ebe3e23 b17d86f 75183d4 b17d86f 717b6b1 b17d86f 75183d4 b17d86f 75183d4 b17d86f 75183d4 b17d86f 717b6b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import gradio as gr
import pytesseract
from pdf2image import convert_from_path
from PIL import Image
import os
# Function to perform OCR
def ocr(input_file, lang='fas'): # 'fas': Persian language (Farsi)
extracted_text = ""
# Check if the input file is a PDF or an image
if isinstance(input_file, str) and input_file.endswith('.pdf'): # Check if the file is a PDF
# Convert PDF to images
images = convert_from_path(input_file)
# Loop through each image and perform OCR
for page_number, image in enumerate(images):
text = pytesseract.image_to_string(image, lang=lang)
extracted_text += text
elif isinstance(input_file, Image.Image): # If the input is an image
text = pytesseract.image_to_string(input_file, lang=lang)
extracted_text = text
return extracted_text
def gradio_interface():
# Define Gradio inputs and outputs
input_type = gr.Radio(["PDF", "Image"], label="Choose Input Type", value="PDF")
file_input = gr.File(label="Upload PDF/Image")
language_input = gr.Dropdown(
label="Select OCR Language",
choices=[
("English", "eng"),
("Mandarin Chinese", "chi_sim"),
("Hindi", "hin"),
("Spanish", "spa"),
("French", "fra"),
("Standard Arabic", "ara"),
("Bengali", "ben"),
("Portuguese", "por"),
("Russian", "rus"),
("Urdu", "urd"),
("Persian (Farsi)", "fas")
],
value="fas" # Default to Persian
)
output_text = gr.Textbox(label="Extracted Text", interactive=False)
# Function to process the inputs and return the outputs
def process(input_type, file, lang):
if input_type == "PDF":
extracted_text = ocr(file.name, lang)
else:
image = Image.open(file.name)
extracted_text = ocr(image, lang)
return extracted_text
# Create and launch Gradio interface
gr.Interface(
fn=process,
inputs=[input_type, file_input, language_input],
outputs=[output_text],
title="OCR (PDF/Image)",
description="Upload a PDF or Image, select the OCR language, and extract the text."
).launch()
# Call the function to create the interface
gradio_interface()
|