jfrancom commited on
Commit
7fd358e
·
1 Parent(s): 988b378

app load whisper

Browse files
Files changed (1) hide show
  1. app.py +93 -14
app.py CHANGED
@@ -1,22 +1,101 @@
1
- # import high-level pipeline
2
- #from transformers import pipeline
3
 
4
- # This line will load your desired model
5
- #pipe = pipeline("automatic-speech-recognition", model="thennal/whisper-medium-ml")
6
-
7
- from transformers import pipeline
8
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
 
10
 
11
- modelo = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-large-xlsr-53-spanish")
12
 
13
- def transcribe(audio):
14
- text = modelo(audio)["text"]
15
- return text
16
 
17
 
18
- gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  fn=transcribe,
20
- inputs=gr.File(files_type=['.mp3'], label="Upload audio file"),
21
- outputs="textbox"
22
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
 
2
 
 
 
 
 
3
  import gradio as gr
4
+ import pytube as pt
5
+ from transformers import pipeline
6
+
7
+
8
+ MODEL_NAME = "whispy/whisper_italian"
9
+
10
+ device = 0 if torch.cuda.is_available() else "cpu"
11
+
12
+ summarizer = pipeline(
13
+ "summarization",
14
+ model="it5/it5-efficient-small-el32-news-summarization",
15
+ )
16
+
17
+ pipe = pipeline(
18
+ task="automatic-speech-recognition",
19
+ model=MODEL_NAME,
20
+ chunk_length_s=30,
21
+ device=device,
22
+ )
23
+
24
+ def transcribe(microphone, file_upload):
25
+ warn_output = ""
26
+ if (microphone is not None) and (file_upload is not None):
27
+ warn_output = (
28
+ "WARNING: You've uploaded an audio file and used the microphone. "
29
+ "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
30
+ )
31
+
32
+ elif (microphone is None) and (file_upload is None):
33
+ return "ERROR: You have to either use the microphone or upload an audio file"
34
 
35
+ file = microphone if microphone is not None else file_upload
36
 
37
+ text = pipe(file)["text"]
38
 
39
+ return warn_output + text
 
 
40
 
41
 
42
+ def _return_yt_html_embed(yt_url):
43
+ video_id = yt_url.split("?v=")[-1]
44
+ HTML_str = (
45
+ f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
46
+ " </center>"
47
+ )
48
+ return HTML_str
49
+
50
+
51
+ def yt_transcribe(yt_url):
52
+ yt = pt.YouTube(yt_url)
53
+ html_embed_str = _return_yt_html_embed(yt_url)
54
+ stream = yt.streams.filter(only_audio=True)[0]
55
+ stream.download(filename="audio.mp3")
56
+
57
+ text = pipe("audio.mp3")["text"]
58
+ summary = summarizer(text)
59
+ summary = summary[0]["summary_text"]
60
+
61
+ return html_embed_str, text, summary
62
+
63
+ demo = gr.Blocks()
64
+
65
+ mf_transcribe = gr.Interface(
66
  fn=transcribe,
67
+ inputs=[
68
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True),
69
+ gr.inputs.Audio(source="upload", type="filepath", optional=True),
70
+ ],
71
+ outputs="text",
72
+ layout="horizontal",
73
+ theme="huggingface",
74
+ title="Whisper Demo: Transcribe Audio",
75
+ description=(
76
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the the fine-tuned"
77
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
78
+ " of arbitrary length."
79
+ ),
80
+ allow_flagging="never",
81
+ )
82
+
83
+ yt_transcribe = gr.Interface(
84
+ fn=yt_transcribe,
85
+ inputs=[gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
86
+ outputs=["html", "text", "text"],
87
+ layout="horizontal",
88
+ theme="huggingface",
89
+ title="Whisper Demo: Transcribe YouTube",
90
+ description=(
91
+ "Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
92
+ f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files of"
93
+ " arbitrary length."
94
+ ),
95
+ allow_flagging="never",
96
+ )
97
+
98
+ with demo:
99
+ gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
100
+
101
+ demo.launch(enable_queue=True)