Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from engine.speech2text import Speech2Text
|
2 |
+
from engine.ner_engine import NERInfer
|
3 |
+
from engine.audio_chunk import AudioChunk
|
4 |
+
import glob
|
5 |
+
import time
|
6 |
+
import gradio as gr
|
7 |
+
import os
|
8 |
+
|
9 |
+
audio_chunk = AudioChunk()
|
10 |
+
speech2text = Speech2Text()
|
11 |
+
ner_tag = NERInfer()
|
12 |
+
|
13 |
+
text = ""
|
14 |
+
def streaming_process(streaming_audio_file) -> str:
|
15 |
+
global text
|
16 |
+
text = speech2text.process_streaming(streaming_audio_file)
|
17 |
+
# return text
|
18 |
+
|
19 |
+
def output_streaming(text_streaming,text01)-> str:
|
20 |
+
text_streaming+=text01
|
21 |
+
return text_streaming
|
22 |
+
|
23 |
+
def recorded_process(recorded_audio_file) -> str:
|
24 |
+
"""
|
25 |
+
to get both input
|
26 |
+
and use speech2text for get text
|
27 |
+
"""
|
28 |
+
text = speech2text.process_microphone(recorded_audio_file)
|
29 |
+
result = ner_tag.infer(text)
|
30 |
+
return {"text": text, "entities": result}
|
31 |
+
|
32 |
+
|
33 |
+
def uploaded_process(uploaded_audio_file) -> str:
|
34 |
+
audio_chunk.chunks_audio(uploaded_audio_file)
|
35 |
+
text = speech2text.process_audio()
|
36 |
+
result = ner_tag.infer(text)
|
37 |
+
return {"text": text, "entities": result}
|
38 |
+
|
39 |
+
|
40 |
+
def clear_inputs_and_outputs() -> list:
|
41 |
+
"""
|
42 |
+
Clears all inputs and outputs when the user clicks "Clear" button
|
43 |
+
"""
|
44 |
+
audio_chunk.remove_chunk()
|
45 |
+
return [None, None, None, None]
|
46 |
+
|
47 |
+
text_streaming = ""
|
48 |
+
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
"""
|
51 |
+
buld gradio app
|
52 |
+
|
53 |
+
"""
|
54 |
+
gr.Markdown(
|
55 |
+
"""
|
56 |
+

|
57 |
+
|
58 |
+
# Automatic Speech Recognition
|
59 |
+
|
60 |
+
##### Experience real-time, accurate, and multilingual speech-to-text conversion with our cutting-edge ASR technology.
|
61 |
+
"""
|
62 |
+
)
|
63 |
+
|
64 |
+
with gr.Tab("Record File"):
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Column():
|
67 |
+
mic_input = gr.Microphone( type="filepath",label="Record voice")
|
68 |
+
with gr.Row():
|
69 |
+
clr_btn = gr.Button(value="Clear", variant="secondary")
|
70 |
+
sub_btn = gr.Button(value="submit")
|
71 |
+
with gr.Column():
|
72 |
+
lbl_output = gr.HighlightedText(label="Result")
|
73 |
+
|
74 |
+
clr_btn.click(
|
75 |
+
fn=clear_inputs_and_outputs,
|
76 |
+
inputs=[],
|
77 |
+
outputs=[mic_input, lbl_output]
|
78 |
+
)
|
79 |
+
|
80 |
+
sub_btn.click(
|
81 |
+
fn=recorded_process,
|
82 |
+
inputs=[mic_input],
|
83 |
+
outputs=[lbl_output]
|
84 |
+
)
|
85 |
+
|
86 |
+
with gr.Tab("streaming"):
|
87 |
+
gr.Interface(
|
88 |
+
fn=streaming_process,
|
89 |
+
inputs=[
|
90 |
+
gr.Microphone(type="filepath", streaming=True)],
|
91 |
+
outputs=[
|
92 |
+
# # gr.HighlightedText(label="Result"),
|
93 |
+
gr.Textbox(type ="text", label="Result",)],
|
94 |
+
live=True,
|
95 |
+
allow_flagging="never"
|
96 |
+
)
|
97 |
+
with gr.Row():
|
98 |
+
with gr.Column():
|
99 |
+
print(text)
|
100 |
+
text_streaming = output_streaming(text_streaming,text)
|
101 |
+
gr.Textbox(value=text, label="Result", autofocus=True)
|
102 |
+
|
103 |
+
with gr.Tab("Upload File"):
|
104 |
+
with gr.Row():
|
105 |
+
with gr.Column():
|
106 |
+
upl_input = gr.Audio( type="filepath", label="Upload a file")
|
107 |
+
with gr.Row():
|
108 |
+
clr_btn = gr.Button(value="Clear", variant="secondary")
|
109 |
+
sub_btn = gr.Button(value="submit")
|
110 |
+
gr.Examples(examples=[
|
111 |
+
os.path.join(os.path.dirname(__file__),"examples/politics.mp3"),
|
112 |
+
os.path.join(os.path.dirname(__file__),"examples/law1.mp3"),
|
113 |
+
os.path.join(os.path.dirname(__file__),"examples/law2.mp3"),
|
114 |
+
os.path.join(os.path.dirname(__file__),"examples/law3.mp3"),
|
115 |
+
os.path.join(os.path.dirname(__file__),"examples/economy.mp3"),
|
116 |
+
os.path.join(os.path.dirname(__file__),"examples/general.mp3")
|
117 |
+
],
|
118 |
+
inputs = upl_input)
|
119 |
+
with gr.Column():
|
120 |
+
lbl_output = gr.HighlightedText(label="Result")
|
121 |
+
|
122 |
+
clr_btn.click(
|
123 |
+
fn=clear_inputs_and_outputs,
|
124 |
+
inputs=[],
|
125 |
+
outputs=[upl_input, lbl_output]
|
126 |
+
)
|
127 |
+
sub_btn.click(
|
128 |
+
fn=uploaded_process,
|
129 |
+
inputs=[upl_input],
|
130 |
+
outputs=[lbl_output]
|
131 |
+
)
|
132 |
+
|
133 |
+
demo.launch(favicon_path = "./image/fe_logo.png", server_name="0.0.0.0", server_port=8085)
|