Update app.py
Browse files4 unmodified lines567891011121314151617181920218 unmodified lines303132333435363738394041424 unmodified lines# Load NER pipelinener = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")# Custom sentence splitting functiondef split_sentences(text):# Avoid splitting after abbreviationsprotected = r"\b(?:[A-Z]\.|D\.C\.|U\.S\.|Mr\.|Mrs\.|Dr\.|Jr\.|Sr\.|vs\.|Inc\.|Ltd\.|etc\.)"# Split on ., ?, ! followed by a space + capital/lowercase/’ (not part of abbreviation)pattern = re.compile(rf"(?<!{protected})(?<=[.!?])\s+(?=[A-Z'‘“a-z])")sentences = re.split(pattern, text.strip())return [s.strip() for s in sentences if s.strip()]# API functiondef analyze_text(text):sentences = split_sentences(text)results = []8 unmodified linesreturn {"sentences": results, "total_sentences": len(sentences)}# Gradio interface (API-style)demo = gr.Interface(fn=analyze_text,inputs=gr.Textbox(label="Input Text", lines=6, placeholder="Paste your article..."),outputs=gr.JSON(label="NER + Sentence Output"),title="Sentence Splitter + NER API",description="Splits text into sentences (with abbreviation protection) and runs NER using dslim/bert-base-NER.")if __name__ == "__main__":4 unmodified lines5678910111213141516171819202122232425262728293031323334353637383940418 unmodified lines505152535455565758596061624 unmodified lines# Load NER pipelinener = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")# --- Safe Sentence Splitter ---def split_sentences(text):"""Splits text into sentences while protecting abbreviations like 'U.S.' or 'Dr.'.Avoids variable-length lookbehind errors by using placeholder replacement."""# Common abbreviations to protectprotected_terms = ["Mr.", "Mrs.", "Ms.", "Dr.", "Jr.", "Sr.","U.S.", "D.C.", "vs.", "Lt.", "St.", "Prof.", "Inc.", "Ltd.", "etc."]# Temporarily replace periods in abbreviations to avoid splittingprotected_map = {term: term.replace(".", "<DOT>") for term in protected_terms}for original, safe in protected_map.items():text = text.replace(original, safe)# Split on ., ?, ! followed by space + capital/lowercase/apostrophepattern = re.compile(r"(?<=[.!?])\s+(?=[A-Z'‘“a-z])")parts = re.split(pattern, text.strip())# Restore abbreviationsrestored = []for s in parts:for original, safe in protected_map.items():s = s.replace(safe, original)restored.append(s.strip())return [s for s in restored if s]# --- API Function ---def analyze_text(text):sentences = split_sentences(text)results = []8 unmodified linesreturn {"sentences": results, "total_sentences": len(sentences)}# --- Gradio Interface (API Style) ---demo = gr.Interface(fn=analyze_text,inputs=gr.Textbox(label="Input Text", lines=6, placeholder="Paste your article..."),outputs=gr.JSON(label="NER + Sentence Output"),title="Sentence Splitter + NER API",description="Splits text into sentences (protects abbreviations) and runs Named Entity Recognition (dslim/bert-base-NER).")if __name__ == "__main__":
app.py+32-12