Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Use a pipeline as a high-level helper
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
# model_path = ("../Models/models--Helsinki-NLP--opus-mt-en-de/snapshots"
|
| 8 |
+
# "/6183067f769a302e3861815543b9f312c71b0ca4")
|
| 9 |
+
|
| 10 |
+
pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-en-de")
|
| 11 |
+
# text_translator = pipeline("translation", model=model_path, tokenizer=model_path)
|
| 12 |
+
|
| 13 |
+
def translate_text(text, destination_language):
|
| 14 |
+
# German uses your preloaded local snapshot
|
| 15 |
+
if destination_language == "German":
|
| 16 |
+
out = text_translator(text)
|
| 17 |
+
return out[0]["translation_text"]
|
| 18 |
+
|
| 19 |
+
# For other targets we load the correct Marian model on demand (EN -> target)
|
| 20 |
+
if destination_language == "French":
|
| 21 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
| 22 |
+
elif destination_language == "Hindi":
|
| 23 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
|
| 24 |
+
elif destination_language == "Romanian":
|
| 25 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ro")
|
| 26 |
+
elif destination_language == "Spanish":
|
| 27 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es")
|
| 28 |
+
elif destination_language == "Italian":
|
| 29 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-it")
|
| 30 |
+
elif destination_language == "Portuguese":
|
| 31 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-pt")
|
| 32 |
+
elif destination_language == "Russian":
|
| 33 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ru")
|
| 34 |
+
elif destination_language == "Japanese":
|
| 35 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ja")
|
| 36 |
+
elif destination_language == "Korean":
|
| 37 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ko")
|
| 38 |
+
elif destination_language == "Chinese":
|
| 39 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-zh")
|
| 40 |
+
elif destination_language == "Arabic":
|
| 41 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar")
|
| 42 |
+
elif destination_language == "Turkish":
|
| 43 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-tr")
|
| 44 |
+
elif destination_language == "Dutch":
|
| 45 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-nl")
|
| 46 |
+
elif destination_language == "Polish":
|
| 47 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-pl")
|
| 48 |
+
elif destination_language == "Ukrainian":
|
| 49 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-uk")
|
| 50 |
+
elif destination_language == "Czech":
|
| 51 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-cs")
|
| 52 |
+
elif destination_language == "Swedish":
|
| 53 |
+
p = pipeline("translation", model="Helsinki-NLP/opus-mt-en-sv")
|
| 54 |
+
else:
|
| 55 |
+
return "Unsupported language. Please choose a listed destination."
|
| 56 |
+
|
| 57 |
+
out = p(text) # Marian pipeline returns a list of dicts
|
| 58 |
+
return out[0]["translation_text"]
|
| 59 |
+
|
| 60 |
+
gr.close_all()
|
| 61 |
+
demo = gr.Interface(
|
| 62 |
+
fn=translate_text,
|
| 63 |
+
inputs=[
|
| 64 |
+
gr.Textbox(label="Input text to translate", lines=6),
|
| 65 |
+
gr.Dropdown(
|
| 66 |
+
[
|
| 67 |
+
"German", "French", "Hindi", "Romanian", "Spanish", "Italian",
|
| 68 |
+
"Portuguese", "Russian", "Japanese", "Korean", "Chinese",
|
| 69 |
+
"Arabic", "Turkish", "Dutch", "Polish", "Ukrainian", "Czech", "Swedish"
|
| 70 |
+
],
|
| 71 |
+
label="Select Destination Language"
|
| 72 |
+
)
|
| 73 |
+
],
|
| 74 |
+
outputs=[gr.Textbox(label="Translated text", lines=4)],
|
| 75 |
+
title="@SahibhimGenAI Project 4: Multi language translator",
|
| 76 |
+
description="Translate English text to your selected language (loads the appropriate MarianMT model per language)."
|
| 77 |
+
)
|
| 78 |
+
demo.launch()
|