Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,41 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
sys.path.append(os.path.abspath("libs/IndicTransToolkit"))
|
9 |
-
from IndicTransToolkit.processor import IndicProcessor
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
+
import os
|
5 |
+
import sys
|
6 |
+
|
7 |
+
# Add local IndicTransToolkit path
|
8 |
+
sys.path.append(os.path.abspath("libs/IndicTransToolkit"))
|
9 |
+
from IndicTransToolkit.processor import IndicProcessor
|
10 |
+
|
11 |
+
# Load processor and model
|
12 |
+
st.title("IndicTrans Translator")
|
13 |
+
st.write("Translate English text into Indian languages using IndicTrans2.")
|
14 |
+
|
15 |
+
ip = IndicProcessor(inference=True)
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-en-indic-dist-200M", trust_remote_code=True)
|
17 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/indictrans2-en-indic-dist-200M", trust_remote_code=True)
|
18 |
+
|
19 |
+
text = st.text_area("Enter text in English:", height=150)
|
20 |
+
target_lang = st.selectbox("Select Target Language", [
|
21 |
+
"hin_Deva", "ben_Beng", "pan_Guru", "guj_Gujr", "tam_Taml", "tel_Telu", "mal_Mlym", "mar_Deva", "kan_Knda", "asm_Beng"
|
22 |
+
])
|
23 |
+
|
24 |
+
if st.button("Translate"):
|
25 |
+
if not text.strip():
|
26 |
+
st.warning("Please enter some text.")
|
27 |
+
else:
|
28 |
+
try:
|
29 |
+
batch = ip.preprocess_batch([text], src_lang="eng_Latn", tgt_lang=target_lang)
|
30 |
+
batch = tokenizer(batch, padding="longest", truncation=True, max_length=256, return_tensors="pt")
|
31 |
+
|
32 |
+
with torch.inference_mode():
|
33 |
+
outputs = model.generate(**batch, num_beams=5, max_length=256)
|
34 |
+
|
35 |
+
with tokenizer.as_target_tokenizer():
|
36 |
+
decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
37 |
+
|
38 |
+
translated = ip.postprocess_batch(decoded, lang=target_lang)[0]
|
39 |
+
st.success(f"Translation: {translated}")
|
40 |
+
except Exception as e:
|
41 |
+
st.error(f"Error: {e}")
|