eaysu commited on
Commit
b5d5b5e
1 Parent(s): 9bc03bc

requirements and app updated

Browse files
Files changed (2) hide show
  1. app.py +67 -71
  2. requirements.txt +2 -1
app.py CHANGED
@@ -19,89 +19,85 @@ def load_model(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()
 
19
 
20
  def translate_text(model_name, text):
21
  """
22
+ Translate input text using the specified model.
23
  """
24
+ if not model_name or not text:
25
+ return "Please select a model and provide text for translation."
26
+
27
  try:
28
+ # Load the model and tokenizer
29
  model, tokenizer = load_model(model_name)
30
+
31
+ # Tokenize the 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 translation
37
  translated = model.generate(**tokens)
 
 
38
  return tokenizer.decode(translated[0], skip_special_tokens=True)
39
+
40
  except Exception as e:
41
  return f"Error: {str(e)}"
42
 
43
+ # Model options
44
+ model_options = [
45
+ ("English to Turkish", "Helsinki-NLP/opus-mt-tc-big-en-tr"),
46
+ ("Turkish to English", "Helsinki-NLP/opus-mt-tc-big-tr-en"),
47
+ ("English to French", "Helsinki-NLP/opus-mt-tc-big-en-fr"),
48
+ ("French to English", "Helsinki-NLP/opus-mt-tc-big-fr-en"),
49
+ ("English to German", "Helsinki-NLP/opus-mt-en-de"),
50
+ ("German to English", "Helsinki-NLP/opus-mt-de-en"),
51
+ ("English to Spanish", "Helsinki-NLP/opus-mt-tc-big-en-es"),
52
+ ("Spanish to English", "Helsinki-NLP/opus-mt-es-en"),
53
+ ("English to Arabic", "Helsinki-NLP/opus-mt-tc-big-en-ar"),
54
+ ("Arabic to English", "Helsinki-NLP/opus-mt-tc-big-ar-en"),
55
+ ("English to Urdu", "Helsinki-NLP/opus-mt-en-ur"),
56
+ ("Urdu to English", "Helsinki-NLP/opus-mt-ur-en"),
57
+ ("English to Hindi", "Helsinki-NLP/opus-mt-en-hi"),
58
+ ("Hindi to English", "Helsinki-NLP/opus-mt-hi-en"),
59
+ ("English to Chinese", "Helsinki-NLP/opus-mt-en-zh"),
60
+ ("Chinese to English", "Helsinki-NLP/opus-mt-zh-en"),
61
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ # Create Gradio interface
64
+ with gr.Blocks() as demo:
65
+ gr.Markdown("# 🌍 Real-Time Sentence Translation")
66
+
67
+ with gr.Row():
68
+ model_dropdown = gr.Dropdown(
69
+ label="Select Translation Model",
70
+ choices=[option[1] for option in model_options],
71
+ type="value",
72
+ )
73
+
74
+ with gr.Row():
75
+ input_text = gr.Textbox(
76
+ label="Enter text (complete sentences)",
77
+ lines=5,
78
+ placeholder="Type here...",
79
+ )
80
+
81
+ with gr.Row():
82
+ translate_button = gr.Button("Translate")
83
+ clear_button = gr.Button("Clear")
84
+
85
+ output_text = gr.Textbox(label="Translated Text", interactive=False)
86
+
87
+ def clear_inputs():
88
+ return "", ""
89
+
90
+ translate_button.click(
91
+ fn=translate_text,
92
+ inputs=[model_dropdown, input_text],
93
+ outputs=output_text,
94
  )
95
+
96
+ clear_button.click(
97
+ fn=clear_inputs,
98
+ inputs=[],
99
+ outputs=[input_text, output_text],
100
  )
101
 
102
+ # Run the Gradio app
103
+ demo.launch()
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  transformers
2
  torch
3
  gradio
4
- sentencepiece
 
 
1
  transformers
2
  torch
3
  gradio
4
+ sentencepiece
5
+ sacremoses