Spaces:
Sleeping
Sleeping
eaysu
commited on
Commit
·
be003b4
1
Parent(s):
3aeaf4f
initial commit
Browse files- app.py +120 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
3 |
+
import torch
|
4 |
+
import nltk
|
5 |
+
|
6 |
+
# Download punkt for sentence tokenization
|
7 |
+
nltk.download('punkt')
|
8 |
+
nltk.download('punkt_tab')
|
9 |
+
|
10 |
+
from nltk.tokenize import sent_tokenize
|
11 |
+
|
12 |
+
# Cache for storing models and tokenizers
|
13 |
+
models_cache = {}
|
14 |
+
|
15 |
+
def load_model(model_name):
|
16 |
+
"""
|
17 |
+
Load and cache the MarianMT model and tokenizer.
|
18 |
+
"""
|
19 |
+
if model_name not in models_cache:
|
20 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
21 |
+
model = MarianMTModel.from_pretrained(model_name)
|
22 |
+
if torch.cuda.is_available():
|
23 |
+
model = model.to('cuda')
|
24 |
+
models_cache[model_name] = (model, tokenizer)
|
25 |
+
return models_cache[model_name]
|
26 |
+
|
27 |
+
def translate_text(model_name, text):
|
28 |
+
"""
|
29 |
+
Translate input text sentence by sentence using the specified model.
|
30 |
+
"""
|
31 |
+
if not model_name or not text:
|
32 |
+
return "Please select a model and provide text for translation."
|
33 |
+
|
34 |
+
try:
|
35 |
+
# Load the model and tokenizer
|
36 |
+
model, tokenizer = load_model(model_name)
|
37 |
+
|
38 |
+
# Split text into sentences
|
39 |
+
sentences = sent_tokenize(text)
|
40 |
+
translated_sentences = []
|
41 |
+
|
42 |
+
for sentence in sentences:
|
43 |
+
# Tokenize the sentence
|
44 |
+
print(f"Sentence: {sentence}\n")
|
45 |
+
tokens = tokenizer(sentence, return_tensors="pt", padding=True)
|
46 |
+
if torch.cuda.is_available():
|
47 |
+
tokens = {k: v.to('cuda') for k, v in tokens.items()}
|
48 |
+
|
49 |
+
# Generate translation for the sentence
|
50 |
+
translated = model.generate(**tokens)
|
51 |
+
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
52 |
+
translated_sentences.append(translated_text)
|
53 |
+
|
54 |
+
# Join translated sentences back into a single string
|
55 |
+
return " ".join(translated_sentences)
|
56 |
+
|
57 |
+
except Exception as e:
|
58 |
+
return f"Error: {str(e)}"
|
59 |
+
|
60 |
+
# Model options
|
61 |
+
model_options = [
|
62 |
+
("English to Turkish", "Helsinki-NLP/opus-mt-tc-big-en-tr"),
|
63 |
+
("Turkish to English", "Helsinki-NLP/opus-mt-tc-big-tr-en"),
|
64 |
+
("English to French", "Helsinki-NLP/opus-mt-tc-big-en-fr"),
|
65 |
+
("French to English", "Helsinki-NLP/opus-mt-tc-big-fr-en"),
|
66 |
+
("English to German", "Helsinki-NLP/opus-mt-en-de"),
|
67 |
+
("German to English", "Helsinki-NLP/opus-mt-de-en"),
|
68 |
+
("English to Spanish", "Helsinki-NLP/opus-mt-tc-big-en-es"),
|
69 |
+
("Spanish to English", "Helsinki-NLP/opus-mt-es-en"),
|
70 |
+
("English to Arabic", "Helsinki-NLP/opus-mt-tc-big-en-ar"),
|
71 |
+
("Arabic to English", "Helsinki-NLP/opus-mt-tc-big-ar-en"),
|
72 |
+
("English to Urdu", "Helsinki-NLP/opus-mt-en-ur"),
|
73 |
+
("Urdu to English", "Helsinki-NLP/opus-mt-ur-en"),
|
74 |
+
("English to Hindi", "Helsinki-NLP/opus-mt-en-hi"),
|
75 |
+
("Hindi to English", "Helsinki-NLP/opus-mt-hi-en"),
|
76 |
+
("English to Chinese", "Helsinki-NLP/opus-mt-en-zh"),
|
77 |
+
("Chinese to English", "Helsinki-NLP/opus-mt-zh-en")
|
78 |
+
]
|
79 |
+
|
80 |
+
# Create Gradio interface
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
gr.Markdown("# 🌍 Real-Time Sentence Translation")
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
model_dropdown = gr.Dropdown(
|
86 |
+
label="Select Translation Model",
|
87 |
+
choices=[option[1] for option in model_options],
|
88 |
+
type="value",
|
89 |
+
)
|
90 |
+
|
91 |
+
with gr.Row():
|
92 |
+
input_text = gr.Textbox(
|
93 |
+
label="Enter text (complete sentences)",
|
94 |
+
lines=5,
|
95 |
+
placeholder="Type here...",
|
96 |
+
)
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
translate_button = gr.Button("Translate")
|
100 |
+
clear_button = gr.Button("Clear")
|
101 |
+
|
102 |
+
output_text = gr.Textbox(label="Translated Text", interactive=False)
|
103 |
+
|
104 |
+
def clear_inputs():
|
105 |
+
return "", ""
|
106 |
+
|
107 |
+
translate_button.click(
|
108 |
+
fn=translate_text,
|
109 |
+
inputs=[model_dropdown, input_text],
|
110 |
+
outputs=output_text,
|
111 |
+
)
|
112 |
+
|
113 |
+
clear_button.click(
|
114 |
+
fn=clear_inputs,
|
115 |
+
inputs=[],
|
116 |
+
outputs=[input_text, output_text],
|
117 |
+
)
|
118 |
+
|
119 |
+
# Run the Gradio app
|
120 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
nltk
|
4 |
+
gradio
|
5 |
+
sentencepiece
|
6 |
+
sacremoses
|