File size: 2,563 Bytes
2720f57
 
 
 
 
 
bb7f34f
2720f57
dd56ef4
2720f57
 
ac45433
2720f57
 
3f76811
 
 
f60f6ff
550a372
3f76811
2720f57
 
 
 
 
 
 
 
 
b46bf98
2720f57
 
b46bf98
2720f57
e08bf83
3a682a4
07e0344
e08bf83
 
 
546b6d1
 
 
 
 
 
 
 
 
 
bb7f34f
 
 
 
 
11fb197
bb7f34f
 
 
 
 
 
 
 
 
 
5ea0c83
bb7f34f
 
 
2720f57
d72ad49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import gradio as gr
from deep_translator import GoogleTranslator
#import deepl
from transformers import pipeline
import os
from gtts import gTTS
from pytube import YouTube

pipe = pipeline(model="freeja/lab2-whisper-sv")

def transcribe_audio(audio,language):
    transcribed = pipe(audio)["text"]
    result = "Transcribed text\n"
    result += transcribed + "\n"
    result += "Translated text\n"
    trans_text = translate_audio(transcribed,language)
    result += trans_text
    #text_to_speech(trans_text,language)
    #result += text_to_speech
    return result


def translate_audio(text,language):
    #translate = deepl.Translator
    language_dict = {"English":"en","Spanish":"es","German":"de","French":"fr","Italian":"it"}
    lang = language_dict[language]
    translated_text = GoogleTranslator(source='sv', target=lang).translate(text)
    return translated_text

def text_to_speech(text,language):
    language_dict = {"English":"en","Spanish":"es","German":"de","French":"fr","Italian":"it"}
    lang = language_dict[language]
    gTTS(text,lang,slow=False)

def transcribe_video(URL):
    video = YouTube(str(URL))
    yt = video.streams.filter(only_audio=True).first().download('youtube_video')
    text = pipe(yt)["text"]
    return text

"""iface = gr.Interface(
    fn=transcribe_audio,
    inputs=[
    gr.Audio(source="microphone", type="filepath", label="Transcribe from Microphone"),
    gr.Dropdown(["English","Spanish","Dutch","French","Italian"], value="English", label="Translate to ")
    ],
    outputs="text",
    title="Whisper Small Swedish",
    description="Realtime demo for Swedish speech recognition with translation using a fine-tuned Whisper small model")"""

video_transcription = gr.Interface(
    fn = transcribe_video, 
    inputs = "text", 
    outputs = "text",
    title = "Whisper Small Swedish",
    description = "Realtime demo for Swedish speech recognition with translation using a fine-tuned Whisper small model",
)

audio_transcription = gr.Interface(
    fn=transcribe_audio, 
    inputs=[
        gr.Audio(source="microphone", type="filepath", label="Transcribe from Microphone"),
        gr.Dropdown(["English","Spanish","Dutch","French","Italian"], value="English", label="Translate to ")
    ], 
    outputs="text",
    title="Whisper Small Swedish",
    description="Realtime demo for Swedish speech recognition with translation using a fine-tuned Whisper small model",
)

iface = gr.TabbedInterface([audio_transcription, video_transcription], ["Transcribe Audio", "Transcribe Video"])

iface.launch()