Spaces:
Sleeping
Sleeping
try new approach
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import reimport gradio as grdef insert_points(text):# --- Step 0: Normalize line endings ---# Replace Windows (\r\n) and old Mac (\r) line endings with Unix (\n)# This simplifies the regex patterns below.text = text.replace('\r\n', '\n').replace('\r', '\n')counter = 1# Replacer function for the initial insertion passdef replacer(match):nonlocal countertag = f"[INSERT_POINT_{counter:03d}]"counter += 1# Return the tag followed by the original matched text# group(0) will be one or more newlines OR one or more hashesreturn tag + match.group(0)# --- Step 1: Insert tags before hash sequences and consolidated newline sequences ---# Pattern matches:# 1) '\n+' : One or more consecutive (now normalized) newline characters. Collapses blank lines.# 2) '\#+' : One or more consecutive '#' characters.pattern_initial = r'(\n+|\#+)'processed_text = re.sub(pattern_initial, replacer, text)# --- Step 2: Clean up potential heading splits ---# This fixes cases where Step 1 resulted in "[TAG_A]\n[TAG_B]###"# It looks for TAG_A, followed by whitespace (\s* includes the \n),# followed by TAG_B, followed by hashes (#+).cleanup_pattern = r'(\[INSERT_POINT_\d{3}\])\s*(\[INSERT_POINT_\d{3}\])(\#+)'# Replaces the whole match with just TAG_B (\2) and the hashes (\3).processed_text = re.sub(cleanup_pattern, r'\2\3', processed_text)return processed_text# --- Gradio Interface Code (with Copy Button) ---demo = gr.Interface(fn=insert_points,inputs=gr.Textbox(lines=10,placeholder="Paste your text here...\n(Line endings will be normalized)",label="Your Input Text"),outputs=gr.Textbox(label="Processed Text with Tags",show_copy_button=True # <--- Added copy button here),title="Insert Point Tagger v3", # Optional: update titledescription=("Paste text to add '[INSERT_POINT_###]' tags:\n""1) **Before** each '#' sequence (headings).\n""2) **Before** each sequence of one or more newlines (one tag per line break/blank line).\n""Normalizes line endings and cleans up heading tag spacing."),allow_flagging='never')if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import reimport gradio as grdef insert_points(text):counter = 1# -----------------------------# 1) COLLAPSE MULTIPLE BLANK LINES INTO ONE# e.g. 2+ newlines in a row => single newline# -----------------------------text = re.sub(r'\n\s*\n+', '\n', text)# -----------------------------# 2) INSERT A TAG BEFORE EACH REMAINING NEWLINE# So every line (including blank ones) is prefixed by exactly one tag# -----------------------------def newline_replacer(_match):nonlocal counterout = f"[INSERT_POINT_{counter:03d}]\n"counter += 1return outtext = re.sub(r'\n', newline_replacer, text)# -----------------------------# 3) INSERT A TAG BEFORE EACH HEADING IF IT'S NOT# (A) ALREADY TAGGED, OR# (B) IMMEDIATELY AFTER A NEWLINE (WHICH WOULD HAVE A TAG)## We detect # or ## or ### up to 6 in a row. If there's no# [INSERT_POINT_\d{3}] or newline right before, we insert a new tag.# -----------------------------def heading_replacer(m):nonlocal counter# group(1) is the string of hashes, like "##" or "#####"hashes = m.group(1)replacement = f"[INSERT_POINT_{counter:03d}]{hashes}"counter += 1return replacement# Negative lookbehind means:# - Not preceded by [INSERT_POINT_###]# - Not preceded by a newline# So if the line starts with "##" and we've just inserted a tag for the line break,# we won't insert a second tag for the heading.pattern = r'(?<!\[INSERT_POINT_\d{3}\])(?<!\n)(\#{1,6})'text = re.sub(pattern, heading_replacer, text)return text# -----------------------------# GRADIO INTERFACE# -----------------------------demo = gr.Interface(fn=insert_points,inputs=gr.Textbox(lines=10,placeholder="Paste your text here...",label="Your Input Text"),outputs=gr.Textbox(label="Processed Text with Tags"),title="Insert Point Tagger",description=("1) Collapses multiple blank lines into one.\n""2) Inserts a single [INSERT_POINT_XXX] tag before each remaining newline.\n""3) Inserts a single [INSERT_POINT_XXX] tag before any '#' heading, unless\n"" there's already a tag from the preceding newline.\n""This avoids double tags while keeping heading text on the same line."),)if __name__ == "__main__":demo.launch()
app.py+56-42