Overthrow4232 commited on
Commit
04ca745
1 Parent(s): 809eaa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import spaces
2
  import gradio as gr
3
  from wtpsplit import SaT
4
  import json
@@ -7,22 +6,20 @@ import json
7
  sat = SaT("sat-12l-sm")
8
  sat.half().to("cuda")
9
 
10
- @spaces.GPU(duration=59)
11
- def segment_text(input_text, txt_file):
12
  results = {}
13
 
14
  if input_text:
15
  # Process single text input
16
  sentences = sat.split(input_text)
17
  results["input_text"] = {"segments": sentences}
18
- elif txt_file is not None:
19
- # Process txt file
20
- with open(txt_file.name, 'r', encoding='utf-8') as file:
21
- for i, line in enumerate(file, 1):
22
- line = line.strip()
23
- if line: # Skip empty lines
24
- sentences = sat.split(line)
25
- results[f"document_{i}"] = {"segments": sentences}
26
 
27
  # Create a JSON object with the results
28
  json_output = json.dumps(results, indent=2)
@@ -34,15 +31,16 @@ iface = gr.Interface(
34
  fn=segment_text,
35
  inputs=[
36
  gr.Textbox(lines=5, label="Input Text (Optional)"),
37
- gr.File(label="Upload TXT file (Optional) Row-separated", file_types=[".txt"])
38
  ],
39
  outputs=gr.JSON(label="Segmented Text (JSON)"),
40
  title="Text Segmentation with SaT",
41
- description="This app uses the SaT (Segment any Text) model to split input text into sentences and return the result as JSON. You can input text directly or upload a TXT file containing multiple documents (one per line). All credits to the respective author(s). Github: https://github.com/segment-any-text/wtpsplit/tree/main",
42
  examples=[
43
- ["This is a test This is another test.", None],
44
- ["Hello this is a test But this is different now Now the next one starts looool", None],
45
- ["The quick brown fox jumps over the lazy dog It was the best of times, it was the worst of times", None],
 
46
  ]
47
  )
48
 
 
 
1
  import gradio as gr
2
  from wtpsplit import SaT
3
  import json
 
6
  sat = SaT("sat-12l-sm")
7
  sat.half().to("cuda")
8
 
9
+ def segment_text(input_text, multi_doc_input):
 
10
  results = {}
11
 
12
  if input_text:
13
  # Process single text input
14
  sentences = sat.split(input_text)
15
  results["input_text"] = {"segments": sentences}
16
+
17
+ if multi_doc_input:
18
+ # Process multiple documents
19
+ documents = [doc.strip() for doc in multi_doc_input.split('\n') if doc.strip()]
20
+ for i, doc in enumerate(documents, 1):
21
+ sentences = sat.split(doc)
22
+ results[f"row_{i}"] = {"segments": sentences}
 
23
 
24
  # Create a JSON object with the results
25
  json_output = json.dumps(results, indent=2)
 
31
  fn=segment_text,
32
  inputs=[
33
  gr.Textbox(lines=5, label="Input Text (Optional)"),
34
+ gr.Textbox(lines=10, label="Multiple Documents (Optional, one per line)")
35
  ],
36
  outputs=gr.JSON(label="Segmented Text (JSON)"),
37
  title="Text Segmentation with SaT",
38
+ description="This app uses the SaT (Segment any Text) model to split input text into sentences and return the result as JSON. You can input text directly or provide multiple documents (one per line). All credits to the respective author(s). Github: https://github.com/segment-any-text/wtpsplit/tree/main",
39
  examples=[
40
+ ["This is a test This is another test.", ""],
41
+ ["Hello this is a test But this is different now Now the next one starts looool", ""],
42
+ ["The quick brown fox jumps over the lazy dog It was the best of times, it was the worst of times", ""],
43
+ ["", "Document 1 first sentence Document 1 second sentence\nDocument 2 only sentence\nDocument 3 first Document 3 second"]
44
  ]
45
  )
46