Spaces:
Runtime error
Runtime error
initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os, shutil
|
3 |
+
import easyocr
|
4 |
+
from PIL import Image
|
5 |
+
import py3langid as langid
|
6 |
+
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
7 |
+
|
8 |
+
languages = ['en', 'fr', 'es']
|
9 |
+
threshold = 0.2
|
10 |
+
langid.set_languages(languages)
|
11 |
+
|
12 |
+
def read_text_from_image(img):
|
13 |
+
reader = easyocr.Reader(languages)
|
14 |
+
result = reader.readtext(img)
|
15 |
+
texts = []
|
16 |
+
|
17 |
+
for (bbox, text, prob) in result:
|
18 |
+
# print(f"Text: {text}, Probability: {prob}")
|
19 |
+
# filter by prob
|
20 |
+
if prob > threshold:
|
21 |
+
texts.append(text)
|
22 |
+
|
23 |
+
if len(texts) == 0:
|
24 |
+
raise ValueError("No text detected")
|
25 |
+
|
26 |
+
concatenated_text = " ".join(texts).lower()
|
27 |
+
return concatenated_text
|
28 |
+
|
29 |
+
def detect_language(text):
|
30 |
+
lang, prob = langid.classify(text)
|
31 |
+
# print(f"The text {text} is classify as {lang} with probability {prob}")
|
32 |
+
return lang
|
33 |
+
|
34 |
+
def translate_to_id(text, lang):
|
35 |
+
query = f"translate from {lang} to ge: {text}"
|
36 |
+
|
37 |
+
tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_1.2B", src_lang=lang)
|
38 |
+
model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_1.2B")
|
39 |
+
|
40 |
+
input_ids = tokenizer(query, return_tensors="pt").input_ids
|
41 |
+
outputs = model.generate(input_ids, forced_bos_token_id=tokenizer.get_lang_id("id"))
|
42 |
+
|
43 |
+
translated = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
44 |
+
return translated
|
45 |
+
|
46 |
+
def predict(img):
|
47 |
+
try:
|
48 |
+
text = read_text_from_image(img)
|
49 |
+
lang = detect_language(text)
|
50 |
+
translated = translate_to_id(text, lang)
|
51 |
+
|
52 |
+
# print(f"Text: {text}. Language: {lang}. Translated: {translated}")
|
53 |
+
return text, lang, translated
|
54 |
+
except ValueError as e:
|
55 |
+
print(e)
|
56 |
+
return e
|
57 |
+
|
58 |
+
app = gr.Interface(
|
59 |
+
fn=predict,
|
60 |
+
inputs=gr.Image(label="Input Image"),
|
61 |
+
outputs='text',
|
62 |
+
)
|
63 |
+
|
64 |
+
app.launch()
|