ocr / app.py
anoopk682's picture
Update app.py
0901e74 verified
import os
# import shutil
# import tempfile
# from typing import List
# from fastapi import FastAPI, File, UploadFile, HTTPException
# from fastapi.responses import JSONResponse
import gradio as gr
from PIL import Image
import pytesseract
class OCR:
def __init__(self, image_path):
self.image = image_path
def extract_text(self):
# image = Image.open(self.image_path)
text = pytesseract.image_to_string(self.image, lang="eng")
os.remove(self.image)
return text
def run_ocr(image_path: str) -> str:
# if not image_path or not os.path.exists(image_path):
# raise FileNotFoundError("Image path is invalid or file does not exist.")
ocr_engine = OCR(image_path=image_path)
return ocr_engine.extract_text()
def run():
demo = gr.Interface(
fn=run_ocr,
inputs=gr.Image(type="filepath"),
outputs=gr.Textbox(label="Extracted Text"),
title="OCR Demo (pytesseract)"
)
demo.launch(
# server_name="0.0.0.0",
# server_port=7860,
share=True # safer during debugging
# show_api=False # <-- avoids the API info route that’s crashing
)
if __name__ == "__main__":
run()