Rogerjs commited on
Commit
eeb1fec
·
verified ·
1 Parent(s): c44a2cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -9
app.py CHANGED
@@ -1,26 +1,57 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # Initialize the translation pipelines
5
- translator_en_fi = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fi")
6
- translator_fi_en = pipeline("translation", model="Helsinki-NLP/opus-mt-fi-en")
 
 
 
 
 
 
 
 
 
 
7
 
8
  def translate(text, direction):
 
 
 
 
 
 
 
 
9
  if direction == 'en-fi':
10
  result = translator_en_fi(text)[0]['translation_text']
11
  else:
12
  result = translator_fi_en(text)[0]['translation_text']
13
  return result
14
 
 
 
 
 
 
 
15
  iface = gr.Interface(
16
  fn=translate,
17
  inputs=[
18
- gr.Textbox(lines=2, placeholder="Enter text here..."),
19
- gr.Radio(choices=["en-fi", "fi-en"], label="Translation Direction")
20
  ],
21
- outputs=gr.Textbox(),
22
- title="Translation App",
23
- description="Translate text between English and Finnish using Hugging Face Transformers."
 
 
 
 
 
 
24
  )
25
 
26
- iface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import requests
4
 
5
+ # Load translation pipelines
6
+ translator_en_fi = pipeline(
7
+ "translation_en_to_fi",
8
+ model="Helsinki-NLP/opus-mt-en-fi",
9
+ # Optional: cache_dir="./model_cache",
10
+ # Optional parameters:
11
+ # max_length=512, num_beams=5
12
+ )
13
+ translator_fi_en = pipeline(
14
+ "translation_fi_to_en",
15
+ model="Helsinki-NLP/opus-mt-fi-en",
16
+ # max_length=512, num_beams=5
17
+ )
18
 
19
  def translate(text, direction):
20
+ text = text.strip()
21
+ if not text:
22
+ return "Please enter some text for translation."
23
+
24
+ # Simple input validation
25
+ if len(text) > 2000:
26
+ return "Input text too long. Please shorten it."
27
+
28
  if direction == 'en-fi':
29
  result = translator_en_fi(text)[0]['translation_text']
30
  else:
31
  result = translator_fi_en(text)[0]['translation_text']
32
  return result
33
 
34
+ # Optional: Provide some example texts to guide users.
35
+ examples = [
36
+ ["Hello, how are you?", "en-fi"],
37
+ ["Mitä kuuluu?", "fi-en"]
38
+ ]
39
+
40
  iface = gr.Interface(
41
  fn=translate,
42
  inputs=[
43
+ gr.Textbox(lines=3, placeholder="Enter text here..."),
44
+ gr.Radio(choices=["en-fi", "fi-en"], label="Translation Direction", value="en-fi")
45
  ],
46
+ outputs=gr.Textbox(label="Translated Text"),
47
+ title="English-Finnish Translation App",
48
+ description=(
49
+ "This application uses Helsinki-NLP translation models "
50
+ "to translate text between English and Finnish."
51
+ ),
52
+ examples=examples,
53
+ allow_flagging="never", # Disables any flagging if you don't need it
54
+ enable_queue=True # Allows request queuing if concurrency is needed
55
  )
56
 
57
+ iface.launch()