Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,8 @@ import jaconv
|
|
3 |
import gradio as gr
|
4 |
from transformers import AutoTokenizer, AutoFeatureExtractor, VisionEncoderDecoderModel
|
5 |
from PIL import Image
|
6 |
-
import torch
|
7 |
-
import spaces
|
8 |
|
9 |
tokenizer = AutoTokenizer.from_pretrained("kha-white/manga-ocr-base")
|
10 |
|
@@ -13,8 +13,6 @@ model.to("cuda")
|
|
13 |
|
14 |
feature_extractor = AutoFeatureExtractor.from_pretrained("kha-white/manga-ocr-base")
|
15 |
|
16 |
-
examples = ["00.jpg", "01.jpg", "02.jpg", "03.jpg", "04.jpg", "05.jpg", "06.jpg", "07.jpg", "08.jpg", "09.jpg", "10.jpg", "11.jpg"]
|
17 |
-
|
18 |
def post_process(text):
|
19 |
text = ''.join(text.split())
|
20 |
text = text.replace('…', '...')
|
@@ -22,20 +20,37 @@ def post_process(text):
|
|
22 |
text = jaconv.h2z(text, ascii=True, digit=True)
|
23 |
return text
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
@spaces.GPU
|
26 |
-
def manga_ocr(
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
iface = gr.Interface(
|
37 |
fn=manga_ocr,
|
38 |
-
inputs=gr.
|
39 |
outputs="text",
|
40 |
title="Manga OCR",
|
41 |
description="Extract Manga in lighting speed ⚡",
|
|
|
3 |
import gradio as gr
|
4 |
from transformers import AutoTokenizer, AutoFeatureExtractor, VisionEncoderDecoderModel
|
5 |
from PIL import Image
|
6 |
+
import torch, time
|
7 |
+
import spaces
|
8 |
|
9 |
tokenizer = AutoTokenizer.from_pretrained("kha-white/manga-ocr-base")
|
10 |
|
|
|
13 |
|
14 |
feature_extractor = AutoFeatureExtractor.from_pretrained("kha-white/manga-ocr-base")
|
15 |
|
|
|
|
|
16 |
def post_process(text):
|
17 |
text = ''.join(text.split())
|
18 |
text = text.replace('…', '...')
|
|
|
20 |
text = jaconv.h2z(text, ascii=True, digit=True)
|
21 |
return text
|
22 |
|
23 |
+
# @spaces.GPU
|
24 |
+
# def manga_ocr(img):
|
25 |
+
# img = img.convert('L').convert('RGB')
|
26 |
+
# pixel_values = feature_extractor(img, return_tensors="pt").pixel_values.to("cuda")
|
27 |
+
# start_time = time.time()
|
28 |
+
# output = model.generate(pixel_values)[0]
|
29 |
+
# print("Time taken for OCR:", time.time() - start_time)
|
30 |
+
# text = tokenizer.decode(output, skip_special_tokens=True)
|
31 |
+
# text = post_process(text)
|
32 |
+
# return text
|
33 |
+
|
34 |
+
|
35 |
@spaces.GPU
|
36 |
+
def manga_ocr(imgs):
|
37 |
+
texts = []
|
38 |
+
for img in imgs:
|
39 |
+
img = Image.open(img)
|
40 |
+
img = img.convert('L').convert('RGB')
|
41 |
+
pixel_values = feature_extractor(img, return_tensors="pt").pixel_values.to("cuda")
|
42 |
+
start_time = time.time()
|
43 |
+
output = model.generate(pixel_values)[0]
|
44 |
+
print("Time taken for OCR:", time.time() - start_time)
|
45 |
+
text = tokenizer.decode(output, skip_special_tokens=True)
|
46 |
+
text = post_process(text)
|
47 |
+
texts.append(text)
|
48 |
+
return "|||".join(texts)
|
49 |
+
|
50 |
|
51 |
iface = gr.Interface(
|
52 |
fn=manga_ocr,
|
53 |
+
inputs=gr.File(file_types=["image"], file_count="multiple"),
|
54 |
outputs="text",
|
55 |
title="Manga OCR",
|
56 |
description="Extract Manga in lighting speed ⚡",
|