torileatherman commited on
Commit
c00a822
·
1 Parent(s): a271228

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -32
app.py CHANGED
@@ -1,39 +1,30 @@
1
  from transformers import pipeline, AutoTokenizer, AutoModelWithLMHead, TranslationPipeline
2
  import gradio as gr
3
  import os
 
 
4
 
 
 
 
 
 
5
  pipe = pipeline(model="torileatherman/train_first_try") # change to "your-username/the-name-you-picked"
6
 
7
  def transcribe(audio):
8
- text = pipe(audio)["text"]
9
- return text
10
-
11
- translation_pipeline = TranslationPipeline(model=AutoModelWithLMHead.from_pretrained("SEBIS/legal_t5_small_trans_sv_en"),
12
- tokenizer=AutoTokenizer.from_pretrained(pretrained_model_name_or_path = "SEBIS/legal_t5_small_trans_sv_en",
13
- do_lower_case=False,
14
- skip_special_tokens=True),
15
- device=0)
16
-
17
- def translate(text):
18
- translation = translation_pipeline([text], max_length=512)
19
- return translation
20
-
21
- demo = gr.Blocks()
22
-
23
- with demo:
24
-
25
- title="Whisper Small Swedish",
26
- description="Realtime demo for Swedish speech recognition using a fine-tuned Whisper small model."
27
-
28
- inputs_audio = gr.Audio(source="microphone", type="filepath"),
29
-
30
- text = gr.Textbox()
31
- translation = gr.Label()
32
-
33
- b1 = gr.Button("Record audio")
34
- b2 = gr.Button("Translate text")
35
-
36
- b1.click(transcribe, inputs=inputs_audio, outputs=text)
37
- b2.click(translate, inputs=text, outputs=translation)
38
-
39
- demo.launch()
 
1
  from transformers import pipeline, AutoTokenizer, AutoModelWithLMHead, TranslationPipeline
2
  import gradio as gr
3
  import os
4
+ import deepl
5
+ import openai
6
 
7
+ target_lan = "EN-GB"
8
+ deepl_key = os.environ.get('DEEPL')
9
+ openai.api_key = os.environ.get('OPENAI')
10
+
11
+ translator = deepl.Translator(deepl_key)
12
  pipe = pipeline(model="torileatherman/train_first_try") # change to "your-username/the-name-you-picked"
13
 
14
  def transcribe(audio):
15
+ text_sv = pipe(audio)["text"]
16
+ print(f"Audio transcribed: {text_sv}")
17
+ text_en = translator.translate_text(text_sv, target_lang=TARGET_LANG).text
18
+ print(f"Text translated: {text_en}")
19
+ return text_sv, text_en
20
+
21
+ iface = gr.Interface(
22
+ fn=transcribe,
23
+ inputs=gr.Audio(source="microphone", type="filepath"),
24
+ outputs=[gr.Textbox(label="Transcribed text"),
25
+ gr.Textbox(label="English translation")],
26
+ title="Swedish speech to english text",
27
+ description="Transcribing swedish speech to text and translating to english!",
28
+ )
29
+
30
+ iface.launch()