Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -34,14 +34,18 @@ def get_wikidata_info(entity, lang="en"):
|
|
34 |
pass
|
35 |
return entity, "No description available.", "", ""
|
36 |
|
37 |
-
# Get
|
38 |
-
|
|
|
39 |
try:
|
40 |
wikipedia.set_lang(lang)
|
41 |
page = wikipedia.page(entity, auto_suggest=True, redirect=True)
|
42 |
-
|
|
|
|
|
|
|
43 |
except:
|
44 |
-
return ""
|
45 |
|
46 |
# Enrich info with tags and intent
|
47 |
def enrich_info(summary):
|
@@ -83,7 +87,7 @@ def ner_wikidata_lookup(text):
|
|
83 |
seen.add(name)
|
84 |
|
85 |
label, desc, coord, wikidata_url = get_wikidata_info(name, lang=detected_lang)
|
86 |
-
wiki_url =
|
87 |
|
88 |
related_tags, detected_intent = enrich_info(desc)
|
89 |
|
@@ -104,6 +108,12 @@ def ner_wikidata_lookup(text):
|
|
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>
|
@@ -111,6 +121,7 @@ def ner_wikidata_lookup(text):
|
|
111 |
<p>{osm_link}</p>
|
112 |
{tags_html}
|
113 |
{intent_html}
|
|
|
114 |
"""
|
115 |
|
116 |
return result if seen else "No named entities found."
|
@@ -121,8 +132,8 @@ iface = gr.Interface(
|
|
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
|
125 |
)
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
-
iface.launch()
|
|
|
34 |
pass
|
35 |
return entity, "No description available.", "", ""
|
36 |
|
37 |
+
# Get Wikipedia details
|
38 |
+
|
39 |
+
def get_wikipedia_details(entity, lang="en"):
|
40 |
try:
|
41 |
wikipedia.set_lang(lang)
|
42 |
page = wikipedia.page(entity, auto_suggest=True, redirect=True)
|
43 |
+
categories = page.categories[:5]
|
44 |
+
links = page.links[:5]
|
45 |
+
url = page.url
|
46 |
+
return url, categories, links
|
47 |
except:
|
48 |
+
return "", [], []
|
49 |
|
50 |
# Enrich info with tags and intent
|
51 |
def enrich_info(summary):
|
|
|
87 |
seen.add(name)
|
88 |
|
89 |
label, desc, coord, wikidata_url = get_wikidata_info(name, lang=detected_lang)
|
90 |
+
wiki_url, wiki_categories, wiki_links = get_wikipedia_details(name, lang=detected_lang)
|
91 |
|
92 |
related_tags, detected_intent = enrich_info(desc)
|
93 |
|
|
|
108 |
tags_html = f"<p><b>Related Tags:</b> {' | '.join(related_tags)}</p>" if related_tags else ""
|
109 |
intent_html = f"<p><b>Intent:</b> {detected_intent}</p>"
|
110 |
|
111 |
+
extra_info = ""
|
112 |
+
if wiki_categories:
|
113 |
+
extra_info += f"<p><b>Wikipedia Categories:</b> {', '.join(wiki_categories)}</p>"
|
114 |
+
if wiki_links:
|
115 |
+
extra_info += f"<p><b>Related Topics:</b> {', '.join(wiki_links)}</p>"
|
116 |
+
|
117 |
result += f"""
|
118 |
<hr><h3>π {label}</h3>
|
119 |
<p>{desc}</p>
|
|
|
121 |
<p>{osm_link}</p>
|
122 |
{tags_html}
|
123 |
{intent_html}
|
124 |
+
{extra_info}
|
125 |
"""
|
126 |
|
127 |
return result if seen else "No named entities found."
|
|
|
132 |
inputs=gr.Textbox(lines=4, placeholder="Type any sentence in any language..."),
|
133 |
outputs=gr.HTML(),
|
134 |
title="π NER with Wikidata + Wikipedia + Smart Tags",
|
135 |
+
description="Detects named entities, retrieves Wikidata descriptions, adds Wikipedia links, maps, and enriches output with semantic tags, intent detection, categories, and related topics."
|
136 |
)
|
137 |
|
138 |
if __name__ == "__main__":
|
139 |
+
iface.launch()
|