Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import spaces
4
+ import torch
5
+
6
+ model_name = "sarvamai/sarvam-translate"
7
+
8
+ # Load tokenizer and model
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
12
+
13
+ @spaces.GPU
14
+ def generate(tgt_lang, input_txt):
15
+ messages = [
16
+ {"role": "system", "content": f"Translate the following sentence into {tgt_lang}."},
17
+ {"role": "user", "content": input_txt},
18
+ ]
19
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
20
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
21
+ generated_ids = model.generate(
22
+ **model_inputs,
23
+ max_new_tokens=1024,
24
+ do_sample=True,
25
+ temperature=0.01,
26
+ num_return_sequences=1
27
+ )
28
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
29
+ return tokenizer.decode(output_ids, skip_special_tokens=True)
30
+
31
+ demo = gr.Interface(
32
+ fn=generate,
33
+ inputs=[
34
+ gr.Radio(["Hindi", "Bengali", "Marathi", "Telugu", "Tamil", "Gujarati", "Urdu", "Kannada", "Odia", "Malayalam", "Punjabi", "Assamese", "Maithili", "Santali", "Kashmiri", "Nepali", "Sindhi", "Dogri", "Konkani", "Manipuri (Meitei)", "Bodo", "Sanskrit"], label="Target Language", value="Hindi"),
35
+ gr.Textbox(label="Input Text", value="Be the change you wish to see in the world."),
36
+ ],
37
+ outputs=gr.Textbox(label="Translation"),
38
+ title="BhashaBridge"
39
+ )
40
+ demo.launch()