glt3953 commited on
Commit
bf8c5cb
·
1 Parent(s): 9f7c1fe

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -0
app.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ utc_dt = datetime.datetime.utcnow()
64
+ beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
65
+ formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S.%f")
66
+
67
+ # 提取音频为mp3
68
+ audiofile = works_path + '/' + formatted + '.mp3'
69
+ os.system(f"ffmpeg -i {filepath} -vn -c:a libmp3lame -q:a 4 {audiofile}")
70
+
71
+ #识别音频文件
72
+ text_output, text_file, srt_output, srt_file = audio_recog(model_size, audiofile)
73
+
74
+ # # 给视频添加字幕
75
+ # os.system(f"ffmpeg -i {filepath} -i {srt_file} -c:s mov_text -c:v copy -c:a copy {worksfile}")
76
+ # print("作品:" + worksfile)
77
+
78
+ return text_output, text_file, srt_output, srt_file
79
+
80
+ css_style = "#fixed_size_img {height: 240px;} " \
81
+ "#overview {margin: auto;max-width: 400px; max-height: 400px;}"
82
+
83
+ title = "音视频转录 by宁侠"
84
+ description = "您只需要上传一段音频或视频文件,我们的服务会快速对其进行语音识别,然后生成相应的文字和字幕。这样,您就可以轻松地记录下重要的语音内容,或者为视频添加精准的字幕。现在就来试试我们的音视频转录服务吧,让您的生活和工作更加便捷!"
85
+
86
+ examples_path = 'examples/'
87
+ examples = [[examples_path + 'demo_shejipuhui.mp4']]
88
+
89
+ # gradio interface
90
+ with gr.Blocks(title=title, css=css_style) as demo:
91
+ gr.HTML('''
92
+ <div style="text-align: center; max-width: 720px; margin: 0 auto;">
93
+ <div
94
+ style="
95
+ display: inline-flex;
96
+ align-items: center;
97
+ gap: 0.8rem;
98
+ font-size: 1.75rem;
99
+ "
100
+ >
101
+ <h1 style="font-family: PingFangSC; font-weight: 500; font-size: 36px; margin-bottom: 7px;">
102
+ 音视频转录
103
+ </h1>
104
+ <h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 16px; margin-bottom: 7px;">
105
+ by宁侠
106
+ </h1>
107
+ ''')
108
+ gr.Markdown(description)
109
+
110
+ with gr.Tab("🔊音频转录 Audio Transcribe"):
111
+ with gr.Row():
112
+ with gr.Column():
113
+ audio_input = gr.Audio(label="🔊音频输入 Audio Input", type="filepath")
114
+ gr.Examples(['examples/paddlespeech.asr-zh.wav', 'examples/demo_shejipuhui.mp3'], [audio_input])
115
+ audio_model_size = gr.components.Radio(label="模型尺寸", choices=["tiny", "base", "small", "medium", "large"], value="small")
116
+ audio_recog_button = gr.Button("👂音频识别 Recognize")
117
+ with gr.Column():
118
+ audio_text_output = gr.Textbox(label="✏️识别结果 Recognition Result", max_lines=5)
119
+ audio_text_file = gr.File(label="✏️识别结果文件 Recognition Result File")
120
+ audio_srt_output = gr.Textbox(label="📖SRT字幕内容 SRT Subtitles", max_lines=10)
121
+ audio_srt_file = gr.File(label="📖SRT字幕文件 SRT File")
122
+ audio_subtitles_button = gr.Button("添加字幕\nGenerate Subtitles", visible=False)
123
+ audio_output = gr.Audio(label="🔊音频 Audio", visible=False)
124
+
125
+ 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])
126
+ # audio_subtitles_button.click(audio_subtitles, inputs=[audio_text_input], outputs=[audio_output])
127
+
128
+ with gr.Tab("🎥视频转录 Video Transcribe"):
129
+ with gr.Row():
130
+ with gr.Column():
131
+ video_input = gr.Video(label="🎥视频输入 Video Input")
132
+ gr.Examples(['examples/demo_shejipuhui.mp4'], [video_input], label='语音识别示例 ASR Demo')
133
+ video_model_size = gr.components.Radio(label="模型尺寸", choices=["tiny", "base", "small", "medium", "large"], value="small")
134
+ video_recog_button = gr.Button("👂视频识别 Recognize")
135
+ video_output = gr.Video(label="🎥视频 Video", visible=False)
136
+ with gr.Column():
137
+ video_text_output = gr.Textbox(label="✏️识别结果 Recognition Result", max_lines=5)
138
+ video_text_file = gr.File(label="✏️识别结果文件 Recognition Result File")
139
+ video_srt_output = gr.Textbox(label="📖SRT字幕内容 SRT Subtitles", max_lines=10)
140
+ video_srt_file = gr.File(label="📖SRT字幕文件 SRT File")
141
+ with gr.Row(visible=False):
142
+ font_size = gr.Slider(minimum=10, maximum=100, value=32, step=2, label="🔠字幕字体大小 Subtitle Font Size")
143
+ font_color = gr.Radio(["black", "white", "green", "red"], label="🌈字幕颜色 Subtitle Color", value='white')
144
+ video_subtitles_button = gr.Button("添加字幕\nGenerate Subtitles", visible=False)
145
+
146
+
147
+ 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])
148
+ # video_subtitles_button.click(video_subtitles, inputs=[video_text_input], outputs=[video_output])
149
+
150
+ # start gradio service in local
151
+ demo.queue(api_open=False).launch(debug=True)