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
- p, stream, audio_queue, stop_event, audio_thread = record_audio(duration)
42
- return (p, stream, audio_queue, stop_event, audio_thread)
43
-
44
- def end_recording_and_transcribe(recording_state, whisper_model, vad_filter, save_recording, save_to_db, custom_title):
45
- if recording_state is None:
46
- return "Recording hasn't started yet.", None
47
-
48
- p, stream, audio_queue, stop_event, audio_thread = recording_state
49
- audio_data = stop_recording(p, stream, audio_queue, stop_event, audio_thread)
50
-
51
- temp_file = save_audio_temp(audio_data)
52
- segments = speech_to_text(temp_file, whisper_model=whisper_model, vad_filter=vad_filter)
53
- transcription = "\n".join([segment["Text"] for segment in segments])
54
-
55
- if save_recording:
56
- return transcription, temp_file
57
- else:
58
- os.remove(temp_file)
59
- return transcription, None
60
-
61
- def save_transcription_to_db(transcription, custom_title):
62
- if custom_title.strip() == "":
63
- custom_title = "Self-recorded Audio"
64
-
65
- try:
66
- url = "self_recorded"
67
- info_dict = {
68
- "title": custom_title,
69
- "uploader": "self-recorded",
70
- "webpage_url": url
71
- }
72
- segments = [{"Text": transcription}]
73
- summary = ""
74
- keywords = ["self-recorded", "audio"]
75
- custom_prompt_input = ""
76
- whisper_model = "self-recorded"
77
- media_type = "audio"
78
-
79
- result = add_media_to_database(
80
- url=url,
81
- info_dict=info_dict,
82
- segments=segments,
83
- summary=summary,
84
- keywords=keywords,
85
- custom_prompt_input=custom_prompt_input,
86
- whisper_model=whisper_model,
87
- media_type=media_type
88
- )
89
- return f"Transcription saved to database successfully. {result}"
90
- except Exception as e:
91
- logging.error(f"Error saving transcription to database: {str(e)}")
92
- return f"Error saving transcription to database: {str(e)}"
93
-
94
- def update_custom_title_visibility(save_to_db):
95
- return gr.update(visible=save_to_db)
96
-
97
- record_button.click(
98
- fn=start_recording,
99
- inputs=[duration],
100
- outputs=[recording_state]
101
- )
102
-
103
- stop_button.click(
104
- fn=end_recording_and_transcribe,
105
- inputs=[recording_state, whisper_models_input, vad_filter, save_recording, save_to_db, custom_title],
106
- outputs=[output, audio_output]
107
- )
108
-
109
- save_to_db.change(
110
- fn=update_custom_title_visibility,
111
- inputs=[save_to_db],
112
- outputs=[custom_title]
113
- )
114
-
115
- gr.Button("Save to Database").click(
116
- fn=save_transcription_to_db,
117
- inputs=[output, custom_title],
118
- outputs=gr.Textbox(label="Database Save Status")
119
- )
120
-
121
- #
122
- # End of Functions
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
+ ########################################################################################################################