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