Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -15,13 +15,13 @@ if not os.path.exists(works_path):
|
|
15 |
os.makedirs(works_path)
|
16 |
print('作品目录:' + works_path)
|
17 |
|
18 |
-
|
19 |
-
model = whisper.load_model(
|
20 |
|
21 |
-
def transcript(audiofile, prompt, output_dir):
|
22 |
-
os.system(f"whisper {audiofile} --model {
|
23 |
|
24 |
-
def audio_recog(audiofile):
|
25 |
utc_dt = datetime.datetime.utcnow()
|
26 |
beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
|
27 |
formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S")
|
@@ -32,20 +32,20 @@ def audio_recog(audiofile):
|
|
32 |
|
33 |
prompt = "以下是普通话的句子"
|
34 |
filename = os.path.splitext(os.path.basename(audiofile))[0]
|
35 |
-
|
36 |
-
|
37 |
|
38 |
output_dir = works_path
|
39 |
-
transcript(audiofile, prompt, output_dir)
|
40 |
-
with open(
|
41 |
text_output = f.read()
|
42 |
print("text:" + text_output)
|
43 |
-
print("text文件:" +
|
44 |
|
45 |
-
with open(
|
46 |
srt_output = f.read()
|
47 |
print("srt:" + srt_output)
|
48 |
-
print("srt文件:" +
|
49 |
|
50 |
utc_dt = datetime.datetime.utcnow()
|
51 |
beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
|
@@ -53,57 +53,31 @@ def audio_recog(audiofile):
|
|
53 |
print(f"结束时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
|
54 |
f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
|
55 |
|
56 |
-
return text_output, srt_output
|
57 |
|
58 |
-
def video_recog(filepath):
|
59 |
-
utc_dt = datetime.datetime.utcnow()
|
60 |
-
beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
|
61 |
-
formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S")
|
62 |
-
print(f"开始时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
|
63 |
-
f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
|
64 |
-
|
65 |
filename = os.path.splitext(os.path.basename(filepath))[0]
|
66 |
worksfile = works_path + '/works_' + filename + '.mp4'
|
67 |
print("视频文件:" + filepath)
|
68 |
|
69 |
-
# 提取音频为
|
70 |
audiofile = works_path + '/' + filename + '.mp3'
|
71 |
os.system(f"ffmpeg -i {filepath} -vn -c:a libmp3lame -q:a 4 {audiofile}")
|
72 |
-
print("音频文件:" + audiofile)
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
output_srt = works_path + '/' + filename + '.srt'
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
text_output = f.read()
|
82 |
-
print("text:" + text_output)
|
83 |
-
print("text文件:" + output_txt)
|
84 |
-
|
85 |
-
with open(output_srt, "r") as f:
|
86 |
-
srt_output = f.read()
|
87 |
-
print("srt:" + srt_output)
|
88 |
-
print("srt文件:" + output_srt)
|
89 |
-
|
90 |
-
# 给视频添加字幕
|
91 |
-
os.system(f"ffmpeg -i {filepath} -i {output_srt} -c:s mov_text -c:v copy -c:a copy {worksfile}")
|
92 |
-
print("作品:" + worksfile)
|
93 |
|
94 |
-
|
95 |
-
beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
|
96 |
-
formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S")
|
97 |
-
print(f"结束时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
|
98 |
-
f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
|
99 |
-
|
100 |
-
return text_output, srt_output, worksfile
|
101 |
|
102 |
css_style = "#fixed_size_img {height: 240px;} " \
|
103 |
"#overview {margin: auto;max-width: 400px; max-height: 400px;}"
|
104 |
|
105 |
title = "音视频转录 by宁侠"
|
106 |
-
description = ""
|
107 |
|
108 |
examples_path = 'examples/'
|
109 |
examples = [[examples_path + 'demo_shejipuhui.mp4']]
|
@@ -128,41 +102,46 @@ with gr.Blocks(title=title, css=css_style) as demo:
|
|
128 |
</h1>
|
129 |
''')
|
130 |
gr.Markdown(description)
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
with gr.Tab("🎥视频转录 Video Transcribe"):
|
134 |
with gr.Row():
|
135 |
with gr.Column():
|
136 |
video_input = gr.Video(label="🎥视频输入 Video Input")
|
137 |
gr.Examples(['examples/demo_shejipuhui.mp4'], [video_input], label='语音识别示例 ASR Demo')
|
|
|
138 |
video_recog_button = gr.Button("👂视频识别 Recognize")
|
139 |
video_output = gr.Video(label="🎥视频 Video", visible=False)
|
140 |
with gr.Column():
|
141 |
-
video_text_output = gr.Textbox(label="✏️识别结果 Recognition Result")
|
142 |
-
|
|
|
|
|
143 |
with gr.Row(visible=False):
|
144 |
font_size = gr.Slider(minimum=10, maximum=100, value=32, step=2, label="🔠字幕字体大小 Subtitle Font Size")
|
145 |
font_color = gr.Radio(["black", "white", "green", "red"], label="🌈字幕颜色 Subtitle Color", value='white')
|
146 |
video_subtitles_button = gr.Button("添加字幕\nGenerate Subtitles", visible=False)
|
147 |
|
148 |
|
149 |
-
video_recog_button.click(video_recog, inputs=[video_input], outputs=[video_text_output, video_srt_output,
|
150 |
# video_subtitles_button.click(video_subtitles, inputs=[video_text_input], outputs=[video_output])
|
151 |
|
152 |
-
with gr.Tab("🔊音频转录 Audio Transcribe"):
|
153 |
-
with gr.Row():
|
154 |
-
with gr.Column():
|
155 |
-
audio_input = gr.Audio(label="🔊音频输入 Audio Input", type="filepath")
|
156 |
-
gr.Examples(['examples/paddlespeech.asr-zh.wav', 'examples/demo_shejipuhui.mp3'], [audio_input])
|
157 |
-
audio_recog_button = gr.Button("👂音频识别 Recognize")
|
158 |
-
with gr.Column():
|
159 |
-
audio_text_output = gr.Textbox(label="✏️识别结果 Recognition Result")
|
160 |
-
audio_srt_output = gr.Textbox(label="📖SRT字幕内容 SRT Subtitles")
|
161 |
-
audio_subtitles_button = gr.Button("添加字幕\nGenerate Subtitles", visible=False)
|
162 |
-
audio_output = gr.Audio(label="🔊音频 Audio", visible=False)
|
163 |
-
|
164 |
-
audio_recog_button.click(audio_recog, inputs=[audio_input], outputs=[audio_text_output, audio_srt_output])
|
165 |
-
# audio_subtitles_button.click(audio_subtitles, inputs=[audio_text_input], outputs=[audio_output])
|
166 |
-
|
167 |
# start gradio service in local
|
168 |
-
demo.queue(api_open=False).launch(debug=True)
|
|
|
15 |
os.makedirs(works_path)
|
16 |
print('作品目录:' + works_path)
|
17 |
|
18 |
+
#model_size = "small"
|
19 |
+
#model = whisper.load_model(model_size) #tiny、base、small、medium(可用)、large
|
20 |
|
21 |
+
def transcript(model_size, audiofile, prompt, output_dir):
|
22 |
+
os.system(f"whisper {audiofile} --model {model_size} --language zh --initial_prompt {prompt} --output_dir {output_dir}")
|
23 |
|
24 |
+
def audio_recog(model_size, audiofile):
|
25 |
utc_dt = datetime.datetime.utcnow()
|
26 |
beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
|
27 |
formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S")
|
|
|
32 |
|
33 |
prompt = "以下是普通话的句子"
|
34 |
filename = os.path.splitext(os.path.basename(audiofile))[0]
|
35 |
+
text_file = works_path + '/' + filename + '.txt'
|
36 |
+
srt_file = works_path + '/' + filename + '.srt'
|
37 |
|
38 |
output_dir = works_path
|
39 |
+
transcript(model_size, audiofile, prompt, output_dir)
|
40 |
+
with open(text_file, "r") as f:
|
41 |
text_output = f.read()
|
42 |
print("text:" + text_output)
|
43 |
+
print("text文件:" + text_file)
|
44 |
|
45 |
+
with open(srt_file, "r") as f:
|
46 |
srt_output = f.read()
|
47 |
print("srt:" + srt_output)
|
48 |
+
print("srt文件:" + srt_file)
|
49 |
|
50 |
utc_dt = datetime.datetime.utcnow()
|
51 |
beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
|
|
|
53 |
print(f"结束时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
|
54 |
f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
|
55 |
|
56 |
+
return text_output, text_file, srt_output, srt_file
|
57 |
|
58 |
+
def video_recog(model_size, filepath):
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
filename = os.path.splitext(os.path.basename(filepath))[0]
|
60 |
worksfile = works_path + '/works_' + filename + '.mp4'
|
61 |
print("视频文件:" + filepath)
|
62 |
|
63 |
+
# 提取音频为mp3
|
64 |
audiofile = works_path + '/' + filename + '.mp3'
|
65 |
os.system(f"ffmpeg -i {filepath} -vn -c:a libmp3lame -q:a 4 {audiofile}")
|
|
|
66 |
|
67 |
+
#识别音频文件
|
68 |
+
text_output, text_file, srt_output, srt_file = audio_recog(model_size, audiofile)
|
|
|
69 |
|
70 |
+
# # 给视频添加字幕
|
71 |
+
# os.system(f"ffmpeg -i {filepath} -i {srt_file} -c:s mov_text -c:v copy -c:a copy {worksfile}")
|
72 |
+
# print("作品:" + worksfile)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
return text_output, text_file, srt_output, srt_file
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
css_style = "#fixed_size_img {height: 240px;} " \
|
77 |
"#overview {margin: auto;max-width: 400px; max-height: 400px;}"
|
78 |
|
79 |
title = "音视频转录 by宁侠"
|
80 |
+
description = "您只需要上传一段音频或视频文件,我们的服务会快速对其进行语音识别,然后生成相应的文字和字幕。这样,您就可以轻松地记录下重要的语音内容,或者为视频添加精准的字幕。现在就���试试我们的音视频转录服务吧,让您的生活和工作更加便捷!"
|
81 |
|
82 |
examples_path = 'examples/'
|
83 |
examples = [[examples_path + 'demo_shejipuhui.mp4']]
|
|
|
102 |
</h1>
|
103 |
''')
|
104 |
gr.Markdown(description)
|
105 |
+
|
106 |
+
with gr.Tab("🔊音频转录 Audio Transcribe"):
|
107 |
+
with gr.Row():
|
108 |
+
with gr.Column():
|
109 |
+
audio_input = gr.Audio(label="🔊音频输入 Audio Input", type="filepath")
|
110 |
+
gr.Examples(['examples/paddlespeech.asr-zh.wav', 'examples/demo_shejipuhui.mp3'], [audio_input])
|
111 |
+
audio_model_size = gr.components.Radio(label="模型尺寸", choices=["tiny", "base", "small", "medium", "large"], value="small")
|
112 |
+
audio_recog_button = gr.Button("👂音频识别 Recognize")
|
113 |
+
with gr.Column():
|
114 |
+
audio_text_output = gr.Textbox(label="✏️识别结果 Recognition Result", max_lines=5)
|
115 |
+
audio_text_file = gr.File(label="✏️识别结果文件 Recognition Result File")
|
116 |
+
audio_srt_output = gr.Textbox(label="📖SRT字幕内容 SRT Subtitles", max_lines=10)
|
117 |
+
audio_srt_file = gr.File(label="📖SRT字幕文件 SRT File")
|
118 |
+
audio_subtitles_button = gr.Button("添加字幕\nGenerate Subtitles", visible=False)
|
119 |
+
audio_output = gr.Audio(label="🔊音频 Audio", visible=False)
|
120 |
+
|
121 |
+
audio_recog_button.click(audio_recog, inputs=[audio_model_size, audio_input], outputs=[audio_text_output, audio_text_file, audio_srt_output, audio_srt_file])
|
122 |
+
# audio_subtitles_button.click(audio_subtitles, inputs=[audio_text_input], outputs=[audio_output])
|
123 |
+
|
124 |
with gr.Tab("🎥视频转录 Video Transcribe"):
|
125 |
with gr.Row():
|
126 |
with gr.Column():
|
127 |
video_input = gr.Video(label="🎥视频输入 Video Input")
|
128 |
gr.Examples(['examples/demo_shejipuhui.mp4'], [video_input], label='语音识别示例 ASR Demo')
|
129 |
+
video_model_size = gr.components.Radio(label="模型尺寸", choices=["tiny", "base", "small", "medium", "large"], value="small")
|
130 |
video_recog_button = gr.Button("👂视频识别 Recognize")
|
131 |
video_output = gr.Video(label="🎥视频 Video", visible=False)
|
132 |
with gr.Column():
|
133 |
+
video_text_output = gr.Textbox(label="✏️识别结果 Recognition Result", max_lines=5)
|
134 |
+
video_text_file = gr.File(label="✏️识别结果文件 Recognition Result File")
|
135 |
+
video_srt_output = gr.Textbox(label="📖SRT字幕内容 SRT Subtitles", max_lines=10)
|
136 |
+
video_srt_file = gr.File(label="📖SRT字幕文件 SRT File")
|
137 |
with gr.Row(visible=False):
|
138 |
font_size = gr.Slider(minimum=10, maximum=100, value=32, step=2, label="🔠字幕字体大小 Subtitle Font Size")
|
139 |
font_color = gr.Radio(["black", "white", "green", "red"], label="🌈字幕颜色 Subtitle Color", value='white')
|
140 |
video_subtitles_button = gr.Button("添加字幕\nGenerate Subtitles", visible=False)
|
141 |
|
142 |
|
143 |
+
video_recog_button.click(video_recog, inputs=[video_model_size, video_input], outputs=[video_text_output, video_text_file, video_srt_output, video_srt_file])
|
144 |
# video_subtitles_button.click(video_subtitles, inputs=[video_text_input], outputs=[video_output])
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
# start gradio service in local
|
147 |
+
demo.queue(api_open=False).launch(debug=True)
|