File size: 9,601 Bytes
b821413
c5ecbf5
7d4f47e
b195470
0a59b92
 
c5ecbf5
b195470
c5ecbf5
 
0a59b92
b821413
26dd92c
dc5e4a5
0a59b92
b821413
b195470
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a59b92
 
c5ecbf5
 
0a59b92
c5ecbf5
0a59b92
 
c5ecbf5
 
 
 
 
 
 
0a59b92
 
 
 
c5ecbf5
075cff2
c5ecbf5
 
0a59b92
c5ecbf5
 
0a59b92
c5ecbf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b195470
 
 
 
 
 
 
 
 
 
 
 
 
c42a43b
 
 
0a59b92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b195470
0a59b92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b195470
0a59b92
 
 
 
 
247daa1
0a59b92
 
 
 
 
 
 
 
 
 
 
b195470
 
 
0a59b92
 
 
 
 
b195470
0a59b92
b195470
 
0a59b92
 
b195470
0a59b92
b195470
 
 
 
 
 
 
 
 
 
 
 
 
 
0a59b92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b195470
0a59b92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b195470
0a59b92
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os
import torch
import spaces
import gradio as gr
from threading import Thread
from collections.abc import Iterator
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer

MAX_MAX_NEW_TOKENS = 4096
MAX_INPUT_TOKEN_LENGTH = 4096
DEFAULT_MAX_NEW_TOKENS = 2048
HF_TOKEN = os.environ['HF_TOKEN']

model_id = "ai4bharat/IndicTrans3-beta"
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto", token=HF_TOKEN)
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-3B-Instruct")

LANGUAGES = {
    "Hindi": "hin_Deva",
    "Bengali": "ben_Beng",
    "Telugu": "tel_Telu",
    "Marathi": "mar_Deva",
    "Tamil": "tam_Taml",
    "Urdu": "urd_Arab",
    "Gujarati": "guj_Gujr",
    "Kannada": "kan_Knda",
    "Odia": "ori_Orya",
    "Malayalam": "mal_Mlym",
    "Punjabi": "pan_Guru",
    "Assamese": "asm_Beng",
    "Maithili": "mai_Mith",
    "Santali": "sat_Olck",
    "Kashmiri": "kas_Arab",
    "Nepali": "nep_Deva",
    "Sindhi": "snd_Arab",
    "Konkani": "kok_Deva",
    "Dogri": "dgo_Deva",
    "Manipuri": "mni_Beng",
    "Bodo": "brx_Deva"
}

def format_message_for_translation(message, target_lang):
    return f"Translate the following text to {target_lang}: {message}"

@spaces.GPU
def translate_message(
    message: str,
    chat_history: list[dict],
    target_language: str = "Hindi",
    max_new_tokens: int = 1024,
    temperature: float = 0.6,
    top_p: float = 0.9,
    top_k: int = 50,
    repetition_penalty: float = 1.2,
) -> Iterator[str]:
    conversation = []
    
    translation_request = format_message_for_translation(message, target_language)
    print(f"Translation request: {translation_request}")
    conversation.append({"role": "user", "content": translation_request})

    input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt", add_generation_prompt=True)
    if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
        input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
        gr.Warning(f"Trimmed input as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
    input_ids = input_ids.to(model.device)

    streamer = TextIteratorStreamer(tokenizer, timeout=240.0, skip_prompt=True, skip_special_tokens=True)
    generate_kwargs = dict(
        {"input_ids": input_ids},
        streamer=streamer,
        max_new_tokens=max_new_tokens,
        do_sample=True,
        top_p=top_p,
        top_k=top_k,
        temperature=temperature,
        num_beams=1,
        repetition_penalty=repetition_penalty,
    )
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()

    outputs = []
    for text in streamer:
        outputs.append(text)
        yield "".join(outputs)

def store_feedback(rating, feedback_text):
    if not rating:
        gr.Warning("Please select a rating before submitting feedback.", duration=5)
        return None
    
    if not feedback_text or feedback_text.strip() == "":
        gr.Warning("Please provide some feedback before submitting.", duration=5)
        return None
    
    gr.Info("Feedback submitted successfully!")
    return "Thank you for your feedback!"

css = """
# body {
#     background-color: #f7f7f7;
# }
.feedback-section {
    margin-top: 30px;
    border-top: 1px solid #ddd;
    padding-top: 20px;
}
.container {
    max-width: 90%;
    margin: 0 auto;
}
.language-selector {
    margin-bottom: 20px;
    padding: 10px;
    background-color: #ffffff;
    border-radius: 8px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.advanced-options {
    margin-top: 20px;
}
"""

DESCRIPTION = """\
IndicTrans3 is the latest state-of-the-art (SOTA) translation model from AI4Bharat, designed to handle translations across <b>22 Indic languages</b> with high accuracy. It supports <b>document-level machine translation (MT)</b> and is built to match the performance of other leading SOTA models. <br>
πŸ“’ <b>Training data will be released soon!</b>  
<h3>πŸ”Ή Features</h3>  
βœ… Supports <b>22 Indic languages</b>  
βœ… Enables <b>document-level translation</b>  
βœ… Achieves <b>SOTA performance</b> in Indic MT  
βœ… Optimized for <b>real-world applications</b>  
<h3>πŸš€ Try It Out!</h3>  
1️⃣ Enter text in any supported language  
2️⃣ Select the target language  
3️⃣ Click <b>Translate</b> and get high-quality results!  
Built for <b>linguistic diversity and accessibility</b>, IndicTrans3 is a major step forward in <b>Indic language AI</b>.  
πŸ’‘ <b>Source:</b> AI4Bharat | Powered by Hugging Face  
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_classes="container"):
        gr.Markdown("# 🌏 IndicTrans3-beta πŸš€: Multilingual Translation for 22 Indic Languages </center>")
        gr.Markdown(DESCRIPTION)
        
        target_language = gr.Dropdown(
            list(LANGUAGES.keys()), 
            value="Hindi",
            label="Which language would you like to translate to?",
            elem_id="language-dropdown"
        )
    
        chatbot = gr.Chatbot(height=400, elem_id="chatbot")
        
        with gr.Row():
            msg = gr.Textbox(
                placeholder="Enter text to translate...",
                show_label=False,
                container=False,
                scale=9
            )
            submit_btn = gr.Button("Translate", scale=1)
        
        gr.Examples(
            examples=[
                "The Taj Mahal stands majestically along the banks of river Yamuna, a timeless symbol of eternal love.",
                "Kumbh Mela is the world's largest gathering of people, where millions of pilgrims bathe in sacred rivers for spiritual purification.",
                "India's classical dance forms like Bharatanatyam, Kathak, and Odissi beautifully blend rhythm, expression, and storytelling.",
                "Ayurveda, the ancient Indian medical system, focuses on holistic wellness through natural herbs and balanced living.",
                "During Diwali, homes across India are decorated with oil lamps, colorful rangoli patterns, and twinkling lights to celebrate the victory of light over darkness."
            ],
            inputs=msg
        )
        
        
        with gr.Accordion("Provide Feedback", open=True):
            gr.Markdown("## Rate Translation & Provide Feedback πŸ“")
            gr.Markdown("Help us improve the translation quality by providing your feedback.")
            with gr.Row():
                rating = gr.Radio(
                    ["1", "2", "3", "4", "5"],
                    label="Translation Rating (1-5)"
                )
            
            feedback_text = gr.Textbox(
                placeholder="Share your feedback about the translation...",
                label="Feedback",
                lines=3
            )
            
            feedback_submit = gr.Button("Submit Feedback")
            feedback_result = gr.Textbox(label="", visible=False)

        with gr.Accordion("Advanced Options", open=False, elem_classes="advanced-options"):
            max_new_tokens = gr.Slider(
                label="Max new tokens",
                minimum=1,
                maximum=MAX_MAX_NEW_TOKENS,
                step=1,
                value=DEFAULT_MAX_NEW_TOKENS,
            )
            temperature = gr.Slider(
                label="Temperature",
                minimum=0.1,
                maximum=1.0,
                step=0.1,
                value=0.1,
            )
            top_p = gr.Slider(
                label="Top-p (nucleus sampling)",
                minimum=0.05,
                maximum=1.0,
                step=0.05,
                value=0.9,
            )
            top_k = gr.Slider(
                label="Top-k",
                minimum=1,
                maximum=100,
                step=1,
                value=50,
            )
            repetition_penalty = gr.Slider(
                label="Repetition penalty",
                minimum=1.0,
                maximum=2.0,
                step=0.05,
                value=1.0,
            )
        
        chat_state = gr.State([])
        
        def user(user_message, history, target_lang):
            return "", history + [[user_message, None]]
        
        def bot(history, target_lang, max_tokens, temp, top_p_val, top_k_val, rep_penalty):
            user_message = history[-1][0]
            history[-1][1] = ""
            
            for chunk in translate_message(
                user_message, 
                history[:-1], 
                target_lang, 
                max_tokens, 
                temp, 
                top_p_val, 
                top_k_val, 
                rep_penalty
            ):
                history[-1][1] = chunk
                yield history
        
        msg.submit(
            user,
            [msg, chatbot, target_language],
            [msg, chatbot],
            queue=False
        ).then(
            bot,
            [chatbot, target_language, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
            chatbot
        )
        
        submit_btn.click(
            user,
            [msg, chatbot, target_language],
            [msg, chatbot],
            queue=False
        ).then(
            bot,
            [chatbot, target_language, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
            chatbot
        )
        
        feedback_submit.click(
            fn=store_feedback, 
            inputs=[rating, feedback_text], 
            outputs=feedback_result
        )

if __name__ == "__main__":
    demo.launch()