Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,33 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import VisionEncoderDecoderModel, TrOCRProcessor
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
def recognize_captcha(input, mdl):
|
| 6 |
+
input = transform(input.convert('RGB'))
|
| 7 |
|
| 8 |
+
# Load model and processor
|
| 9 |
+
processor = TrOCRProcessor.from_pretrained(mdl)
|
| 10 |
+
model = VisionEncoderDecoderModel.from_pretrained(mdl)
|
| 11 |
+
|
| 12 |
+
# Prepare image
|
| 13 |
+
pixel_values = processor(input, return_tensors="pt").pixel_values
|
| 14 |
+
|
| 15 |
+
# Generate text
|
| 16 |
+
generated_ids = model.generate(pixel_values)
|
| 17 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 18 |
+
return generated_text
|
| 19 |
+
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
+
recognize_captcha[
|
| 22 |
+
gr.Image,
|
| 23 |
+
gr.Dropdown(
|
| 24 |
+
['anuashok/ocr-captcha-v3','anuashok/ocr-captcha-v2','anuashok/ocr-captcha-v1'], label='Model to use'
|
| 25 |
+
)
|
| 26 |
+
],
|
| 27 |
+
'text',
|
| 28 |
+
title = "character sequence recognition from scene-image (captcha)",
|
| 29 |
+
description = "Using some TrOCR models found on the HF Hub. Will you have to train your own?",
|
| 30 |
+
examples = ['','']
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
iface.launch()
|