Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,13 @@ from transformers import pipeline
|
|
3 |
from langdetect import detect
|
4 |
import requests
|
5 |
import wikipedia
|
|
|
6 |
|
7 |
# Load NER model
|
8 |
ner_pipeline = pipeline("ner", model="Davlan/xlm-roberta-base-ner-hrl", grouped_entities=True)
|
9 |
|
10 |
# Get Wikidata entity info
|
|
|
11 |
def get_wikidata_info(entity, lang="en"):
|
12 |
query = f'''
|
13 |
SELECT ?item ?itemLabel ?itemDescription ?coordinate WHERE {{
|
@@ -41,6 +43,29 @@ def get_wikipedia_url(entity, lang="en"):
|
|
41 |
except:
|
42 |
return ""
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# Main combined function
|
45 |
def ner_wikidata_lookup(text):
|
46 |
try:
|
@@ -60,6 +85,8 @@ def ner_wikidata_lookup(text):
|
|
60 |
label, desc, coord, wikidata_url = get_wikidata_info(name, lang=detected_lang)
|
61 |
wiki_url = get_wikipedia_url(name, lang=detected_lang)
|
62 |
|
|
|
|
|
63 |
osm_link = ""
|
64 |
if coord:
|
65 |
try:
|
@@ -74,11 +101,16 @@ def ner_wikidata_lookup(text):
|
|
74 |
if wiki_url:
|
75 |
links += f"<a href='{wiki_url}' target='_blank'>π Wikipedia</a>"
|
76 |
|
|
|
|
|
|
|
77 |
result += f"""
|
78 |
<hr><h3>π {label}</h3>
|
79 |
<p>{desc}</p>
|
80 |
<p>{links}</p>
|
81 |
<p>{osm_link}</p>
|
|
|
|
|
82 |
"""
|
83 |
|
84 |
return result if seen else "No named entities found."
|
@@ -88,8 +120,8 @@ iface = gr.Interface(
|
|
88 |
fn=ner_wikidata_lookup,
|
89 |
inputs=gr.Textbox(lines=4, placeholder="Type any sentence in any language..."),
|
90 |
outputs=gr.HTML(),
|
91 |
-
title="π NER with Wikidata + Wikipedia +
|
92 |
-
description="Detects named entities, retrieves Wikidata descriptions, adds Wikipedia links and
|
93 |
)
|
94 |
|
95 |
if __name__ == "__main__":
|
|
|
3 |
from langdetect import detect
|
4 |
import requests
|
5 |
import wikipedia
|
6 |
+
import re
|
7 |
|
8 |
# Load NER model
|
9 |
ner_pipeline = pipeline("ner", model="Davlan/xlm-roberta-base-ner-hrl", grouped_entities=True)
|
10 |
|
11 |
# Get Wikidata entity info
|
12 |
+
|
13 |
def get_wikidata_info(entity, lang="en"):
|
14 |
query = f'''
|
15 |
SELECT ?item ?itemLabel ?itemDescription ?coordinate WHERE {{
|
|
|
43 |
except:
|
44 |
return ""
|
45 |
|
46 |
+
# Enrich info with tags and intent
|
47 |
+
def enrich_info(summary):
|
48 |
+
related_info = []
|
49 |
+
|
50 |
+
if re.search(r'capital', summary, re.IGNORECASE):
|
51 |
+
related_info.append("ποΈ Capital city")
|
52 |
+
if re.search(r'tourism|attraction', summary, re.IGNORECASE):
|
53 |
+
related_info.append("π§³ Popular for tourism")
|
54 |
+
if re.search(r'population', summary, re.IGNORECASE):
|
55 |
+
related_info.append("π₯ Densely populated")
|
56 |
+
if re.search(r'university|education', summary, re.IGNORECASE):
|
57 |
+
related_info.append("π Educational hub")
|
58 |
+
if re.search(r'beach', summary, re.IGNORECASE):
|
59 |
+
related_info.append("ποΈ Known for beaches")
|
60 |
+
|
61 |
+
intent = "General knowledge inquiry"
|
62 |
+
if re.search(r'tourism|travel', summary, re.IGNORECASE):
|
63 |
+
intent = "Looking for travel guidance"
|
64 |
+
elif re.search(r'university|education', summary, re.IGNORECASE):
|
65 |
+
intent = "Seeking educational info"
|
66 |
+
|
67 |
+
return related_info, intent
|
68 |
+
|
69 |
# Main combined function
|
70 |
def ner_wikidata_lookup(text):
|
71 |
try:
|
|
|
85 |
label, desc, coord, wikidata_url = get_wikidata_info(name, lang=detected_lang)
|
86 |
wiki_url = get_wikipedia_url(name, lang=detected_lang)
|
87 |
|
88 |
+
related_tags, detected_intent = enrich_info(desc)
|
89 |
+
|
90 |
osm_link = ""
|
91 |
if coord:
|
92 |
try:
|
|
|
101 |
if wiki_url:
|
102 |
links += f"<a href='{wiki_url}' target='_blank'>π Wikipedia</a>"
|
103 |
|
104 |
+
tags_html = f"<p><b>Related Tags:</b> {' | '.join(related_tags)}</p>" if related_tags else ""
|
105 |
+
intent_html = f"<p><b>Intent:</b> {detected_intent}</p>"
|
106 |
+
|
107 |
result += f"""
|
108 |
<hr><h3>π {label}</h3>
|
109 |
<p>{desc}</p>
|
110 |
<p>{links}</p>
|
111 |
<p>{osm_link}</p>
|
112 |
+
{tags_html}
|
113 |
+
{intent_html}
|
114 |
"""
|
115 |
|
116 |
return result if seen else "No named entities found."
|
|
|
120 |
fn=ner_wikidata_lookup,
|
121 |
inputs=gr.Textbox(lines=4, placeholder="Type any sentence in any language..."),
|
122 |
outputs=gr.HTML(),
|
123 |
+
title="π NER with Wikidata + Wikipedia + Smart Tags",
|
124 |
+
description="Detects named entities, retrieves Wikidata descriptions, adds Wikipedia links, maps, and enriches output with semantic tags and intent detection."
|
125 |
)
|
126 |
|
127 |
if __name__ == "__main__":
|