eric sali commited on
Commit
c0b088b
·
1 Parent(s): dbd02c3

Add application file

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -1,19 +1,19 @@
1
- import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
- tokenizer = AutoTokenizer.from_pretrained("t5-small")
5
- model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
6
 
7
  def translate_text(text):
8
- inputs = tokenizer.encode("translate English to French: " + text, return_tensors="pt")
9
- outputs = model.generate(inputs, max_length=128, num_beams=4, early_stopping=True)
10
- translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
11
  return translated_text
12
 
13
  output_1 = gr.Textbox(label="Speech to Text")
14
  output_2 = gr.Textbox(label="Speech Translation")
15
 
16
- generator = gr.Interface.load("huggingface/facebook/wav2vec2-base-960h",
17
  inputs="microphone",
18
  outputs=output_1,
19
  title="Speech-to-text",
@@ -26,4 +26,5 @@ translator = gr.Interface(fn=translate_text,
26
  description="Translate English speech to French text using the T5-small model.",
27
  )
28
 
29
- gr.Series(generator, translator).launch()
 
 
1
+ import gradio as gr # Imports the Gradio library, which is used to create user interfaces for machine learning models.
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM # Imports the AutoTokenizer and AutoModelForSeq2SeqLM classes from the Transformers library, which will be used to tokenize and translate text.
3
 
4
+ tokenizer = AutoTokenizer.from_pretrained("t5-small") # Instantiates an AutoTokenizer object using the pre-trained T5-small model. The tokenizer is used to convert input text into a sequence of numerical values that can be used as input to the T5 model.
5
+ model = AutoModelForSeq2SeqLM.from_pretrained("t5-small") # Instantiates an AutoModelForSeq2SeqLM object using the pre-trained T5-small model. This is the model that will be used to generate translations from input text.
6
 
7
  def translate_text(text):
8
+ inputs = tokenizer.encode("translate English to French: " + text, return_tensors="pt") # Uses the tokenizer to encode the input text as a sequence of numerical values that the T5 model can process. The text is prepended with the string "translate English to French: ", which is required by the T5 model to know which language to translate from and to. The return_tensors argument is set to "pt" to return a PyTorch tensor.
9
+ outputs = model.generate(inputs, max_length=128, num_beams=4, early_stopping=True) # Uses the T5 model to generate a translation for the input text. The generate method takes the encoded input text as input and returns a tensor containing the translated text. The max_length argument specifies the maximum length of the generated text, num_beams specifies the number of beams to use during decoding, and early_stopping specifies whether to stop generating output as soon as the model predicts an end-of-sentence token.
10
+ translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) # Uses the tokenizer to convert the tensor of translated text back into a string. The skip_special_tokens argument specifies whether to remove special tokens like padding and end-of-sentence tokens from the decoded text.
11
  return translated_text
12
 
13
  output_1 = gr.Textbox(label="Speech to Text")
14
  output_2 = gr.Textbox(label="Speech Translation")
15
 
16
+ generator = gr.Interface.load("huggingface/facebook/wav2vec2-base-960h", # Creates a Gradio interface that loads the pre-trained Facebook Wav2Vec2 model for speech recognition.
17
  inputs="microphone",
18
  outputs=output_1,
19
  title="Speech-to-text",
 
26
  description="Translate English speech to French text using the T5-small model.",
27
  )
28
 
29
+ gr.Series(generator, translator).launch()
30
+