Snowad commited on
Commit
1233b47
1 Parent(s): 7155387

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -15
app.py CHANGED
@@ -1,25 +1,47 @@
1
- from manga_ocr import MangaOcr
2
- from PIL import Image
3
- import time, random, io, base64
4
  import gradio as gr
 
 
 
5
  import spaces
6
 
7
- mocr = MangaOcr()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  @spaces.GPU
10
- def ocr_text(image):
11
- start = time.time()
12
- text = mocr(image)
13
- inference_time = time.time() - start
14
- return text, f"Temps d'inférence : {inference_time:.2f} secondes"
 
 
15
 
16
  iface = gr.Interface(
17
- fn=ocr_text,
18
- inputs=gr.Image(type='pil'),
19
- outputs=["text", "text"],
 
 
20
  title="Manga OCR",
21
- description="Extract Manga in lighting speed ",
 
 
 
22
  )
23
 
24
- if __name__ == '__main__':
25
- 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
  import spaces
8
 
9
+ tokenizer = AutoTokenizer.from_pretrained("kha-white/manga-ocr-base")
10
+
11
+ model = VisionEncoderDecoderModel.from_pretrained("kha-white/manga-ocr-base")
12
+ 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('…', '...')
21
+ text = re.sub('[・.]{2,}', lambda x: (x.end() - x.start()) * '.', text)
22
+ text = jaconv.h2z(text, ascii=True, digit=True)
23
+ return text
24
 
25
  @spaces.GPU
26
+ def manga_ocr(img):
27
+ img = img.convert('L').convert('RGB')
28
+ pixel_values = feature_extractor(img, return_tensors="pt").pixel_values
29
+ output = model.generate(pixel_values)[0]
30
+ text = tokenizer.decode(output, skip_special_tokens=True)
31
+ text = post_process(text)
32
+ return text
33
 
34
  iface = gr.Interface(
35
+ fn=manga_ocr,
36
+ inputs=[gr.inputs.Image(label="Input", type="pil")],
37
+ outputs="text",
38
+ layout="horizontal",
39
+ theme="huggingface",
40
  title="Manga OCR",
41
+ 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.",
42
+ allow_flagging='never',
43
+ examples=examples,
44
+ article = "Author: <a href=\"https://huggingface.co/gryan-galario\">Gryan Galario</a>",
45
  )
46
 
47
+ iface.launch()