NightPrince commited on
Commit
24114e8
·
verified ·
1 Parent(s): aa7782b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -94
app.py CHANGED
@@ -1,95 +1,97 @@
1
- import torch
2
-
3
- import gradio as gr
4
- import pytube as pt
5
- from transformers import pipeline
6
- from huggingface_hub import model_info
7
-
8
- MODEL_NAME = "tarteel-ai/whisper-base-ar-quran" #this always needs to stay in line 8 :D sorry for the hackiness
9
- lang = "ar"
10
-
11
- device = 0 if torch.cuda.is_available() else "cpu"
12
- pipe = pipeline(
13
- task="automatic-speech-recognition",
14
- model=MODEL_NAME,
15
- chunk_length_s=30,
16
- device=device,
17
- )
18
-
19
- pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang, task="transcribe")
20
-
21
- def transcribe(microphone, file_upload):
22
- warn_output = ""
23
- if (microphone is not None) and (file_upload is not None):
24
- warn_output = (
25
- "WARNING: You've uploaded an audio file and used the microphone. "
26
- "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
27
- )
28
-
29
- elif (microphone is None) and (file_upload is None):
30
- return "ERROR: You have to either use the microphone or upload an audio file"
31
-
32
- file = microphone if microphone is not None else file_upload
33
-
34
- text = pipe(file)["text"]
35
-
36
- return warn_output + text
37
-
38
-
39
- def _return_yt_html_embed(yt_url):
40
- video_id = yt_url.split("?v=")[-1]
41
- HTML_str = (
42
- f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
43
- " </center>"
44
- )
45
- return HTML_str
46
-
47
-
48
- def yt_transcribe(yt_url):
49
- yt = pt.YouTube(yt_url)
50
- html_embed_str = _return_yt_html_embed(yt_url)
51
- stream = yt.streams.filter(only_audio=True)[0]
52
- stream.download(filename="audio.mp3")
53
-
54
- text = pipe("audio.mp3")["text"]
55
-
56
- return html_embed_str, text
57
-
58
-
59
- demo = gr.Blocks()
60
-
61
- mf_transcribe = gr.Interface(
62
- fn=transcribe,
63
- inputs=[
64
- gr.Audio(sources=["microphone"], type="filepath"),
65
- gr.Audio(sources=["upload"], type="filepath"),
66
- ],
67
- outputs="text",
68
- theme="huggingface",
69
- title="Whisper Demo: Transcribe Audio",
70
- description=(
71
- "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the the fine-tuned"
72
- f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
73
- " of arbitrary length."
74
- ),
75
- allow_flagging="never",
76
- )
77
-
78
- yt_transcribe = gr.Interface(
79
- fn=yt_transcribe,
80
- inputs=[gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
81
- outputs=["html", "text"],
82
- theme="huggingface",
83
- title="Whisper Demo: Transcribe YouTube",
84
- description=(
85
- "Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
86
- f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files of"
87
- " arbitrary length."
88
- ),
89
- allow_flagging="never",
90
- )
91
-
92
- with demo:
93
- gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
94
-
 
 
95
  demo.queue().launch(server_port=7860, server_name="0.0.0.0")
 
1
+ import torch
2
+
3
+ import gradio as gr
4
+ import pytube as pt
5
+ from transformers import pipeline
6
+ from huggingface_hub import model_info
7
+ os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
8
+ os.environ["HF_HOME"] = "/app/hf_home"
9
+
10
+ MODEL_NAME = "tarteel-ai/whisper-base-ar-quran" #this always needs to stay in line 8 :D sorry for the hackiness
11
+ lang = "ar"
12
+
13
+ device = 0 if torch.cuda.is_available() else "cpu"
14
+ pipe = pipeline(
15
+ task="automatic-speech-recognition",
16
+ model=MODEL_NAME,
17
+ chunk_length_s=30,
18
+ device=device,
19
+ )
20
+
21
+ pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang, task="transcribe")
22
+
23
+ def transcribe(microphone, file_upload):
24
+ warn_output = ""
25
+ if (microphone is not None) and (file_upload is not None):
26
+ warn_output = (
27
+ "WARNING: You've uploaded an audio file and used the microphone. "
28
+ "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
29
+ )
30
+
31
+ elif (microphone is None) and (file_upload is None):
32
+ return "ERROR: You have to either use the microphone or upload an audio file"
33
+
34
+ file = microphone if microphone is not None else file_upload
35
+
36
+ text = pipe(file)["text"]
37
+
38
+ return warn_output + text
39
+
40
+
41
+ def _return_yt_html_embed(yt_url):
42
+ video_id = yt_url.split("?v=")[-1]
43
+ HTML_str = (
44
+ f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
45
+ " </center>"
46
+ )
47
+ return HTML_str
48
+
49
+
50
+ def yt_transcribe(yt_url):
51
+ yt = pt.YouTube(yt_url)
52
+ html_embed_str = _return_yt_html_embed(yt_url)
53
+ stream = yt.streams.filter(only_audio=True)[0]
54
+ stream.download(filename="audio.mp3")
55
+
56
+ text = pipe("audio.mp3")["text"]
57
+
58
+ return html_embed_str, text
59
+
60
+
61
+ demo = gr.Blocks()
62
+
63
+ mf_transcribe = gr.Interface(
64
+ fn=transcribe,
65
+ inputs=[
66
+ gr.Audio(sources=["microphone"], type="filepath"),
67
+ gr.Audio(sources=["upload"], type="filepath"),
68
+ ],
69
+ outputs="text",
70
+ theme="huggingface",
71
+ title="Whisper Demo: Transcribe Audio",
72
+ description=(
73
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the the fine-tuned"
74
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
75
+ " of arbitrary length."
76
+ ),
77
+ allow_flagging="never",
78
+ )
79
+
80
+ yt_transcribe = gr.Interface(
81
+ fn=yt_transcribe,
82
+ inputs=[gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
83
+ outputs=["html", "text"],
84
+ theme="huggingface",
85
+ title="Whisper Demo: Transcribe YouTube",
86
+ description=(
87
+ "Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
88
+ f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files of"
89
+ " arbitrary length."
90
+ ),
91
+ allow_flagging="never",
92
+ )
93
+
94
+ with demo:
95
+ gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
96
+
97
  demo.queue().launch(server_port=7860, server_name="0.0.0.0")