|
|
|
|
|
import requests |
|
import wikipedia |
|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
ner = pipeline( |
|
"ner", |
|
model="dslim/bert-base-NER-uncased", |
|
grouped_entities=True |
|
) |
|
|
|
|
|
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"]) |
|
|
|
|
|
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 |
|
|
|
|
|
def get_context(text: str): |
|
results = ner(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: |
|
|
|
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 |
|
|
|
|
|
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() |
|
|