File size: 2,467 Bytes
258d1de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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()