eengel7 commited on
Commit
2e1f2f2
1 Parent(s): c473c54

Update app.py

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