Sajjo commited on
Commit
49c524c
·
verified ·
1 Parent(s): d90e8cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -25
app.py CHANGED
@@ -1,7 +1,9 @@
1
- import gradio as gr
2
- import os
3
  HF_TOKEN = os.getenv('HW_Token')
4
  hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "save_audio")
 
 
 
5
  class TextFileReader:
6
  def __init__(self):
7
  self.lines = []
@@ -35,12 +37,22 @@ def save_text_lines(file):
35
  f.write("\n".join(reader.lines))
36
  return lines
37
 
38
- # Define a function to save the audio file
39
- def save_audio_file(audio):
40
- audio_path = "/tmp/recorded_audio.wav"
 
 
 
 
41
  with open(audio_path, "wb") as f:
42
  f.write(audio['data'])
43
- return audio_path
 
 
 
 
 
 
44
 
45
  # Define the Gradio interface
46
  with gr.Blocks() as demo:
@@ -50,33 +62,17 @@ with gr.Blocks() as demo:
50
 
51
  current_line = gr.Textbox(label="Current Line")
52
 
53
- with gr.Row():
54
- prev_button = gr.Button("Previous")
55
- next_button = gr.Button("Next")
56
-
57
  def update_output(file):
58
  lines = reader.read_lines(file)
59
  save_text_lines(file) # Save the text lines to a file
60
  return lines
61
 
62
- def forward_output():
63
- return reader.forward_line()
64
-
65
- def backward_output():
66
- return reader.backward_line()
67
-
68
  generate_button.click(fn=update_output, inputs=file_upload, outputs=current_line)
69
- prev_button.click(fn=backward_output, inputs=None, outputs=current_line)
70
- next_button.click(fn=forward_output, inputs=None, outputs=current_line)
71
 
72
  with gr.Row():
73
- audio_record = gr.Audio(sources=["microphone","upload"], type="filepath")
74
- audio_output = gr.Audio(label="Recorded Audio")
75
-
76
- def display_audio(audio):
77
- audio_path = save_audio_file(audio) # Save the audio file
78
- return audio_path
79
 
80
- audio_record.change(fn=display_audio, inputs=audio_record, outputs=audio_output)
81
 
82
  demo.launch()
 
1
+
 
2
  HF_TOKEN = os.getenv('HW_Token')
3
  hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "save_audio")
4
+ import gradio as gr
5
+ import os
6
+
7
  class TextFileReader:
8
  def __init__(self):
9
  self.lines = []
 
37
  f.write("\n".join(reader.lines))
38
  return lines
39
 
40
+ # Define a function to save the audio file and corresponding text
41
+ def save_audio_text(audio, text):
42
+ if not os.path.exists("/tmp/recordings"):
43
+ os.makedirs("/tmp/recordings")
44
+ audio_path = f"/tmp/recordings/line_{reader.current_index}.wav"
45
+ text_path = f"/tmp/recordings/line_{reader.current_index}.txt"
46
+
47
  with open(audio_path, "wb") as f:
48
  f.write(audio['data'])
49
+
50
+ with open(text_path, "w") as f:
51
+ f.write(text)
52
+
53
+ # Move to the next line after saving
54
+ next_line = reader.forward_line()
55
+ return next_line
56
 
57
  # Define the Gradio interface
58
  with gr.Blocks() as demo:
 
62
 
63
  current_line = gr.Textbox(label="Current Line")
64
 
 
 
 
 
65
  def update_output(file):
66
  lines = reader.read_lines(file)
67
  save_text_lines(file) # Save the text lines to a file
68
  return lines
69
 
 
 
 
 
 
 
70
  generate_button.click(fn=update_output, inputs=file_upload, outputs=current_line)
 
 
71
 
72
  with gr.Row():
73
+ audio_record = gr.Audio(source="microphone", type="file", label="Record Audio")
74
+ save_button = gr.Button("Save Audio and Next Line")
 
 
 
 
75
 
76
+ save_button.click(fn=save_audio_text, inputs=[audio_record, current_line], outputs=current_line)
77
 
78
  demo.launch()