Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,147 +0,0 @@
|
|
1 |
-
import whisper
|
2 |
-
import gradio as gr
|
3 |
-
import os
|
4 |
-
import datetime
|
5 |
-
|
6 |
-
#获取当前北京时间
|
7 |
-
utc_dt = datetime.datetime.utcnow()
|
8 |
-
beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
|
9 |
-
formatted = beijing_dt.strftime("%Y-%m-%d_%H")
|
10 |
-
print(f"北京时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
|
11 |
-
f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
|
12 |
-
#创建作品存放目录
|
13 |
-
works_path = '../works_audio_video_transcribe/' + formatted
|
14 |
-
if not os.path.exists(works_path):
|
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")
|
28 |
-
print(f"开始时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
|
29 |
-
f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
|
30 |
-
|
31 |
-
print("音频文件:" + audiofile)
|
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)))
|
52 |
-
formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S")
|
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']]
|
84 |
-
|
85 |
-
# gradio interface
|
86 |
-
with gr.Blocks(title=title, css=css_style) as demo:
|
87 |
-
gr.HTML('''
|
88 |
-
<div style="text-align: center; max-width: 720px; margin: 0 auto;">
|
89 |
-
<div
|
90 |
-
style="
|
91 |
-
display: inline-flex;
|
92 |
-
align-items: center;
|
93 |
-
gap: 0.8rem;
|
94 |
-
font-size: 1.75rem;
|
95 |
-
"
|
96 |
-
>
|
97 |
-
<h1 style="font-family: PingFangSC; font-weight: 500; font-size: 36px; margin-bottom: 7px;">
|
98 |
-
音视频转录
|
99 |
-
</h1>
|
100 |
-
<h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 16px; margin-bottom: 7px;">
|
101 |
-
by宁侠
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|