lodhrangpt commited on
Commit
0fde74b
·
verified ·
1 Parent(s): 21e4d46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -5,7 +5,6 @@ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
5
  model_name = "Helsinki-NLP/opus-mt-en-fr" # Example model for English to French
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
- translator = pipeline("translation", model=model, tokenizer=tokenizer)
9
 
10
  # Define the translation function
11
  def translate_text(text, target_language):
@@ -23,17 +22,22 @@ languages = {
23
  "ru": "Russian"
24
  }
25
 
26
- # Set up the Gradio interface
27
- iface = gr.Interface(
28
- fn=translate_text,
29
- inputs=[
30
- gr.Textbox(label="Enter text to translate"),
31
- gr.Dropdown(list(languages.keys()), label="Target Language", type="value")
32
- ],
33
- outputs=gr.Textbox(label="Translation"),
34
- live=True,
35
- title="Text Translator",
36
- description="Translate text into multiple languages using Hugging Face models."
37
- )
 
 
 
 
 
38
 
39
  iface.launch()
 
5
  model_name = "Helsinki-NLP/opus-mt-en-fr" # Example model for English to French
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
 
8
 
9
  # Define the translation function
10
  def translate_text(text, target_language):
 
22
  "ru": "Russian"
23
  }
24
 
25
+ # Set up the Gradio interface with a submit button
26
+ with gr.Blocks() as iface:
27
+ gr.Markdown("# Text Translator")
28
+ gr.Markdown("Translate text into multiple languages using Hugging Face models.")
29
+
30
+ # Input components
31
+ text_input = gr.Textbox(label="Enter text to translate")
32
+ language_dropdown = gr.Dropdown(list(languages.keys()), label="Target Language", type="value")
33
+
34
+ # Output component
35
+ translation_output = gr.Textbox(label="Translation")
36
+
37
+ # Button to submit
38
+ submit_button = gr.Button("Translate")
39
+
40
+ # When button is clicked, trigger the translation
41
+ submit_button.click(fn=translate_text, inputs=[text_input, language_dropdown], outputs=translation_output)
42
 
43
  iface.launch()