File size: 2,564 Bytes
2a3aa81 a70a295 e08081f 2a3aa81 e08081f 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 2a3aa81 a70a295 e08081f a70a295 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# 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()
|