Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Initialize the pipeline
|
7 |
+
pipe = pipeline(
|
8 |
+
"text-generation",
|
9 |
+
model="sarvamai/sarvam-translate",
|
10 |
+
torch_dtype=torch.bfloat16,
|
11 |
+
device="cuda",
|
12 |
+
)
|
13 |
+
|
14 |
+
@spaces.GPU
|
15 |
+
def generate_response(tgt_lang, user_prompt):
|
16 |
+
messages = [
|
17 |
+
{"role": "system", "content": f"Translate the following sentence into {tgt_lang}."},
|
18 |
+
{"role": "user", "content": user_prompt},
|
19 |
+
]
|
20 |
+
|
21 |
+
output = pipe(messages, max_new_tokens=2048)
|
22 |
+
return output[0]["generated_text"][-1]["content"]
|
23 |
+
|
24 |
+
# Create Gradio UI
|
25 |
+
demo = gr.Interface(
|
26 |
+
fn=generate_response,
|
27 |
+
inputs=[
|
28 |
+
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"),
|
29 |
+
gr.Textbox(label="Input Text", value="Be the change you wish to see in the world."),
|
30 |
+
],
|
31 |
+
outputs=gr.Textbox(label="Translation"),
|
32 |
+
title="SARVAM - TRANSLATE",
|
33 |
+
description="Now supporting 22 Indian languages and structured long-form text"
|
34 |
+
)
|
35 |
+
|
36 |
+
# Launch the app
|
37 |
+
demo.launch()
|