oceansweep
commited on
Commit
•
44ae733
1
Parent(s):
a58290d
Delete App_Function_Libraries/Gradio_UI/Live_Recording.py
Browse files
App_Function_Libraries/Gradio_UI/Live_Recording.py
DELETED
@@ -1,123 +0,0 @@
|
|
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.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 |
-
########################################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|