Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,8 @@ css = "footer {display: none !important;}"
|
|
| 11 |
|
| 12 |
# sample images
|
| 13 |
examples = [
|
| 14 |
-
["",
|
| 15 |
-
["",
|
| 16 |
]
|
| 17 |
|
| 18 |
#you can load any model from huggingface
|
|
@@ -22,16 +22,27 @@ model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-large-handwri
|
|
| 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 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Handle Gradio 4+ sketchpad returning a dictionary
|
| 30 |
if isinstance(imgDraw, dict) and "composite" in imgDraw:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
#predict the image using microsoft/trocr-large-handwritten model loaded earlier
|
| 37 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
|
|
|
| 11 |
|
| 12 |
# sample images
|
| 13 |
examples = [
|
| 14 |
+
["", "images/1.jpg", "images/1.jpg"],
|
| 15 |
+
["", "images/sample-handwritten-2.PNG", "images/sample-handwritten-2.PNG"]
|
| 16 |
]
|
| 17 |
|
| 18 |
#you can load any model from huggingface
|
|
|
|
| 22 |
# prediction function for handwritting
|
| 23 |
def predict(ImageUrl,imgDraw,imgUplod):
|
| 24 |
|
| 25 |
+
image = None
|
| 26 |
#fetch the image from url or handwritten canvas or the uplaoded image
|
| 27 |
+
if ImageUrl:
|
| 28 |
+
try:
|
| 29 |
+
image = Image.open(requests.get(ImageUrl, stream=True).raw).convert("RGB")
|
| 30 |
+
except:
|
| 31 |
+
return "Error: Invalid Image URL"
|
| 32 |
+
# Prioritize uploaded image if it exists
|
| 33 |
+
elif imgUplod is not None:
|
| 34 |
+
image = imgUplod.convert("RGB")
|
| 35 |
+
# Fallback to the drawing canvas
|
| 36 |
+
elif imgDraw is not None:
|
| 37 |
# Handle Gradio 4+ sketchpad returning a dictionary
|
| 38 |
if isinstance(imgDraw, dict) and "composite" in imgDraw:
|
| 39 |
+
if imgDraw["composite"] is not None:
|
| 40 |
+
image = imgDraw["composite"].convert("RGB")
|
| 41 |
+
elif not isinstance(imgDraw, dict):
|
| 42 |
+
image = imgDraw.convert("RGB")
|
| 43 |
+
|
| 44 |
+
if image is None:
|
| 45 |
+
return "Please provide an image via URL, Sketchpad, or Upload."
|
| 46 |
|
| 47 |
#predict the image using microsoft/trocr-large-handwritten model loaded earlier
|
| 48 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|