Spaces:
Runtime error
Runtime error
Update App_Function_Libraries/Gradio_UI/Live_Recording.py
Browse files
App_Function_Libraries/Gradio_UI/Live_Recording.py
CHANGED
|
@@ -1,123 +1,43 @@
|
|
| 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 (
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
gr.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 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.Audio_Transcription_Lib import (speech_to_text)
|
| 11 |
+
from App_Function_Libraries.DB.DB_Manager import add_media_to_database
|
| 12 |
+
#
|
| 13 |
+
#######################################################################################################################
|
| 14 |
+
#
|
| 15 |
+
# Functions:
|
| 16 |
+
|
| 17 |
+
whisper_models = ["small", "medium", "small.en", "medium.en", "medium", "large", "large-v1", "large-v2", "large-v3",
|
| 18 |
+
"distil-large-v2", "distil-medium.en", "distil-small.en"]
|
| 19 |
+
|
| 20 |
+
def create_live_recording_tab():
|
| 21 |
+
with gr.Tab("Live Recording and Transcription"):
|
| 22 |
+
gr.Markdown("# Live Audio Recording and Transcription")
|
| 23 |
+
with gr.Row():
|
| 24 |
+
with gr.Column():
|
| 25 |
+
duration = gr.Slider(minimum=1, maximum=8000, value=15, label="Recording Duration (seconds)")
|
| 26 |
+
whisper_models_input = gr.Dropdown(choices=whisper_models, value="medium", label="Whisper Model")
|
| 27 |
+
vad_filter = gr.Checkbox(label="Use VAD Filter")
|
| 28 |
+
save_recording = gr.Checkbox(label="Save Recording")
|
| 29 |
+
save_to_db = gr.Checkbox(label="Save Transcription to Database(Must be checked to save - can be checked afer transcription)", value=False)
|
| 30 |
+
custom_title = gr.Textbox(label="Custom Title (for database)", visible=False)
|
| 31 |
+
record_button = gr.Button("Start Recording (DOESN"T WORK IN DEMO)")
|
| 32 |
+
stop_button = gr.Button("Stop Recording(DOESN"T WORK IN DEMO)")
|
| 33 |
+
with gr.Column():
|
| 34 |
+
output = gr.Textbox(label="Transcription", lines=10)
|
| 35 |
+
audio_output = gr.Audio(label="Recorded Audio", visible=False)
|
| 36 |
+
|
| 37 |
+
recording_state = gr.State(value=None)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
#
|
| 42 |
+
# End of Functions
|
| 43 |
+
########################################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|