Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
class TextFileReader:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
self.lines = []
|
| 7 |
+
self.current_index = 0
|
| 8 |
+
|
| 9 |
+
def read_lines(self, file):
|
| 10 |
+
self.lines = file.decode('utf-8').splitlines()
|
| 11 |
+
self.current_index = 0
|
| 12 |
+
return self.get_current_line()
|
| 13 |
+
|
| 14 |
+
def get_current_line(self):
|
| 15 |
+
if 0 <= self.current_index < len(self.lines):
|
| 16 |
+
return self.lines[self.current_index]
|
| 17 |
+
else:
|
| 18 |
+
return "End of file reached."
|
| 19 |
+
|
| 20 |
+
def forward_line(self):
|
| 21 |
+
self.current_index = min(self.current_index + 1, len(self.lines) - 1)
|
| 22 |
+
return self.get_current_line()
|
| 23 |
+
|
| 24 |
+
def backward_line(self):
|
| 25 |
+
self.current_index = max(self.current_index - 1, 0)
|
| 26 |
+
return self.get_current_line()
|
| 27 |
+
|
| 28 |
+
# Initialize the text reader
|
| 29 |
+
reader = TextFileReader()
|
| 30 |
+
|
| 31 |
+
# Define a function to save the text lines to a file
|
| 32 |
+
def save_text_lines(file):
|
| 33 |
+
lines = reader.read_lines(file)
|
| 34 |
+
with open("text_lines.txt", "w") as f:
|
| 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 |
+
with open("recorded_audio.wav", "wb") as f:
|
| 41 |
+
f.write(audio['data'])
|
| 42 |
+
return audio
|
| 43 |
+
|
| 44 |
+
# Define the Gradio interface
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
with gr.Row():
|
| 47 |
+
file_upload = gr.File(label="Upload a text file", type="binary")
|
| 48 |
+
generate_button = gr.Button("Generate Lines")
|
| 49 |
+
|
| 50 |
+
current_line = gr.Textbox(label="Current Line")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
prev_button = gr.Button("Previous")
|
| 54 |
+
next_button = gr.Button("Next")
|
| 55 |
+
|
| 56 |
+
def update_output(file):
|
| 57 |
+
lines = reader.read_lines(file)
|
| 58 |
+
save_text_lines(file) # Save the text lines to a file
|
| 59 |
+
return lines
|
| 60 |
+
|
| 61 |
+
def forward_output():
|
| 62 |
+
return reader.forward_line()
|
| 63 |
+
|
| 64 |
+
def backward_output():
|
| 65 |
+
return reader.backward_line()
|
| 66 |
+
|
| 67 |
+
generate_button.click(fn=update_output, inputs=file_upload, outputs=current_line)
|
| 68 |
+
prev_button.click(fn=backward_output, inputs=None, outputs=current_line)
|
| 69 |
+
next_button.click(fn=forward_output, inputs=None, outputs=current_line)
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
audio_record = gr.Audio(source="microphone", type="file", label="Record Audio")
|
| 73 |
+
audio_output = gr.Audio(label="Recorded Audio")
|
| 74 |
+
|
| 75 |
+
def display_audio(audio):
|
| 76 |
+
save_audio_file(audio) # Save the audio file
|
| 77 |
+
return audio
|
| 78 |
+
|
| 79 |
+
audio_record.change(fn=display_audio, inputs=audio_record, outputs=audio_output)
|
| 80 |
+
|
| 81 |
+
demo.launch()
|