preprocessing / app.py
akki2825's picture
add app
522fb71 verified
raw
history blame
1.22 kB
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()