Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spacy
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
nlp = spacy.load("en_core_web_sm")
|
| 6 |
+
|
| 7 |
+
# --- Dictionaries ---
|
| 8 |
+
VILLAGES = ["Triolet","Curepipe","Flacq","Moka","Port Louis"]
|
| 9 |
+
FIRST_NAMES = ["Amit","Reena","Sanjay","Aisha","Sidharth"]
|
| 10 |
+
LAST_NAMES = ["Ramgoolam","Pudaruth","Lalloo","Hossen"]
|
| 11 |
+
PRODUCTS = ["Honda Fit","Suzuki Swift","Toyota Vitz","iPhone"]
|
| 12 |
+
|
| 13 |
+
REGEX_PATTERNS = {
|
| 14 |
+
"NUMBER": r"\b\d+(?:,\d{3})*(?:\.\d+)?\b",
|
| 15 |
+
"TIME": r"\b\d{1,2}[:h]\d{2}(?:am|pm|AM|PM)?\b",
|
| 16 |
+
"DATE": r"\b\d{4}-\d{2}-\d{2}\b"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
def detect_entities(text):
|
| 20 |
+
ents = []
|
| 21 |
+
|
| 22 |
+
# 1. spaCy detection
|
| 23 |
+
doc = nlp(text)
|
| 24 |
+
for ent in doc.ents:
|
| 25 |
+
ents.append({"text": ent.text, "type": ent.label_})
|
| 26 |
+
|
| 27 |
+
# 2. Dictionary detection
|
| 28 |
+
for word in VILLAGES + FIRST_NAMES + LAST_NAMES + PRODUCTS:
|
| 29 |
+
for m in re.finditer(re.escape(word), text, flags=re.IGNORECASE):
|
| 30 |
+
ents.append({"text": m.group(), "type": "KNOWN"})
|
| 31 |
+
|
| 32 |
+
# 3. Regex detection
|
| 33 |
+
for etype, pattern in REGEX_PATTERNS.items():
|
| 34 |
+
for m in re.finditer(pattern, text):
|
| 35 |
+
ents.append({"text": m.group(), "type": etype})
|
| 36 |
+
|
| 37 |
+
return ents
|
| 38 |
+
|
| 39 |
+
# --- Gradio Interface ---
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=detect_entities,
|
| 42 |
+
inputs=gr.Textbox(label="Text"),
|
| 43 |
+
outputs="json",
|
| 44 |
+
title="Entity Detection API",
|
| 45 |
+
description="API for detecting names, places, numbers, times."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# API endpoint: /api/predict
|
| 49 |
+
demo.launch()
|