NER / app.py
Shenuki's picture
Update app.py
2a3aa81 verified
raw
history blame
2.56 kB
# app.py
import requests
import wikipedia
import gradio as gr
from transformers import pipeline
# 1) Load the BERT NER pipeline
ner = pipeline(
"ner",
model="dslim/bert-base-NER-uncased",
grouped_entities=True
)
# 2) Geocode via Nominatim
def geocode(place: str):
resp = requests.get(
"https://nominatim.openstreetmap.org/search",
params={"q": place, "format": "json", "limit": 1},
headers={"User-Agent": "iVoiceContext/1.0"}
)
data = resp.json()
if not data:
return None
return float(data[0]["lat"]), float(data[0]["lon"])
# 3) Fetch POIs via Overpass
def fetch_osm(lat, lon, osm_filter, limit=5):
query = f"""
[out:json][timeout:25];
(
node{osm_filter}(around:1000,{lat},{lon});
way{osm_filter}(around:1000,{lat},{lon});
rel{osm_filter}(around:1000,{lat},{lon});
);
out center {limit};
"""
r = requests.post("https://overpass-api.de/api/interpreter", data={"data": query})
elems = r.json().get("elements", [])
items = []
for e in elems:
name = e.get("tags", {}).get("name")
if name:
items.append({"name": name})
return items
# 4) Main function
def get_context(text: str):
results = ner(text)
# de-duplicate entities by text
ents = {ent["word"]: ent["entity_group"] for ent in results}
out = {}
for word, label in ents.items():
if label == "LOC":
geo = geocode(word)
if not geo:
out[word] = {"type":"location", "error":"could not geocode"}
else:
lat, lon = geo
out[word] = {
"type": "location",
"restaurants": fetch_osm(lat, lon, '["amenity"="restaurant"]'),
"attractions": fetch_osm(lat, lon, '["tourism"="attraction"]'),
}
else:
# for PERSON, ORG, MISC, etc → Wikipedia
try:
summary = wikipedia.summary(word, sentences=2)
except Exception:
summary = "No summary available."
out[word] = {"type": "wiki", "summary": summary}
if not out:
return {"error": "No entities found"}
return out
# 5) Gradio UI
iface = gr.Interface(
fn=get_context,
inputs=gr.Textbox(lines=3, placeholder="Paste your translated text…"),
outputs="json",
title="iVoice Context-Aware",
description="BERT NER → geocode LOC → Overpass POIs → Wikipedia for others"
)
if __name__ == "__main__":
iface.launch()