Upload 3 files
Browse files- .gitattributes +1 -0
- example1.png +0 -0
- example2.png +3 -0
- main.py +71 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
example2.png filter=lfs diff=lfs merge=lfs -text
|
example1.png
ADDED
|
example2.png
ADDED
|
Git LFS Details
|
main.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
OCR & Translate β slicker UI with big title
|
| 3 |
+
> pip install easyocr pillow sacremoses torch torchvision torchaudio gradio
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np
|
| 8 |
+
import easyocr
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
# βββββββ Models βββββββ
|
| 14 |
+
LOCAL_MODEL_PATH = r"C:\Users\96658\Desktop\NLP Project\NLP Project\Models\en-ar-transformer_model"
|
| 15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
reader = easyocr.Reader(['en'], gpu=torch.cuda.is_available())
|
| 17 |
+
tokenizer = MarianTokenizer.from_pretrained(LOCAL_MODEL_PATH)
|
| 18 |
+
translator = MarianMTModel.from_pretrained(LOCAL_MODEL_PATH).to(device)
|
| 19 |
+
|
| 20 |
+
def ocr_and_translate(img: Image.Image):
|
| 21 |
+
lines = reader.readtext(np.array(img), detail=0, paragraph=True)
|
| 22 |
+
text = "\n".join(lines).strip()
|
| 23 |
+
if not text:
|
| 24 |
+
return "", "No text detected."
|
| 25 |
+
toks = tokenizer([text], return_tensors="pt",
|
| 26 |
+
padding=True, truncation=True).to(device)
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
out = translator.generate(**toks, max_length=512)
|
| 29 |
+
arabic = tokenizer.batch_decode(out, skip_special_tokens=True)[0]
|
| 30 |
+
return text, arabic
|
| 31 |
+
|
| 32 |
+
# βββββββ Theme & CSS βββββββ
|
| 33 |
+
nice_theme = gr.themes.Soft(primary_hue="cyan", neutral_hue="stone")
|
| 34 |
+
|
| 35 |
+
custom_css = """
|
| 36 |
+
#container {max-width: 1000px; margin: auto;}
|
| 37 |
+
#main_title {
|
| 38 |
+
font-size: 120px;
|
| 39 |
+
font-weight: 1000;
|
| 40 |
+
text-align: center;
|
| 41 |
+
margin: 1rem 0;
|
| 42 |
+
}
|
| 43 |
+
footer {visibility: hidden;} /* hide βPowered by Gradioβ */
|
| 44 |
+
.gr-box, .gr-button, .gr-image {border-radius: 6px;}
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
# βββββββ UI βββββββ
|
| 48 |
+
with gr.Blocks(theme=nice_theme, css=custom_css) as demo:
|
| 49 |
+
with gr.Column(elem_id="container"):
|
| 50 |
+
gr.Markdown("# π ΨͺΨ±Ψ¬Ω
Ψ§Ω", elem_id="main_title") # BIG title
|
| 51 |
+
with gr.Row():
|
| 52 |
+
img_in = gr.Image(label="Drop or click to upload",
|
| 53 |
+
type="pil",
|
| 54 |
+
height=350)
|
| 55 |
+
with gr.Column():
|
| 56 |
+
txt_out = gr.Textbox(label="Extracted Text (EN)", lines=8)
|
| 57 |
+
trans_out = gr.Textbox(label="Translation (AR)", lines=8)
|
| 58 |
+
btn = gr.Button("π Extract & Translate", variant="primary")
|
| 59 |
+
btn.click(ocr_and_translate, img_in, [txt_out, trans_out])
|
| 60 |
+
|
| 61 |
+
gr.Examples(
|
| 62 |
+
examples=[
|
| 63 |
+
"example1.png",
|
| 64 |
+
"example2.png"
|
| 65 |
+
],
|
| 66 |
+
inputs=img_in,
|
| 67 |
+
label="Try an example"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch(debug=True)
|