Yasaman commited on
Commit
5deae18
·
1 Parent(s): e2360a2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pytube import YouTube
2
+ from transformers import pipeline
3
+ import gradio as gr
4
+ import os
5
+
6
+ pipe = pipeline(model="Yasaman/whisper_fa") # change to "your-username/the-name-you-picked"
7
+
8
+ def get_audio(url):
9
+ yt = YouTube(url)
10
+ stream = yt.streams.filter(only_audio=True).first()
11
+ out_file=stream.download(output_path=".")
12
+ base, ext = os.path.splitext(out_file)
13
+ new_file = base+'.mp3'
14
+ os.rename(out_file, new_file)
15
+ audio = new_file
16
+ return audio
17
+
18
+
19
+ def transcribe(audio=None, file=None, youtube=None):
20
+ if (audio is None) and (file is None) and (youtube is None):
21
+ return "No audio provided!"
22
+ elif audio is not None:
23
+ input=audio
24
+ elif file is not None:
25
+ input=file
26
+ elif youtube is not None:
27
+ input=get_audio(youtube)
28
+ text = pipe(input)["text"]
29
+ return text
30
+
31
+ iface = gr.Interface(
32
+ fn=transcribe,
33
+ inputs=[
34
+ gr.Audio(source="microphone", type="filepath", interactive=True),
35
+ gr.Audio(source="upload", type="filepath", interactive=True),
36
+ gr.Text(label="URL (YouTube, etc.)")],
37
+ outputs="text",
38
+ title="Whisper Small Swedish",
39
+ description="Realtime demo for Swedish speech recognition using a fine-tuned Whisper small model.",
40
+ )
41
+
42
+ iface.launch()