BtB-ExpC commited on
Commit
b9a12d4
·
1 Parent(s): 1311701

support for single-line input

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -3,50 +3,45 @@ 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(
@@ -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 sequence of '#'."
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