Ullas26 commited on
Commit
3a52e89
·
verified ·
1 Parent(s): 2e7ba73

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
+ import spaces
4
+ import torch
5
+
6
+ model_name = "ai4bharat/indictrans2-en-indic-1B"
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
9
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name, trust_remote_code=True)
10
+
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ model.to(device)
13
+
14
+ LANG_MAP = {
15
+ "Hindi": "hin_Deva",
16
+ "Bengali": "ben_Beng",
17
+ "Marathi": "mar_Deva",
18
+ "Telugu": "tel_Telu",
19
+ "Tamil": "tam_Taml",
20
+ "Gujarati": "guj_Gujr",
21
+ "Kannada": "kan_Knda",
22
+ "Malayalam": "mal_Mlym",
23
+ "Punjabi": "pan_Guru",
24
+ "Odia": "ory_Orya",
25
+ }
26
+
27
+ @spaces.GPU
28
+ def translate(target_lang, text):
29
+ lang_code = LANG_MAP[target_lang]
30
+
31
+ inputs = tokenizer(
32
+ text,
33
+ return_tensors="pt",
34
+ padding=True,
35
+ truncation=True
36
+ ).to(device)
37
+
38
+ output = model.generate(
39
+ **inputs,
40
+ forced_bos_token_id=tokenizer.convert_tokens_to_ids(lang_code),
41
+ max_length=200
42
+ )
43
+
44
+ return tokenizer.batch_decode(output, skip_special_tokens=True)[0]
45
+
46
+ iface = gr.Interface(
47
+ fn=translate,
48
+ inputs=[
49
+ gr.Dropdown(list(LANG_MAP.keys()), label="Select Target Language", value="Hindi"),
50
+ gr.Textbox(label="Enter Text in English")
51
+ ],
52
+ outputs=gr.Textbox(label="Translated Output"),
53
+ title="BASHA BRIDGE 🌍",
54
+ description="AI Powered Multilingual Translation System"
55
+ )
56
+
57
+ iface.launch()