Spaces:
Running
Running
oceansweep
commited on
Commit
•
659aaa8
1
Parent(s):
2cbb8f4
Update App_Function_Libraries/Gradio_UI/Live_Recording.py
Browse files
App_Function_Libraries/Gradio_UI/Live_Recording.py
CHANGED
@@ -1,123 +1,125 @@
|
|
1 |
-
# Live_Recording.py
|
2 |
-
# Description: Gradio UI for live audio recording and transcription.
|
3 |
-
#
|
4 |
-
# Import necessary modules and functions
|
5 |
-
import logging
|
6 |
-
import os
|
7 |
-
# External Imports
|
8 |
-
import gradio as gr
|
9 |
-
# Local Imports
|
10 |
-
from App_Function_Libraries.Audio_Transcription_Lib import (record_audio, speech_to_text, save_audio_temp,
|
11 |
-
stop_recording)
|
12 |
-
from App_Function_Libraries.DB.DB_Manager import add_media_to_database
|
13 |
-
#
|
14 |
-
#######################################################################################################################
|
15 |
-
#
|
16 |
-
# Functions:
|
17 |
-
|
18 |
-
whisper_models = ["small", "medium", "small.en", "medium.en", "medium", "large", "large-v1", "large-v2", "large-v3",
|
19 |
-
"distil-large-v2", "distil-medium.en", "distil-small.en"]
|
20 |
-
|
21 |
-
def create_live_recording_tab():
|
22 |
-
with gr.Tab("Live Recording and Transcription"):
|
23 |
-
gr.Markdown("# Live Audio Recording and Transcription")
|
24 |
-
with gr.Row():
|
25 |
-
with gr.Column():
|
26 |
-
duration = gr.Slider(minimum=1, maximum=8000, value=15, label="Recording Duration (seconds)")
|
27 |
-
whisper_models_input = gr.Dropdown(choices=whisper_models, value="medium", label="Whisper Model")
|
28 |
-
vad_filter = gr.Checkbox(label="Use VAD Filter")
|
29 |
-
save_recording = gr.Checkbox(label="Save Recording")
|
30 |
-
save_to_db = gr.Checkbox(label="Save Transcription to Database(Must be checked to save - can be checked afer transcription)", value=False)
|
31 |
-
custom_title = gr.Textbox(label="Custom Title (for database)", visible=False)
|
32 |
-
record_button = gr.Button("Start Recording")
|
33 |
-
stop_button = gr.Button("Stop Recording")
|
34 |
-
with gr.Column():
|
35 |
-
output = gr.Textbox(label="Transcription", lines=10)
|
36 |
-
audio_output = gr.Audio(label="Recorded Audio", visible=False)
|
37 |
-
|
38 |
-
recording_state = gr.State(value=None)
|
39 |
-
|
40 |
-
def start_recording(duration):
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
"
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
1 |
+
# Live_Recording.py
|
2 |
+
# Description: Gradio UI for live audio recording and transcription.
|
3 |
+
#
|
4 |
+
# Import necessary modules and functions
|
5 |
+
import logging
|
6 |
+
import os
|
7 |
+
# External Imports
|
8 |
+
import gradio as gr
|
9 |
+
# Local Imports
|
10 |
+
#from App_Function_Libraries.Audio_Transcription_Lib import (record_audio, speech_to_text, save_audio_temp,
|
11 |
+
stop_recording)
|
12 |
+
from App_Function_Libraries.DB.DB_Manager import add_media_to_database
|
13 |
+
#
|
14 |
+
#######################################################################################################################
|
15 |
+
#
|
16 |
+
# Functions:
|
17 |
+
|
18 |
+
whisper_models = ["small", "medium", "small.en", "medium.en", "medium", "large", "large-v1", "large-v2", "large-v3",
|
19 |
+
"distil-large-v2", "distil-medium.en", "distil-small.en"]
|
20 |
+
|
21 |
+
def create_live_recording_tab():
|
22 |
+
with gr.Tab("Live Recording and Transcription"):
|
23 |
+
gr.Markdown("# Live Audio Recording and Transcription")
|
24 |
+
with gr.Row():
|
25 |
+
with gr.Column():
|
26 |
+
duration = gr.Slider(minimum=1, maximum=8000, value=15, label="Recording Duration (seconds)")
|
27 |
+
whisper_models_input = gr.Dropdown(choices=whisper_models, value="medium", label="Whisper Model")
|
28 |
+
vad_filter = gr.Checkbox(label="Use VAD Filter")
|
29 |
+
save_recording = gr.Checkbox(label="Save Recording")
|
30 |
+
save_to_db = gr.Checkbox(label="Save Transcription to Database(Must be checked to save - can be checked afer transcription)", value=False)
|
31 |
+
custom_title = gr.Textbox(label="Custom Title (for database)", visible=False)
|
32 |
+
record_button = gr.Button("Start Recording")
|
33 |
+
stop_button = gr.Button("Stop Recording")
|
34 |
+
with gr.Column():
|
35 |
+
output = gr.Textbox(label="Transcription", lines=10)
|
36 |
+
audio_output = gr.Audio(label="Recorded Audio", visible=False)
|
37 |
+
|
38 |
+
recording_state = gr.State(value=None)
|
39 |
+
|
40 |
+
def start_recording(duration):
|
41 |
+
pass
|
42 |
+
# p, stream, audio_queue, stop_event, audio_thread = record_audio(duration)
|
43 |
+
# return (p, stream, audio_queue, stop_event, audio_thread)
|
44 |
+
|
45 |
+
def end_recording_and_transcribe(recording_state, whisper_model, vad_filter, save_recording, save_to_db, custom_title):
|
46 |
+
pass
|
47 |
+
# if recording_state is None:
|
48 |
+
# return "Recording hasn't started yet.", None
|
49 |
+
|
50 |
+
# p, stream, audio_queue, stop_event, audio_thread = recording_state
|
51 |
+
# audio_data = stop_recording(p, stream, audio_queue, stop_event, audio_thread)
|
52 |
+
|
53 |
+
# temp_file = save_audio_temp(audio_data)
|
54 |
+
# segments = speech_to_text(temp_file, whisper_model=whisper_model, vad_filter=vad_filter)
|
55 |
+
# transcription = "\n".join([segment["Text"] for segment in segments])
|
56 |
+
|
57 |
+
# if save_recording:
|
58 |
+
# return transcription, temp_file
|
59 |
+
# else:
|
60 |
+
# os.remove(temp_file)
|
61 |
+
# return transcription, None
|
62 |
+
|
63 |
+
def save_transcription_to_db(transcription, custom_title):
|
64 |
+
if custom_title.strip() == "":
|
65 |
+
custom_title = "Self-recorded Audio"
|
66 |
+
|
67 |
+
try:
|
68 |
+
url = "self_recorded"
|
69 |
+
info_dict = {
|
70 |
+
"title": custom_title,
|
71 |
+
"uploader": "self-recorded",
|
72 |
+
"webpage_url": url
|
73 |
+
}
|
74 |
+
segments = [{"Text": transcription}]
|
75 |
+
summary = ""
|
76 |
+
keywords = ["self-recorded", "audio"]
|
77 |
+
custom_prompt_input = ""
|
78 |
+
whisper_model = "self-recorded"
|
79 |
+
media_type = "audio"
|
80 |
+
|
81 |
+
result = add_media_to_database(
|
82 |
+
url=url,
|
83 |
+
info_dict=info_dict,
|
84 |
+
segments=segments,
|
85 |
+
summary=summary,
|
86 |
+
keywords=keywords,
|
87 |
+
custom_prompt_input=custom_prompt_input,
|
88 |
+
whisper_model=whisper_model,
|
89 |
+
media_type=media_type
|
90 |
+
)
|
91 |
+
return f"Transcription saved to database successfully. {result}"
|
92 |
+
except Exception as e:
|
93 |
+
logging.error(f"Error saving transcription to database: {str(e)}")
|
94 |
+
return f"Error saving transcription to database: {str(e)}"
|
95 |
+
|
96 |
+
def update_custom_title_visibility(save_to_db):
|
97 |
+
return gr.update(visible=save_to_db)
|
98 |
+
|
99 |
+
record_button.click(
|
100 |
+
fn=start_recording,
|
101 |
+
inputs=[duration],
|
102 |
+
outputs=[recording_state]
|
103 |
+
)
|
104 |
+
|
105 |
+
stop_button.click(
|
106 |
+
fn=end_recording_and_transcribe,
|
107 |
+
inputs=[recording_state, whisper_models_input, vad_filter, save_recording, save_to_db, custom_title],
|
108 |
+
outputs=[output, audio_output]
|
109 |
+
)
|
110 |
+
|
111 |
+
save_to_db.change(
|
112 |
+
fn=update_custom_title_visibility,
|
113 |
+
inputs=[save_to_db],
|
114 |
+
outputs=[custom_title]
|
115 |
+
)
|
116 |
+
|
117 |
+
gr.Button("Save to Database").click(
|
118 |
+
fn=save_transcription_to_db,
|
119 |
+
inputs=[output, custom_title],
|
120 |
+
outputs=gr.Textbox(label="Database Save Status")
|
121 |
+
)
|
122 |
+
|
123 |
+
#
|
124 |
+
# End of Functions
|
125 |
+
########################################################################################################################
|