Spaces:
Sleeping
Sleeping
import gradio as gr | |
import os | |
class TextFileReader: | |
def __init__(self): | |
self.lines = [] | |
self.current_index = 0 | |
def read_lines(self, file): | |
self.lines = file.decode('utf-8').splitlines() | |
self.current_index = 0 | |
return self.get_current_line() | |
def get_current_line(self): | |
if 0 <= self.current_index < len(self.lines): | |
return self.lines[self.current_index] | |
else: | |
return "End of file reached." | |
def forward_line(self): | |
self.current_index = min(self.current_index + 1, len(self.lines) - 1) | |
return self.get_current_line() | |
def backward_line(self): | |
self.current_index = max(self.current_index - 1, 0) | |
return self.get_current_line() | |
# Initialize the text reader | |
reader = TextFileReader() | |
# Define a function to save the text lines to a file | |
def save_text_lines(file): | |
lines = reader.read_lines(file) | |
with open("text_lines.txt", "w") as f: | |
f.write("\n".join(reader.lines)) | |
return lines | |
# Define a function to save the audio file | |
def save_audio_file(audio): | |
with open("recorded_audio.wav", "wb") as f: | |
f.write(audio['data']) | |
return audio | |
# Define the Gradio interface | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
file_upload = gr.File(label="Upload a text file", type="binary") | |
generate_button = gr.Button("Generate Lines") | |
current_line = gr.Textbox(label="Current Line") | |
with gr.Row(): | |
prev_button = gr.Button("Previous") | |
next_button = gr.Button("Next") | |
def update_output(file): | |
lines = reader.read_lines(file) | |
save_text_lines(file) # Save the text lines to a file | |
return lines | |
def forward_output(): | |
return reader.forward_line() | |
def backward_output(): | |
return reader.backward_line() | |
generate_button.click(fn=update_output, inputs=file_upload, outputs=current_line) | |
prev_button.click(fn=backward_output, inputs=None, outputs=current_line) | |
next_button.click(fn=forward_output, inputs=None, outputs=current_line) | |
with gr.Row(): | |
audio_record = gr.Audio(source="microphone", type="file", label="Record Audio") | |
audio_output = gr.Audio(label="Recorded Audio") | |
def display_audio(audio): | |
save_audio_file(audio) # Save the audio file | |
return audio | |
audio_record.change(fn=display_audio, inputs=audio_record, outputs=audio_output) | |
demo.launch() | |