File size: 1,215 Bytes
522fb71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

def add_line_breaks(file, position):
    try:
        # Read the text from the uploaded file
        text = file.read()

        # Ensure position is within valid range
        position = max(0, min(position, len(text)))

        # Insert \n at the specified position
        modified_text = text[:position] + "\n" + text[position:]

        return modified_text
    except Exception as e:
        return f"Error: {str(e)}"

def main():
    with gr.Blocks() as demo:
        gr.Markdown("# Text Line Break Adder")

        with gr.Row():
            file_input = gr.File(label="Upload Text File", placeholder="Select a .txt file")
            position_input = gr.Number(label="Position to Insert Line Break", 
                                     placeholder="Enter position (0-based index)",
                                     value=0)

        with gr.Row():
            add_button = gr.Button("Add Line Break")
            text_output = gr.Textbox(label="Modified Text", lines=10)

        add_button.click(
            fn=add_line_breaks,
            inputs=[file_input, position_input],
            outputs=text_output
        )

    demo.launch()

if __name__ == "__main__":
    main()