Rogerjs commited on
Commit
785ddc4
1 Parent(s): eeb1fec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -25
app.py CHANGED
@@ -1,27 +1,14 @@
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
 
@@ -31,7 +18,6 @@ def translate(text, direction):
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"]
@@ -45,13 +31,9 @@ iface = gr.Interface(
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()
 
 
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
+ # Initialize the translation pipelines
5
+ translator_en_fi = pipeline("translation_en_to_fi", model="Helsinki-NLP/opus-mt-en-fi")
6
+ translator_fi_en = pipeline("translation_fi_to_en", model="Helsinki-NLP/opus-mt-fi-en")
 
 
 
 
 
 
 
 
 
 
7
 
8
  def translate(text, direction):
9
  text = text.strip()
10
  if not text:
11
  return "Please enter some text for translation."
 
 
12
  if len(text) > 2000:
13
  return "Input text too long. Please shorten it."
14
 
 
18
  result = translator_fi_en(text)[0]['translation_text']
19
  return result
20
 
 
21
  examples = [
22
  ["Hello, how are you?", "en-fi"],
23
  ["Mitä kuuluu?", "fi-en"]
 
31
  ],
32
  outputs=gr.Textbox(label="Translated Text"),
33
  title="English-Finnish Translation App",
34
+ description="This application uses Helsinki-NLP translation models to translate text between English and Finnish.",
35
+ examples=examples
 
 
 
 
 
36
  )
37
 
38
+ if __name__ == "__main__":
39
+ iface.launch()