CoolFace
Apppublic

Slamlab/preprocessing

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py58 linesDownload Raw Back to root
1import gradio as gr2 3def split_into_sentences(text):4    # Split the text into sentences based on periods followed by spaces5    sentences = text.split('. ')6 7    # Add a period back to each sentence except the last one if it doesn't end with a period8    for i in range(len(sentences) - 1):9        sentences[i] += '.'10 11    # Join the sentences with newline characters12    return '\n'.join(sentences)13 14def main():15    with gr.Blocks() as demo:16        gr.Markdown("# Text to Sentences Splitter")17 18        with gr.Row():19            text_input = gr.Textbox(label="Enter or paste your text", lines=5)20            file_input = gr.File(label="Upload Text File")21 22        with gr.Row():23            split_button = gr.Button("Split into Sentences")24            sentences_output = gr.Textbox(label="Sentences", lines=10)25            preview_output = gr.Textbox(label="Preview", lines=3)26 27        # Update preview when file is uploaded28        def update_preview(file):29            if file:30                with open(file.name, 'r') as f:31                    preview_text = f.read()32                    return preview_text[:200]  # Show first 200 characters33            return ""34 35        # Split text into sentences36        def process_text(text, file):37            if file:38                with open(file.name, 'r') as f:39                    text = f.read()40            return split_into_sentences(text)41 42        split_button.click(43            fn=process_text,44            inputs=[text_input, file_input],45            outputs=sentences_output46        )47 48        file_input.change(49            fn=update_preview,50            inputs=[file_input],51            outputs=[preview_output]52        )53 54    demo.launch()55 56if __name__ == "__main__":57    main()58