Spaces:
Runtime error
Runtime error
YuhangDeng123
commited on
Commit
•
5819200
1
Parent(s):
f70f966
Upload app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,7 @@ from datasets import Dataset, Audio
|
|
12 |
import os
|
13 |
from moviepy.editor import AudioFileClip
|
14 |
import time
|
|
|
15 |
|
16 |
pipe = pipeline(model="YuhangDeng123/whisper-small-hi") # change to "your-username/the-name-you-picked"
|
17 |
|
@@ -19,6 +20,33 @@ def transcribe(audio):
|
|
19 |
text = pipe(audio)["text"]
|
20 |
return text
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
with gr.Blocks() as demo:
|
23 |
gr.Markdown("Whisper-Small Cantonese Recognition")
|
24 |
with gr.Row():
|
@@ -30,7 +58,14 @@ with gr.Blocks() as demo:
|
|
30 |
with gr.TabItem("Record from Microphone"):
|
31 |
record_file = gr.Audio(source="microphone", type="filepath",label="Record from microphone")
|
32 |
record_button = gr.Button("Submit")
|
33 |
-
record_outputs = [gr.Textbox(label="Recognized result from Microphone"),]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
upload_button.click(
|
35 |
fn=transcribe,
|
36 |
inputs=upload_file,
|
@@ -41,5 +76,10 @@ with gr.Blocks() as demo:
|
|
41 |
fn=transcribe,
|
42 |
inputs=record_file,
|
43 |
outputs=record_outputs,
|
44 |
-
)
|
|
|
|
|
|
|
|
|
|
|
45 |
demo.launch()
|
|
|
12 |
import os
|
13 |
from moviepy.editor import AudioFileClip
|
14 |
import time
|
15 |
+
import requests
|
16 |
|
17 |
pipe = pipeline(model="YuhangDeng123/whisper-small-hi") # change to "your-username/the-name-you-picked"
|
18 |
|
|
|
20 |
text = pipe(audio)["text"]
|
21 |
return text
|
22 |
|
23 |
+
def DownloadFile(mp3_url):
|
24 |
+
file_name = 'test.mp3'
|
25 |
+
res = requests.get(mp3_url)
|
26 |
+
music = res.content
|
27 |
+
# 获取文件地址
|
28 |
+
file_path = os.path.join(os.getcwd(), file_name)
|
29 |
+
with open(file_path, 'ab') as file: #保存到本地的文件名
|
30 |
+
file.write(res.content)
|
31 |
+
file.flush()
|
32 |
+
return file_path
|
33 |
+
|
34 |
+
def convert_to_wav(path):
|
35 |
+
|
36 |
+
audio = AudioFileClip(path)
|
37 |
+
audio_frame = audio.subclip(0, -2)
|
38 |
+
audio_frame.write_audiofile(f"audio.wav")
|
39 |
+
return f"audio.wav"
|
40 |
+
|
41 |
+
def url_transcribe(url):
|
42 |
+
path = DownloadFile(url)
|
43 |
+
path_wav = convert_to_wav(path)
|
44 |
+
audio_dataset = Dataset.from_dict({"audio": [path_wav]}).cast_column("audio", Audio(sampling_rate=16000))
|
45 |
+
text = pipe(audio_dataset["audio"])
|
46 |
+
return text[0]["text"]
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
with gr.Blocks() as demo:
|
51 |
gr.Markdown("Whisper-Small Cantonese Recognition")
|
52 |
with gr.Row():
|
|
|
58 |
with gr.TabItem("Record from Microphone"):
|
59 |
record_file = gr.Audio(source="microphone", type="filepath",label="Record from microphone")
|
60 |
record_button = gr.Button("Submit")
|
61 |
+
record_outputs = [gr.Textbox(label="Recognized result from Microphone"),]
|
62 |
+
with gr.Row():
|
63 |
+
with gr.TabItem("Transcribe from GitHub URL"):
|
64 |
+
url = gr.Text(max_lines=1, label="Transcribe from GitHub URL")
|
65 |
+
Github_button = gr.Button("Submit")
|
66 |
+
Github_outputs = [
|
67 |
+
gr.Textbox(label="Recognized speech from GitHub URL")
|
68 |
+
]
|
69 |
upload_button.click(
|
70 |
fn=transcribe,
|
71 |
inputs=upload_file,
|
|
|
76 |
fn=transcribe,
|
77 |
inputs=record_file,
|
78 |
outputs=record_outputs,
|
79 |
+
)
|
80 |
+
Github_button.click(
|
81 |
+
fn=url_transcribe,
|
82 |
+
inputs=url,
|
83 |
+
outputs=Github_outputs,
|
84 |
+
)
|
85 |
demo.launch()
|