Spaces:
Sleeping
Sleeping
support for single-line input
Browse files
app.py
CHANGED
|
@@ -3,50 +3,45 @@ import gradio as gr
|
|
| 3 |
|
| 4 |
|
| 5 |
def insert_points(text):
|
| 6 |
-
# Split
|
| 7 |
lines = text.split('\n')
|
| 8 |
|
| 9 |
counter = 1
|
| 10 |
-
|
| 11 |
|
| 12 |
for line in lines:
|
| 13 |
-
#
|
| 14 |
-
#
|
| 15 |
-
segments = re.split(r'(
|
| 16 |
|
| 17 |
-
# We'll rebuild
|
| 18 |
-
rebuilt_line =
|
| 19 |
|
| 20 |
for seg in segments:
|
| 21 |
-
if re.match(r'
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
#
|
| 35 |
-
|
| 36 |
else:
|
| 37 |
-
# Normal text
|
| 38 |
-
rebuilt_line
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
# Finally, insert a tag after the entire line
|
| 45 |
-
output_lines.append(f"[INSERT_POINT_{counter:03d}]")
|
| 46 |
counter += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
return "\n".join(
|
| 50 |
|
| 51 |
|
| 52 |
demo = gr.Interface(
|
|
@@ -60,7 +55,7 @@ demo = gr.Interface(
|
|
| 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
|
| 64 |
),
|
| 65 |
)
|
| 66 |
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def insert_points(text):
|
| 6 |
+
# Split the input by newline to handle multi-line text
|
| 7 |
lines = text.split('\n')
|
| 8 |
|
| 9 |
counter = 1
|
| 10 |
+
output = []
|
| 11 |
|
| 12 |
for line in lines:
|
| 13 |
+
# This pattern captures sequences of '#' (1 or more) plus optional trailing spaces
|
| 14 |
+
# We'll split the line on these sequences, but keep them so we can re-insert with tags.
|
| 15 |
+
segments = re.split(r'(\#+\s*)', line)
|
| 16 |
|
| 17 |
+
# We'll rebuild this line piece by piece
|
| 18 |
+
rebuilt_line = []
|
| 19 |
|
| 20 |
for seg in segments:
|
| 21 |
+
if re.match(r'^\#+\s*$', seg):
|
| 22 |
+
# This segment is a run of '#' (with optional trailing space)
|
| 23 |
+
# Insert a tag BEFORE it
|
| 24 |
+
tag = f"[INSERT_POINT_{counter:03d}]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
counter += 1
|
| 26 |
+
rebuilt_line.append(tag)
|
| 27 |
|
| 28 |
+
# Then append the actual '#' segment
|
| 29 |
+
rebuilt_line.append(seg)
|
| 30 |
else:
|
| 31 |
+
# Normal text
|
| 32 |
+
rebuilt_line.append(seg)
|
| 33 |
|
| 34 |
+
# After processing all segments in this line,
|
| 35 |
+
# append a final tag to match your "insert a tag after every line" requirement
|
| 36 |
+
line_tag = f"[INSERT_POINT_{counter:03d}]"
|
|
|
|
|
|
|
|
|
|
| 37 |
counter += 1
|
| 38 |
+
rebuilt_line.append(line_tag)
|
| 39 |
+
|
| 40 |
+
# Join the processed segments for this line
|
| 41 |
+
output.append("".join(rebuilt_line))
|
| 42 |
|
| 43 |
+
# Finally, join all lines with newlines
|
| 44 |
+
return "\n".join(output)
|
| 45 |
|
| 46 |
|
| 47 |
demo = gr.Interface(
|
|
|
|
| 55 |
title="Insert Point Tagger",
|
| 56 |
description=(
|
| 57 |
"Paste a block of text and get '[INSERT_POINT_###]' tags added "
|
| 58 |
+
"1) after every newline, and 2) before every '#' sequence."
|
| 59 |
),
|
| 60 |
)
|
| 61 |
|