Spaces:
Sleeping
Sleeping
sharifmabdullah commited on
Commit ·
aa33a7f
1
Parent(s): 5b75e48
add: translation codes for hf space
Browse files- .gitignore +0 -0
- app.py +40 -0
- requirements.txt +5 -0
- translator.py +38 -0
.gitignore
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from translator import GlossTranslator
|
| 3 |
+
|
| 4 |
+
# Configuration
|
| 5 |
+
BASE_MODEL_ID: str = "facebook/mbart-large-50"
|
| 6 |
+
PEFT_MODEL_ID: str = "ayhay/BanglaText2Gloss"
|
| 7 |
+
|
| 8 |
+
# Initialize translator instance
|
| 9 |
+
translator_engine: GlossTranslator = GlossTranslator(
|
| 10 |
+
base_model_id=BASE_MODEL_ID, peft_model_id=PEFT_MODEL_ID
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def process_translation(input_text: str) -> str:
|
| 15 |
+
"""Wrapper function to handle UI interactions and catch errors safely."""
|
| 16 |
+
try:
|
| 17 |
+
output: str = translator_engine.translate(input_text)
|
| 18 |
+
return output
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"System Error: {str(e)}"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def create_ui() -> gr.Interface:
|
| 24 |
+
"""Builds and returns the Gradio interface."""
|
| 25 |
+
interface: gr.Interface = gr.Interface(
|
| 26 |
+
fn=process_translation,
|
| 27 |
+
inputs=gr.Textbox(
|
| 28 |
+
lines=3, placeholder="Type Bangla text here...", label="Input: Bangla"
|
| 29 |
+
),
|
| 30 |
+
outputs=gr.Textbox(label="Output: Sign Language Gloss"),
|
| 31 |
+
title="Bangla to Sign Language Gloss",
|
| 32 |
+
description="A specialized translation model based on mBART-large-50.",
|
| 33 |
+
examples=[["আপনি কেমন আছেন?"]],
|
| 34 |
+
)
|
| 35 |
+
return interface
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
app: gr.Interface = create_ui()
|
| 40 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
peft
|
| 3 |
+
torch
|
| 4 |
+
gradio
|
| 5 |
+
sentencepiece
|
translator.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import (
|
| 3 |
+
MBartForConditionalGeneration,
|
| 4 |
+
AutoTokenizer,
|
| 5 |
+
PreTrainedTokenizer,
|
| 6 |
+
PreTrainedTokenizerFast
|
| 7 |
+
)
|
| 8 |
+
from peft import PeftModel
|
| 9 |
+
from typing import Union, Dict, List
|
| 10 |
+
|
| 11 |
+
TokenizerType = Union[PreTrainedTokenizer, PreTrainedTokenizerFast]
|
| 12 |
+
|
| 13 |
+
class GlossTranslator:
|
| 14 |
+
def __init__(self, base_model_id: str, peft_model_id: str) -> None:
|
| 15 |
+
self.base_model_id: str = base_model_id
|
| 16 |
+
self.peft_model_id: str = peft_model_id
|
| 17 |
+
|
| 18 |
+
# Load Tokenizer
|
| 19 |
+
self.tokenizer: TokenizerType = AutoTokenizer.from_pretrained(self.peft_model_id)
|
| 20 |
+
|
| 21 |
+
# Load Base Model and apply PEFT adapters
|
| 22 |
+
base_model: MBartForConditionalGeneration = MBartForConditionalGeneration.from_pretrained(self.base_model_id)
|
| 23 |
+
self.model: PeftModel = PeftModel.from_pretrained(base_model, self.peft_model_id)
|
| 24 |
+
|
| 25 |
+
# Optimize for inference
|
| 26 |
+
self.model.eval()
|
| 27 |
+
|
| 28 |
+
def translate(self, text: str) -> str:
|
| 29 |
+
if not text.strip():
|
| 30 |
+
return ""
|
| 31 |
+
|
| 32 |
+
inputs: Dict[str, torch.Tensor] = self.tokenizer(text, return_tensors="pt")
|
| 33 |
+
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
output_tokens: torch.Tensor = self.model.generate(**inputs, max_new_tokens=50)
|
| 36 |
+
|
| 37 |
+
decoded_glosses: List[str] = self.tokenizer.batch_decode(output_tokens, skip_special_tokens=True)
|
| 38 |
+
return decoded_glosses[0]
|