preprocessing / app.py
akki2825's picture
fix fn
6df0a02 verified
import gradio as gr
def split_into_sentences(text):
# Split the text into sentences based on periods followed by spaces
sentences = text.split('. ')
# Add a period back to each sentence except the last one if it doesn't end with a period
for i in range(len(sentences) - 1):
sentences[i] += '.'
# Join the sentences with newline characters
return '\n'.join(sentences)
def main():
with gr.Blocks() as demo:
gr.Markdown("# Text to Sentences Splitter")
with gr.Row():
text_input = gr.Textbox(label="Enter or paste your text", lines=5)
file_input = gr.File(label="Upload Text File")
with gr.Row():
split_button = gr.Button("Split into Sentences")
sentences_output = gr.Textbox(label="Sentences", lines=10)
preview_output = gr.Textbox(label="Preview", lines=3)
# Update preview when file is uploaded
def update_preview(file):
if file:
with open(file.name, 'r') as f:
preview_text = f.read()
return preview_text[:200] # Show first 200 characters
return ""
# Split text into sentences
def process_text(text, file):
if file:
with open(file.name, 'r') as f:
text = f.read()
return split_into_sentences(text)
split_button.click(
fn=process_text,
inputs=[text_input, file_input],
outputs=sentences_output
)
file_input.change(
fn=update_preview,
inputs=[file_input],
outputs=[preview_output]
)
demo.launch()
if __name__ == "__main__":
main()