Spaces:
Running
on
Zero
Running
on
Zero
create app
Browse files
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
model = None
|
4 |
+
tokenizer = None
|
5 |
+
|
6 |
+
# device = 0 if torch.cuda.is_available() else -1
|
7 |
+
|
8 |
+
LANGUAGES = {
|
9 |
+
"Hindi": "hin_Deva",
|
10 |
+
"Bengali": "ben_Beng",
|
11 |
+
"Telugu": "tel_Telu",
|
12 |
+
"Marathi": "mar_Deva",
|
13 |
+
"Tamil": "tam_Taml",
|
14 |
+
"Urdu": "urd_Arab",
|
15 |
+
"Gujarati": "guj_Gujr",
|
16 |
+
"Kannada": "kan_Knda",
|
17 |
+
"Odia": "ori_Orya",
|
18 |
+
"Malayalam": "mal_Mlym",
|
19 |
+
"Punjabi": "pan_Guru",
|
20 |
+
"Assamese": "asm_Beng",
|
21 |
+
"Maithili": "mai_Mith",
|
22 |
+
"Santali": "sat_Olck",
|
23 |
+
"Kashmiri": "kas_Arab",
|
24 |
+
"Nepali": "nep_Deva",
|
25 |
+
"Sindhi": "snd_Arab",
|
26 |
+
"Konkani": "kok_Deva",
|
27 |
+
"Dogri": "dgo_Deva",
|
28 |
+
"Manipuri": "mni_Beng",
|
29 |
+
"Bodo": "brx_Deva"
|
30 |
+
}
|
31 |
+
|
32 |
+
def translate(src_lang, text, tgt_lang):
|
33 |
+
|
34 |
+
return "Translation output will appear here..."
|
35 |
+
|
36 |
+
def store_feedback(rating, feedback_text):
|
37 |
+
if not rating:
|
38 |
+
gr.Warning("Please select a rating before submitting feedback.", duration=5)
|
39 |
+
return None
|
40 |
+
|
41 |
+
if not feedback_text or feedback_text.strip() == "":
|
42 |
+
gr.Warning("Please provide some feedback before submitting.", duration=5)
|
43 |
+
return None
|
44 |
+
|
45 |
+
gr.Info("Feedback submitted successfully!")
|
46 |
+
return "Thank you for your feedback!"
|
47 |
+
|
48 |
+
css = """
|
49 |
+
#col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
|
50 |
+
#header {text-align: center;}
|
51 |
+
.message { font-size: 1.2em; }
|
52 |
+
#feedback-section { margin-top: 30px; border-top: 1px solid #ddd; padding-top: 20px; }
|
53 |
+
"""
|
54 |
+
|
55 |
+
with gr.Blocks(theme=gr.themes.Default(), css=css) as demo:
|
56 |
+
gr.Markdown("# IndicTrans3 Demo π", elem_id="header")
|
57 |
+
gr.Markdown("Translate text between multiple Indic languages using the latest IndicTrans3 model from AI4Bharat. This model is trained on the --- dataset and supports translation to 22 Indic languages. Setting a state-of-the-art benchmark on multiple translation tasks, IndicTrans3 is a powerful model that can handle complex translation tasks with ease.", elem_id="description")
|
58 |
+
|
59 |
+
with gr.Column(elem_id="col-container"):
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column():
|
62 |
+
src_lang = gr.Dropdown(
|
63 |
+
["English"],
|
64 |
+
value="English",
|
65 |
+
label="Translate From",
|
66 |
+
elem_id="translate-from"
|
67 |
+
)
|
68 |
+
|
69 |
+
text_input = gr.Textbox(
|
70 |
+
placeholder="Enter text to translate...",
|
71 |
+
label="",
|
72 |
+
lines=10,
|
73 |
+
max_lines=100,
|
74 |
+
elem_id="input-text"
|
75 |
+
)
|
76 |
+
|
77 |
+
with gr.Column():
|
78 |
+
tgt_lang = gr.Dropdown(
|
79 |
+
list(LANGUAGES.keys()),
|
80 |
+
value="Hindi",
|
81 |
+
label="Translate To",
|
82 |
+
elem_id="translate-to"
|
83 |
+
)
|
84 |
+
|
85 |
+
text_output = gr.Textbox(
|
86 |
+
label="",
|
87 |
+
lines=10,
|
88 |
+
max_lines=100,
|
89 |
+
elem_id="output-text"
|
90 |
+
)
|
91 |
+
|
92 |
+
btn_submit = gr.Button("Translate")
|
93 |
+
btn_submit.click(fn=translate, inputs=[src_lang, text_input, tgt_lang], outputs=text_output)
|
94 |
+
|
95 |
+
gr.Examples(
|
96 |
+
examples=[
|
97 |
+
["English", "Hello, how are you today? I hope you're doing well.", "Telugu"],
|
98 |
+
["English", "Hello, how are you today? I hope you're doing well.", "Punjabi"],
|
99 |
+
["English", "Hello, how are you today? I hope you're doing well.", "Hindi"],
|
100 |
+
["English", "Hello, how are you today? I hope you're doing well.", "Marathi"],
|
101 |
+
["English", "Hello, how are you today? I hope you're doing well.", "Malayalam"]
|
102 |
+
],
|
103 |
+
inputs=[src_lang, text_input, tgt_lang],
|
104 |
+
outputs=text_output,
|
105 |
+
fn=translate,
|
106 |
+
cache_examples=True,
|
107 |
+
examples_per_page=5
|
108 |
+
)
|
109 |
+
|
110 |
+
with gr.Column(elem_id="feedback-section"):
|
111 |
+
gr.Markdown("## Rate Translation & Provide Feedback π")
|
112 |
+
gr.Markdown("Help us improve the translation quality by providing your feedback and rating.")
|
113 |
+
with gr.Row():
|
114 |
+
rating = gr.Radio(
|
115 |
+
["1", "2", "3", "4", "5"],
|
116 |
+
label="Translation Rating (1-5)"
|
117 |
+
)
|
118 |
+
|
119 |
+
feedback_text = gr.Textbox(
|
120 |
+
placeholder="Share your feedback about the translation...",
|
121 |
+
label="Feedback",
|
122 |
+
lines=3
|
123 |
+
)
|
124 |
+
|
125 |
+
feedback_submit = gr.Button("Submit Feedback")
|
126 |
+
feedback_result = gr.Textbox(label="", visible=False)
|
127 |
+
|
128 |
+
feedback_submit.click(
|
129 |
+
fn=store_feedback,
|
130 |
+
inputs=[rating, feedback_text],
|
131 |
+
outputs=feedback_result
|
132 |
+
)
|
133 |
+
|
134 |
+
demo.launch()
|