Spaces:
Sleeping
Sleeping
support for ###
Browse files
app.py
CHANGED
|
@@ -1,24 +1,67 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
|
|
|
| 3 |
def insert_points(text):
|
|
|
|
| 4 |
lines = text.split('\n')
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
demo = gr.Interface(
|
| 17 |
fn=insert_points,
|
| 18 |
-
inputs=gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
outputs="text",
|
| 20 |
title="Insert Point Tagger",
|
| 21 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import re
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
|
| 5 |
def insert_points(text):
|
| 6 |
+
# Split into lines first (handles the "after each line" insertion)
|
| 7 |
lines = text.split('\n')
|
| 8 |
+
|
| 9 |
+
counter = 1
|
| 10 |
+
output_lines = []
|
| 11 |
+
|
| 12 |
+
for line in lines:
|
| 13 |
+
# Split the line by sequences of #, but keep the # parts as separate "segments"
|
| 14 |
+
# e.g. "Hello ## World # Another" -> ["Hello ", "##", " World ", "#", " Another"]
|
| 15 |
+
segments = re.split(r'(#+)', line)
|
| 16 |
+
|
| 17 |
+
# We'll rebuild the line piece by piece, inserting tags whenever we hit a # segment
|
| 18 |
+
rebuilt_line = ""
|
| 19 |
+
|
| 20 |
+
for seg in segments:
|
| 21 |
+
if re.match(r'^#+$', seg):
|
| 22 |
+
# seg is a sequence of '#' (e.g., "##", "###")
|
| 23 |
+
|
| 24 |
+
# 1) If there's any "normal text" waiting in rebuilt_line,
|
| 25 |
+
# we push that as a separate line to the output first.
|
| 26 |
+
if rebuilt_line:
|
| 27 |
+
output_lines.append(rebuilt_line)
|
| 28 |
+
rebuilt_line = ""
|
| 29 |
+
|
| 30 |
+
# 2) Insert a tag line right BEFORE the # sequence
|
| 31 |
+
output_lines.append(f"\n[INSERT_POINT_{counter:03d}]\n")
|
| 32 |
+
counter += 1
|
| 33 |
+
|
| 34 |
+
# 3) Now add the actual # sequence on its own line
|
| 35 |
+
output_lines.append(seg)
|
| 36 |
+
else:
|
| 37 |
+
# Normal text (no #)
|
| 38 |
+
rebuilt_line += seg
|
| 39 |
+
|
| 40 |
+
# If there's leftover normal text after processing all # segments
|
| 41 |
+
if rebuilt_line:
|
| 42 |
+
output_lines.append(rebuilt_line)
|
| 43 |
+
|
| 44 |
+
# Finally, insert a tag after the entire line
|
| 45 |
+
output_lines.append(f"[INSERT_POINT_{counter:03d}]")
|
| 46 |
+
counter += 1
|
| 47 |
+
|
| 48 |
+
# Join everything with newlines
|
| 49 |
+
return "\n".join(output_lines)
|
| 50 |
+
|
| 51 |
|
| 52 |
demo = gr.Interface(
|
| 53 |
fn=insert_points,
|
| 54 |
+
inputs=gr.Textbox(
|
| 55 |
+
lines=10,
|
| 56 |
+
placeholder="Paste your text here...",
|
| 57 |
+
label="Your Input Text"
|
| 58 |
+
),
|
| 59 |
outputs="text",
|
| 60 |
title="Insert Point Tagger",
|
| 61 |
+
description=(
|
| 62 |
+
"Paste a block of text and get '[INSERT_POINT_###]' tags added "
|
| 63 |
+
"after every newline and before every sequence of '#'."
|
| 64 |
+
),
|
| 65 |
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|