akki2825 commited on
Commit
c74db10
·
verified ·
1 Parent(s): 15a3e70
Files changed (1) hide show
  1. app.py +34 -8
app.py CHANGED
@@ -1,10 +1,14 @@
1
  import gradio as gr
2
 
3
- def split_into_sentences(text, remove_whitespace):
 
4
  sentences = text.split('\n')
5
- if remove_whitespace:
6
- sentences = [s.strip() for s in sentences if s.strip() != '']
7
- return sentences
 
 
 
8
 
9
  def main():
10
  with gr.Blocks() as demo:
@@ -12,18 +16,40 @@ def main():
12
 
13
  with gr.Row():
14
  text_input = gr.Textbox(label="Enter or paste your text", lines=5)
15
- whitespace_checkbox = gr.Checkbox(label="Remove extra whitespace", value=True)
16
 
17
  with gr.Row():
18
  split_button = gr.Button("Split into Sentences")
19
- sentences_output = gr.Textbox(label="Sentences", lines=5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  split_button.click(
22
- fn=split_into_sentences,
23
- inputs=[text_input, whitespace_checkbox],
24
  outputs=sentences_output
25
  )
26
 
 
 
 
 
 
 
27
  demo.launch()
28
 
29
  if __name__ == "__main__":
 
1
  import gradio as gr
2
 
3
+ def split_into_sentences(text):
4
+ # Split the text by newline characters
5
  sentences = text.split('\n')
6
+
7
+ # Clean up any leading/trailing whitespace in each sentence
8
+ sentences = [s.strip() for s in sentences]
9
+
10
+ # Join the sentences with newline characters to display each on a new line
11
+ return '\n'.join(sentences)
12
 
13
  def main():
14
  with gr.Blocks() as demo:
 
16
 
17
  with gr.Row():
18
  text_input = gr.Textbox(label="Enter or paste your text", lines=5)
19
+ file_input = gr.File(label="Upload Text File")
20
 
21
  with gr.Row():
22
  split_button = gr.Button("Split into Sentences")
23
+ sentences_output = gr.Textbox(label="Sentences", lines=10)
24
+ preview_output = gr.Textbox(label="Preview", lines=3)
25
+
26
+ # Update preview when file is uploaded
27
+ def update_preview(file):
28
+ if file:
29
+ with open(file.name, 'r') as f:
30
+ preview_text = f.read()
31
+ return preview_text[:200] # Show first 200 characters
32
+ return ""
33
+
34
+ # Split text into sentences
35
+ def process_text(text, file):
36
+ if file:
37
+ with open(file.name, 'r') as f:
38
+ text = f.read()
39
+ return split_into_sentences(text)
40
 
41
  split_button.click(
42
+ fn=process_text,
43
+ inputs=[text_input, file_input],
44
  outputs=sentences_output
45
  )
46
 
47
+ file_input.change(
48
+ fn=update_preview,
49
+ inputs=[file_input],
50
+ outputs=[preview_output]
51
+ )
52
+
53
  demo.launch()
54
 
55
  if __name__ == "__main__":