Overthrow4232 commited on
Commit
b71a375
1 Parent(s): f838b34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -17,11 +17,14 @@ def segment_text(input_text, multi_doc_input):
17
  results["input_text"] = {"segments": sentences}
18
 
19
  if multi_doc_input:
20
- # Process multiple documents
21
- documents = [doc.strip() for doc in multi_doc_input.split('\n') if doc.strip()]
22
- for i, doc in enumerate(documents, 1):
23
- sentences = sat.split(doc)
24
- results[f"row_{i}"] = {"segments": sentences}
 
 
 
25
 
26
  # Create a JSON object with the results
27
  json_output = json.dumps(results, indent=2)
@@ -33,16 +36,16 @@ iface = gr.Interface(
33
  fn=segment_text,
34
  inputs=[
35
  gr.Textbox(lines=5, label="Input Text (Optional)"),
36
- gr.Textbox(lines=10, label="Multiple Documents (Optional, one per line)")
37
  ],
38
  outputs=gr.JSON(label="Segmented Text (JSON)"),
39
  title="Text Segmentation with SaT",
40
- 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",
41
  examples=[
42
  ["This is a test This is another test.", ""],
43
  ["Hello this is a test But this is different now Now the next one starts looool", ""],
44
  ["The quick brown fox jumps over the lazy dog It was the best of times, it was the worst of times", ""],
45
- ["", "Document 1 first sentence Document 1 second sentence\nDocument 2 only sentence\nDocument 3 first Document 3 second"]
46
  ]
47
  )
48
 
 
17
  results["input_text"] = {"segments": sentences}
18
 
19
  if multi_doc_input:
20
+ try:
21
+ # Parse the JSON input
22
+ documents = json.loads(multi_doc_input)
23
+ for key, doc in documents.items():
24
+ sentences = sat.split(doc)
25
+ results[f"doc_{key}"] = {"segments": sentences}
26
+ except json.JSONDecodeError:
27
+ results["error"] = "Invalid JSON input for multiple documents"
28
 
29
  # Create a JSON object with the results
30
  json_output = json.dumps(results, indent=2)
 
36
  fn=segment_text,
37
  inputs=[
38
  gr.Textbox(lines=5, label="Input Text (Optional)"),
39
+ gr.Textbox(lines=10, label="Multiple Documents JSON (Optional)", placeholder='{"1": "Document 1 text", "2": "Document 2 text"}')
40
  ],
41
  outputs=gr.JSON(label="Segmented Text (JSON)"),
42
  title="Text Segmentation with SaT",
43
+ 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 as JSON. All credits to the respective author(s). Github: https://github.com/segment-any-text/wtpsplit/tree/main",
44
  examples=[
45
  ["This is a test This is another test.", ""],
46
  ["Hello this is a test But this is different now Now the next one starts looool", ""],
47
  ["The quick brown fox jumps over the lazy dog It was the best of times, it was the worst of times", ""],
48
+ ["", '{"1": "Document 1 first sentence Document 1 second sentence", "2": "Document 2 only sentence", "3": "Document 3 first Document 3 second"}']
49
  ]
50
  )
51