oceansweep commited on
Commit
4799b42
1 Parent(s): 7708101

Upload Live_Recording.py

Browse files
App_Function_Libraries/Gradio_UI/Live_Recording.py CHANGED
@@ -2,11 +2,13 @@
2
  # Description: Gradio UI for live audio recording and transcription.
3
  #
4
  # Import necessary modules and functions
 
5
  import os
6
  # External Imports
7
  import gradio as gr
8
  # Local Imports
9
- from App_Function_Libraries.Audio_Transcription_Lib import (record_audio, speech_to_text, save_audio_temp)
 
10
  from App_Function_Libraries.DB.DB_Manager import add_media_to_database
11
  #
12
  #######################################################################################################################
@@ -25,15 +27,27 @@ def create_live_recording_tab():
25
  whisper_models_input = gr.Dropdown(choices=whisper_models, value="medium", label="Whisper Model")
26
  vad_filter = gr.Checkbox(label="Use VAD Filter")
27
  save_recording = gr.Checkbox(label="Save Recording")
28
- save_to_db = gr.Checkbox(label="Save Transcription to Database")
29
  custom_title = gr.Textbox(label="Custom Title (for database)", visible=False)
30
- record_button = gr.Button("Record and Transcribe")
 
31
  with gr.Column():
32
  output = gr.Textbox(label="Transcription", lines=10)
33
  audio_output = gr.Audio(label="Recorded Audio", visible=False)
34
 
35
- def record_and_transcribe(duration, whisper_model, vad_filter, save_recording):
36
- audio_data = record_audio(duration)
 
 
 
 
 
 
 
 
 
 
 
37
  temp_file = save_audio_temp(audio_data)
38
  segments = speech_to_text(temp_file, whisper_model=whisper_model, vad_filter=vad_filter)
39
  transcription = "\n".join([segment["Text"] for segment in segments])
@@ -48,23 +62,47 @@ def create_live_recording_tab():
48
  if custom_title.strip() == "":
49
  custom_title = "Self-recorded Audio"
50
 
51
- add_media_to_database(
52
- url="self_recorded",
53
- info_dict={"title": custom_title, "uploader": "self-recorded"},
54
- segments=[{"Text": transcription}],
55
- summary="",
56
- keywords="self-recorded,audio",
57
- custom_prompt_input="",
58
- whisper_model="self-recorded"
59
- )
60
- return "Transcription saved to database successfully."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  def update_custom_title_visibility(save_to_db):
63
  return gr.update(visible=save_to_db)
64
 
65
  record_button.click(
66
- fn=record_and_transcribe,
67
- inputs=[duration, whisper_models_input, vad_filter, save_recording],
 
 
 
 
 
 
68
  outputs=[output, audio_output]
69
  )
70
 
 
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
  #######################################################################################################################
 
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])
 
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