eaysu commited on
Commit
6b9283a
1 Parent(s): 5410891

initial commit

Browse files
Files changed (2) hide show
  1. app.py +107 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer
3
+ import torch
4
+
5
+ # Cache for storing models and tokenizers
6
+ models_cache = {}
7
+
8
+ def load_model(model_name):
9
+ """
10
+ Load and cache the MarianMT model and tokenizer.
11
+ """
12
+ if model_name not in models_cache:
13
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
14
+ model = MarianMTModel.from_pretrained(model_name)
15
+ if torch.cuda.is_available():
16
+ model = model.to('cuda')
17
+ models_cache[model_name] = (model, tokenizer)
18
+ return models_cache[model_name]
19
+
20
+ def translate_text(model_name, text):
21
+ """
22
+ Translate text after detecting complete sentences.
23
+ """
24
+ if not model_name or not text.strip():
25
+ return "Select a model and provide text."
26
+
27
+ try:
28
+ # Load the selected model
29
+ model, tokenizer = load_model(model_name)
30
+
31
+ # Tokenize the input text
32
+ tokens = tokenizer(text, return_tensors="pt", padding=True)
33
+ if torch.cuda.is_available():
34
+ tokens = {k: v.to('cuda') for k, v in tokens.items()}
35
+
36
+ # Generate translated tokens
37
+ translated = model.generate(**tokens)
38
+
39
+ # Decode translation
40
+ return tokenizer.decode(translated[0], skip_special_tokens=True)
41
+ except Exception as e:
42
+ return f"Error: {str(e)}"
43
+
44
+ def process_input(model_name, text):
45
+ """
46
+ Process user input to detect completed sentences and translate in real-time.
47
+ """
48
+ sentences = text.strip().split('. ')
49
+ translations = []
50
+ for sentence in sentences:
51
+ if sentence.endswith('.') or sentence.endswith('!') or sentence.endswith('?'):
52
+ translations.append(translate_text(model_name, sentence))
53
+ else:
54
+ # If the sentence is incomplete, skip translation
55
+ translations.append(f"Waiting for completion: {sentence}")
56
+ return '\n'.join(translations)
57
+
58
+ # Define Gradio Interface
59
+ with gr.Blocks() as app:
60
+ gr.Markdown("## 🌍 Real-Time Sentence Translation")
61
+ gr.Markdown("### Enter text in the textbox, and it will be translated after each sentence ends!")
62
+
63
+ model_dropdown = gr.Dropdown(
64
+ label="Select Translation Model",
65
+ choices=[
66
+ "Helsinki-NLP/opus-mt-tc-big-en-tr", # English to Turkish
67
+ "Helsinki-NLP/opus-mt-tc-big-tr-en", # Turkish to English
68
+ "Helsinki-NLP/opus-mt-tc-big-en-fr", # English to French
69
+ "Helsinki-NLP/opus-mt-tc-big-fr-en", # French to English
70
+ "Helsinki-NLP/opus-mt-en-de", # English to German
71
+ "Helsinki-NLP/opus-mt-de-en", # German to English
72
+ "Helsinki-NLP/opus-mt-tc-big-en-es", # English to Spanish
73
+ "Helsinki-NLP/opus-mt-es-en", # Spanish to English
74
+ "Helsinki-NLP/opus-mt-tc-big-en-ar", # English to Arabic
75
+ "Helsinki-NLP/opus-mt-tc-big-ar-en", # Arabic to English
76
+ "Helsinki-NLP/opus-mt-en-ur", # English to Urdu
77
+ "Helsinki-NLP/opus-mt-ur-en", # Urdu to English
78
+ "Helsinki-NLP/opus-mt-en-hi", # English to Hindi
79
+ "Helsinki-NLP/opus-mt-hi-en", # Hindi to English
80
+ "Helsinki-NLP/opus-mt-en-zh", # English to Chinese
81
+ "Helsinki-NLP/opus-mt-zh-en", # Chinese to English",
82
+ ],
83
+ value="Helsinki-NLP/opus-mt-tc-big-en-tr",
84
+ interactive=True
85
+ )
86
+
87
+ input_textbox = gr.Textbox(
88
+ label="Enter Text:",
89
+ placeholder="Type here...",
90
+ lines=5,
91
+ interactive=True
92
+ )
93
+
94
+ output_textbox = gr.Textbox(
95
+ label="Translated Text:",
96
+ lines=5,
97
+ interactive=False
98
+ )
99
+
100
+ input_textbox.change(
101
+ fn=process_input,
102
+ inputs=[model_dropdown, input_textbox],
103
+ outputs=[output_textbox],
104
+ )
105
+
106
+ # Launch Gradio App
107
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch