Spaces:
Sleeping
Sleeping
File size: 1,746 Bytes
522fb71 c74db10 6df0a02 c74db10 6df0a02 c74db10 6df0a02 c74db10 522fb71 15a3e70 522fb71 15a3e70 c74db10 522fb71 15a3e70 c74db10 522fb71 15a3e70 c74db10 15a3e70 522fb71 c74db10 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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()
|