raghuprasadks commited on
Commit
6700161
·
verified ·
1 Parent(s): f48a2f0

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +52 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import torch
5
+
6
+ model_name = "sarvamai/sarvam-translate"
7
+
8
+ # Load tokenizer and model
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32).to('cuda:0')
11
+
12
+ @spaces.GPU(duration=120)
13
+ def generate_response(tgt_lang, user_prompt):
14
+ messages = [
15
+ {"role": "system", "content": f"Translate the following sentence into {tgt_lang}."},
16
+ {"role": "user", "content": user_prompt},
17
+ ]
18
+
19
+ # Apply chat template to structure the conversation
20
+ text = tokenizer.apply_chat_template(
21
+ messages,
22
+ tokenize=False,
23
+ )
24
+
25
+ # Tokenize and move input to model device
26
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
27
+
28
+ # Generate the output
29
+ generated_ids = model.generate(
30
+ **model_inputs,
31
+ max_new_tokens=1024,
32
+ do_sample=True,
33
+ temperature=0.01,
34
+ num_return_sequences=1
35
+ )
36
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
37
+ return tokenizer.decode(output_ids, skip_special_tokens=True)
38
+
39
+ # Create Gradio UI
40
+ demo = gr.Interface(
41
+ fn=generate_response,
42
+ inputs=[
43
+ 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"),
44
+ gr.Textbox(label="Input Text", value="Be the change you wish to see in the world."),
45
+ ],
46
+ outputs=gr.Textbox(label="Translation"),
47
+ title="SARVAM - TRANSLATE",
48
+ description="Now supporting 22 Indian languages and structured long-form text"
49
+ )
50
+
51
+ # Launch the app
52
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ transformers