Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| def split_into_sentences(text): | |
| # Split the text by newline characters | |
| sentences = text.split('\n') | |
| # Clean up any leading/trailing whitespace in each sentence | |
| sentences = [s.strip() for s in sentences] | |
| # Join the sentences with newline characters to display each on a new line | |
| 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() | |