hussain-shk's picture
Duplicate from ai4bharat/IndicTrans-MultilingualTranslation
ef23634
import os
import gradio as gr
from inference.engine import Model
e2i_model_download = "wget --load-cookies /tmp/cookies.txt \"https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1IpcnaQ2ScX_zodt2aLlXa_5Kkntl0nue' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\\n/p')&id=1IpcnaQ2ScX_zodt2aLlXa_5Kkntl0nue\" -O en-indic.zip && rm -rf /tmp/cookies.txt"
os.system(e2i_model_download)
os.system('unzip /home/user/app/en-indic.zip')
i2e_model_download = "wget --load-cookies /tmp/cookies.txt \"https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1-hzy09qi-OEogyge7rQG79K7iV4xsNWa' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\\n/p')&id=1-hzy09qi-OEogyge7rQG79K7iV4xsNWa\" -O indic-en.zip && rm -rf /tmp/cookies.txt"
os.system(i2e_model_download)
os.system('unzip /home/user/app/indic-en.zip')
i2i_model_download = "wget --show-progress -O m2m.tar https://ai4b-my.sharepoint.com/:u:/g/personal/sumanthdoddapaneni_ai4bharat_org/Eajn_jJIp5NEqeyqZ0GW4FgBdiANlZNQiy7dlwkaNr8DHw?download=1"
os.system(i2i_model_download)
os.system("tar -xvf /home/user/app/m2m.tar")
en2indic_model = Model(expdir='/home/user/app/en-indic')
indic2en_model = Model(expdir='/home/user/app/indic-en')
indic2indic_model = Model(expdir='/home/user/app/m2m')
LANGUAGES = {"Assamese": "as", "Bengali": "bn", "Gujarati": "gu", "Hindi": "hi", "Kannada": "kn",
"Malayalam": "ml", "Marathi": "mr", "Odia": "or", "Punjabi": "pa", "Tamil": "ta", "Telugu": "te", "English": "en"}
def translate(text, fromLang, toLang):
if (fromLang != "English" and toLang == "English"):
return indic2en_model.translate_paragraph(text, LANGUAGES[fromLang], LANGUAGES[toLang])
elif (fromLang == "English" and toLang != "English"):
return en2indic_model.translate_paragraph(text, LANGUAGES[fromLang], LANGUAGES[toLang])
elif (fromLang != "English" and toLang != "English"):
return indic2indic_model.translate_paragraph(text, LANGUAGES[fromLang], LANGUAGES[toLang])
else:
return text
languages = list(LANGUAGES.keys())
fromChoice = gr.inputs.Dropdown(
languages, type="value", default="Hindi", label="Select Source Language")
toChoice = gr.inputs.Dropdown(
languages, type="value", default="Tamil", label="Select Target Language")
text_output = gr.outputs.Textbox(
type="auto", label=f"Translation")
text = gr.inputs.Textbox(lines=5, placeholder="Enter Text to translate",
default="", label="Enter Text in Source Language")
supported_lang = ', '.join(languages)
interface_description = f"""
<html>
<body>
<h1>
Usage:
</h1>
<ul>
<li>Choose the Source Language and Target Language for translation.</li>
<li>Enter your text in source language in the textbox.</li>
<li>Click Submit and view your translated output.</li>
</ul>
<br/>
<span>Currently the model supports {supported_lang} </span>
</body>
</html>
"""
interface_article = """
<html>
<body>
<div>
<h1>
About
</h1>
<h4>
Original repository can be found at <a href="https://github.com/AI4Bharat/indicTrans">here</a>.
</h4>
<br/>
<span>
The models used in this interface are multilingual single-script transformer based models for translating between English and Indian languages. The models are trained using the Samanantar corpus and at the time of their release was the state of the art open source model as evaluated on Facebook's FLORES benchmark.
</span>
<br/>
<h4>
These models are currently being used on AI Tools/Platforms such as:
</h4>
<ul>
<li><a href="https://ai4bharat.org/shoonya">Shoonya</a></li>
<li><a href="https://ai4bharat.org/chitralekha">Chitralekha</a> (deployed for NPTEL)</li>
<li><a href="https://ai4bharat.org/anuvaad">Anuvaad</a> (deployed for Supreme Court of India & Bangladesh)</li>
<li>Pratham Books</li>
</ul>
</div>
</body>
</html>
"""
examples = [
["A farmer lives in a village", "English", "Hindi"],
["एक गाव मे एक किसान रहता ता", "Hindi", "English"],
["एक गाव मे एक किसान रहता ता", "Hindi", "Tamil"]
]
iface = gr.Interface(fn=translate, inputs=[text, fromChoice, toChoice], outputs=text_output,
title='IndicTrans - Multilingual Translation', description=interface_description, article=interface_article, examples=examples)
iface.launch(enable_queue=True)