Spaces:
Runtime error
Runtime error
DerrylNessie
commited on
Commit
•
6ad9946
1
Parent(s):
1aa5d0c
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,45 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
iface.launch()
|
|
|
1 |
+
import re
|
2 |
+
import jaconv
|
3 |
import gradio as gr
|
4 |
+
from transformers import AutoTokenizer, AutoFeatureExtractor, VisionEncoderDecoderModel
|
5 |
+
from PIL import Image
|
6 |
+
import torch
|
7 |
|
|
|
|
|
8 |
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("kha-white/manga-ocr-base")
|
10 |
+
|
11 |
+
model = VisionEncoderDecoderModel.from_pretrained("kha-white/manga-ocr-base")
|
12 |
+
|
13 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("kha-white/manga-ocr-base")
|
14 |
+
|
15 |
+
examples = ["japan.jpg"]
|
16 |
+
|
17 |
+
def post_process(text):
|
18 |
+
text = ''.join(text.split())
|
19 |
+
text = text.replace('…', '...')
|
20 |
+
text = re.sub('[・.]{2,}', lambda x: (x.end() - x.start()) * '.', text)
|
21 |
+
text = jaconv.h2z(text, ascii=True, digit=True)
|
22 |
+
return text
|
23 |
+
|
24 |
+
def manga_ocr(img):
|
25 |
+
img = img.convert('L').convert('RGB')
|
26 |
+
pixel_values = feature_extractor(img, return_tensors="pt").pixel_values
|
27 |
+
output = model.generate(pixel_values)[0]
|
28 |
+
text = tokenizer.decode(output, skip_special_tokens=True)
|
29 |
+
text = post_process(text)
|
30 |
+
return text
|
31 |
+
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=manga_ocr,
|
34 |
+
inputs=[gr.inputs.Image(label="Input", type="pil")],
|
35 |
+
outputs="text",
|
36 |
+
layout="horizontal",
|
37 |
+
theme="huggingface",
|
38 |
+
title="Manga OCR",
|
39 |
+
description="Optical Character Recognization for Japanese Texts with focus on Mangas. The model is trained by kha-white with Github link: <a href=\"https://github.com/kha-white/manga-ocr\">manga-ocr</a> while the Space App is made by me.",
|
40 |
+
allow_flagging='never',
|
41 |
+
examples=examples,
|
42 |
+
article = "Author: <a href=\"https://huggingface.co/gryan-galario\">Gryan Galario</a>",
|
43 |
+
)
|
44 |
+
|
45 |
iface.launch()
|