Spaces:
Running
Running
Upload 4 files
Browse files- app.py +51 -0
- images/1.jpg +0 -0
- images/sample-handwritten-2.PNG +0 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# app title
|
| 7 |
+
title = "ScriptSense"
|
| 8 |
+
description = "<p style='text-align: center; font-size: 22px; font-weight: bold;'>design and crafted by aryan verma</p>"
|
| 9 |
+
article = "<p style='text-align: center; font-size: 14px;'>aryan verma | 241306064</p>"
|
| 10 |
+
css = "footer {display: none !important;}"
|
| 11 |
+
|
| 12 |
+
# sample images
|
| 13 |
+
examples = [
|
| 14 |
+
["", None, "images/1.jpg"],
|
| 15 |
+
["", None, "images/sample-handwritten-2.PNG"]
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
#you can load any model from huggingface
|
| 19 |
+
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-large-handwritten')
|
| 20 |
+
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-large-handwritten')
|
| 21 |
+
|
| 22 |
+
# prediction function for handwritting
|
| 23 |
+
def predict(ImageUrl,imgDraw,imgUplod):
|
| 24 |
+
|
| 25 |
+
#fetch the image from url or handwritten canvas or the uplaoded image
|
| 26 |
+
if ImageUrl :
|
| 27 |
+
image = Image.open(requests.get(ImageUrl, stream=True).raw).convert("RGB")
|
| 28 |
+
elif imgDraw :
|
| 29 |
+
image = imgDraw.convert("RGB")
|
| 30 |
+
else :
|
| 31 |
+
image = imgUplod.convert("RGB")
|
| 32 |
+
|
| 33 |
+
#predict the image using microsoft/trocr-large-handwritten model loaded earlier
|
| 34 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 35 |
+
generated_ids = model.generate(pixel_values)
|
| 36 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 37 |
+
return generated_text
|
| 38 |
+
|
| 39 |
+
#gradio interface
|
| 40 |
+
interface = gr.Interface(
|
| 41 |
+
fn=predict,
|
| 42 |
+
inputs=["text",gr.Sketchpad(type="pil",shape=(500, 500)),gr.Image(type="pil")],
|
| 43 |
+
outputs="text",
|
| 44 |
+
title=title,
|
| 45 |
+
description=description,
|
| 46 |
+
article=article,
|
| 47 |
+
examples=examples,
|
| 48 |
+
css=css
|
| 49 |
+
)
|
| 50 |
+
interface.launch()
|
| 51 |
+
|
images/1.jpg
ADDED
|
images/sample-handwritten-2.PNG
ADDED
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio==3.50.2
|
| 4 |
+
pillow
|
| 5 |
+
requests
|
| 6 |
+
fastapi==0.104.1
|
| 7 |
+
starlette==0.27.0
|
| 8 |
+
pydantic==1.10.13
|
| 9 |
+
jinja2<3.1.4
|