Spaces:
Sleeping
Sleeping
kapllan
commited on
Commit
•
af0a76f
1
Parent(s):
1cdf555
Bugfix.
Browse files- app.py +66 -8
- topics_hierarchy.json +273 -0
app.py
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
-
import json as js
|
2 |
-
import os
|
3 |
-
import re
|
4 |
-
from typing import List
|
5 |
-
|
6 |
import fasttext
|
7 |
import gradio as gr
|
8 |
import joblib
|
|
|
9 |
import omikuji
|
|
|
|
|
|
|
10 |
from huggingface_hub import snapshot_download
|
|
|
|
|
11 |
from install_packages import download_model
|
12 |
|
13 |
download_model('https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin', 'lid.176.bin')
|
@@ -24,6 +25,9 @@ lang_model = fasttext.load_model('lid.176.bin')
|
|
24 |
with open('./id2label.json', 'r') as f:
|
25 |
id2label = js.load(f)
|
26 |
|
|
|
|
|
|
|
27 |
|
28 |
def map_language(language: str) -> str:
|
29 |
language_mapping = {'de': 'German',
|
@@ -70,6 +74,56 @@ def predict_topic(text: str) -> [List[str], str]:
|
|
70 |
return results, language
|
71 |
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
def topic_modeling(text: str, threshold: float) -> [List[str], str]:
|
74 |
# Prepare labels and scores for the plot
|
75 |
sorted_topics, language = predict_topic(text)
|
@@ -77,6 +131,8 @@ def topic_modeling(text: str, threshold: float) -> [List[str], str]:
|
|
77 |
sorted_topics = [t for t in sorted_topics if t[1] >= threshold]
|
78 |
else:
|
79 |
sorted_topics = []
|
|
|
|
|
80 |
return sorted_topics, language
|
81 |
|
82 |
|
@@ -88,13 +144,15 @@ with gr.Blocks() as iface:
|
|
88 |
with gr.Column():
|
89 |
input_text = gr.Textbox(lines=10, placeholder="Enter a document")
|
90 |
submit_button = gr.Button("Submit")
|
91 |
-
threshold_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Score Threshold",
|
|
|
92 |
language_text = gr.Textbox(lines=1, placeholder="Detected language will be shown here...",
|
93 |
interactive=False, label="Detected Language")
|
94 |
with gr.Column():
|
95 |
-
output_data = gr.
|
96 |
|
97 |
-
submit_button.click(topic_modeling, inputs=[input_text, threshold_slider],
|
|
|
98 |
|
99 |
# Launch the app
|
100 |
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import fasttext
|
2 |
import gradio as gr
|
3 |
import joblib
|
4 |
+
import json as js
|
5 |
import omikuji
|
6 |
+
import os
|
7 |
+
import re
|
8 |
+
from collections import defaultdict
|
9 |
from huggingface_hub import snapshot_download
|
10 |
+
from typing import List, Tuple, Dict
|
11 |
+
|
12 |
from install_packages import download_model
|
13 |
|
14 |
download_model('https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin', 'lid.176.bin')
|
|
|
25 |
with open('./id2label.json', 'r') as f:
|
26 |
id2label = js.load(f)
|
27 |
|
28 |
+
with open('topics_hierarchy.json', 'r') as f:
|
29 |
+
topics_hierarchy = js.load(f)
|
30 |
+
|
31 |
|
32 |
def map_language(language: str) -> str:
|
33 |
language_mapping = {'de': 'German',
|
|
|
74 |
return results, language
|
75 |
|
76 |
|
77 |
+
def get_row_color(type: str):
|
78 |
+
if 'main' in type.lower():
|
79 |
+
return 'background-color: darkgrey;'
|
80 |
+
if 'sub' in type.lower():
|
81 |
+
return 'background-color: lightgrey;'
|
82 |
+
|
83 |
+
|
84 |
+
def generate_html_table(topics: List[Tuple[str, str, float]]):
|
85 |
+
html = '<table style="width:100%; border: 1px solid black; border-collapse: collapse;">'
|
86 |
+
|
87 |
+
html += '<tr><th>Type</th><th>Topic</th><th>Score</th></tr>'
|
88 |
+
for type, topic, score in topics:
|
89 |
+
color = get_row_color(type)
|
90 |
+
topic = f"<strong>{topic}</strong>" if 'main' in type.lower() else topic
|
91 |
+
type = f"<strong>{type}</strong>" if 'main' in type.lower() else type
|
92 |
+
score = f"<strong>{score}</strong>" if 'main' in type.lower() else score
|
93 |
+
html += f'<tr style="{color}"><td>{type}</td><td>{topic}</td><td>{score}</td></tr>'
|
94 |
+
html += '</table>'
|
95 |
+
return html
|
96 |
+
|
97 |
+
|
98 |
+
def restructure_topics(topics: List[Tuple[str, float]]) -> List[Tuple[str, str, float]]:
|
99 |
+
topics = [(str(x[0]).lower(), x[1]) for x in topics]
|
100 |
+
topics_as_dict = {}
|
101 |
+
for predicted_topic, score in topics:
|
102 |
+
if str(predicted_topic).lower() in topics_hierarchy.keys():
|
103 |
+
topics_as_dict[str(predicted_topic).lower()] = []
|
104 |
+
|
105 |
+
for predicted_topic, score in topics:
|
106 |
+
for main_topic, sub_topics in topics_hierarchy.items():
|
107 |
+
if main_topic in topics_as_dict.keys() and predicted_topic != main_topic and predicted_topic in sub_topics:
|
108 |
+
topics_as_dict[main_topic].append(predicted_topic)
|
109 |
+
|
110 |
+
topics_restructured = []
|
111 |
+
for predicted_main_topic, predicted_sub_topics in topics_as_dict.items():
|
112 |
+
if len(predicted_sub_topics) > 0:
|
113 |
+
score = [t for t in topics if t[0] == predicted_main_topic][0][1]
|
114 |
+
topics_restructured.append(
|
115 |
+
('Main Topic', predicted_main_topic, score))
|
116 |
+
predicted_sub_topics_with_scores = []
|
117 |
+
for pst in predicted_sub_topics:
|
118 |
+
score = [t for t in topics if t[0] == pst][0][1]
|
119 |
+
entry = ('Sub Topic', pst, score)
|
120 |
+
if entry not in predicted_sub_topics_with_scores:
|
121 |
+
predicted_sub_topics_with_scores.append(entry)
|
122 |
+
for x in predicted_sub_topics_with_scores:
|
123 |
+
topics_restructured.append(x)
|
124 |
+
return topics_restructured
|
125 |
+
|
126 |
+
|
127 |
def topic_modeling(text: str, threshold: float) -> [List[str], str]:
|
128 |
# Prepare labels and scores for the plot
|
129 |
sorted_topics, language = predict_topic(text)
|
|
|
131 |
sorted_topics = [t for t in sorted_topics if t[1] >= threshold]
|
132 |
else:
|
133 |
sorted_topics = []
|
134 |
+
sorted_topics = restructure_topics(sorted_topics)
|
135 |
+
sorted_topics = generate_html_table(sorted_topics)
|
136 |
return sorted_topics, language
|
137 |
|
138 |
|
|
|
144 |
with gr.Column():
|
145 |
input_text = gr.Textbox(lines=10, placeholder="Enter a document")
|
146 |
submit_button = gr.Button("Submit")
|
147 |
+
threshold_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Score Threshold",
|
148 |
+
value=0.0)
|
149 |
language_text = gr.Textbox(lines=1, placeholder="Detected language will be shown here...",
|
150 |
interactive=False, label="Detected Language")
|
151 |
with gr.Column():
|
152 |
+
output_data = gr.HTML()
|
153 |
|
154 |
+
submit_button.click(topic_modeling, inputs=[input_text, threshold_slider],
|
155 |
+
outputs=[output_data, language_text])
|
156 |
|
157 |
# Launch the app
|
158 |
iface.launch(share=True)
|
topics_hierarchy.json
ADDED
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"staatspolitik": [
|
3 |
+
"exekutive",
|
4 |
+
"politisches leben",
|
5 |
+
"öffentliche verwaltung",
|
6 |
+
"politische rechte",
|
7 |
+
"politisches system",
|
8 |
+
"demokratie",
|
9 |
+
"föderalismus",
|
10 |
+
"politische partei",
|
11 |
+
"bürgerrecht",
|
12 |
+
"verfassung",
|
13 |
+
"vorrechte und immunität",
|
14 |
+
"volksabstimmung",
|
15 |
+
"wahlen",
|
16 |
+
"staat",
|
17 |
+
"vergaberecht",
|
18 |
+
"auslandschweizer",
|
19 |
+
"service public"
|
20 |
+
],
|
21 |
+
"geschichte": [
|
22 |
+
"geschichte schweiz",
|
23 |
+
"geschichte ausland"
|
24 |
+
],
|
25 |
+
"parlament": [
|
26 |
+
"parlament schweiz",
|
27 |
+
"parlament ausland",
|
28 |
+
"ratsmitglied",
|
29 |
+
"parlamentsgeschichte",
|
30 |
+
"kantonales parlament"
|
31 |
+
],
|
32 |
+
"internationale politik": [
|
33 |
+
"aussenpolitik : schweiz",
|
34 |
+
"aussenpolitik : ausland",
|
35 |
+
"diplomatie",
|
36 |
+
"internationale beziehungen",
|
37 |
+
"internationales abkommen",
|
38 |
+
"internationale politik",
|
39 |
+
"interventionspolitik",
|
40 |
+
"globalisierung",
|
41 |
+
"grenze",
|
42 |
+
"internationale organisation",
|
43 |
+
"internationale zusammenarbeit"
|
44 |
+
],
|
45 |
+
"sicherheitspolitik": [
|
46 |
+
"armee",
|
47 |
+
"bewaffnung",
|
48 |
+
"innere sicherheit",
|
49 |
+
"zivilschutz und bevölkerungsschutz/zivildienst",
|
50 |
+
"terrorismus",
|
51 |
+
"sicherheitspolitik/friedenspolitik",
|
52 |
+
"krieg",
|
53 |
+
"internationaler konflikt"
|
54 |
+
],
|
55 |
+
"europapolitik": [
|
56 |
+
"europapolitik",
|
57 |
+
"beziehung schweiz - eu",
|
58 |
+
"europarecht",
|
59 |
+
"europäische union",
|
60 |
+
"europarat",
|
61 |
+
"europäisches parlament"
|
62 |
+
],
|
63 |
+
"recht allgemein": [
|
64 |
+
"verfahrensrecht",
|
65 |
+
"urheberrecht",
|
66 |
+
"rechtswissenschaft",
|
67 |
+
"verwaltungsrecht",
|
68 |
+
"beschwerderecht"
|
69 |
+
],
|
70 |
+
"zivilrecht": [
|
71 |
+
"zivilrecht",
|
72 |
+
"zivilprozessordnung",
|
73 |
+
"familienrecht",
|
74 |
+
"kinderrechte",
|
75 |
+
"kindes- und erwachsenenschutzrecht",
|
76 |
+
"personenrecht",
|
77 |
+
"erbrecht",
|
78 |
+
"erberecht",
|
79 |
+
"sachenrecht",
|
80 |
+
"obligationenrecht",
|
81 |
+
"konkursrecht",
|
82 |
+
"vertrag"
|
83 |
+
],
|
84 |
+
"strafrecht": [
|
85 |
+
"strafrecht",
|
86 |
+
"strafprozessordnung",
|
87 |
+
"straftat",
|
88 |
+
"korruption"
|
89 |
+
],
|
90 |
+
"gerichtswesen": [
|
91 |
+
"gerichtswesen",
|
92 |
+
"schiedsgerichtsbarkeit"
|
93 |
+
],
|
94 |
+
"rechte und freiheiten": [
|
95 |
+
"grundrechte",
|
96 |
+
"datenschutz",
|
97 |
+
"rassismus",
|
98 |
+
"diskriminierung"
|
99 |
+
],
|
100 |
+
"internationales recht": [
|
101 |
+
"internationales humanitäres recht",
|
102 |
+
"internationale strafjustiz",
|
103 |
+
"internationales privatrecht",
|
104 |
+
"staatssouveränität",
|
105 |
+
"internationale rechtshilfe",
|
106 |
+
"internationales recht",
|
107 |
+
"menschenrechte"
|
108 |
+
],
|
109 |
+
"wirtschaft": [
|
110 |
+
"wirtschaftspolitik",
|
111 |
+
"konsum",
|
112 |
+
"tourismus",
|
113 |
+
"wirtschaftsleben",
|
114 |
+
"unternehmen",
|
115 |
+
"wettbewerb",
|
116 |
+
"aussenwirtschaftspolitik",
|
117 |
+
"handel",
|
118 |
+
"privatversicherung",
|
119 |
+
"produktion",
|
120 |
+
"stiftung"
|
121 |
+
],
|
122 |
+
"finanzwesen": [
|
123 |
+
"öffentliche finanzen und haushaltspolitik",
|
124 |
+
"öffentliche finanzen",
|
125 |
+
"finanzausgleich",
|
126 |
+
"zoll",
|
127 |
+
"finanzmarkt",
|
128 |
+
"finanzplatz"
|
129 |
+
],
|
130 |
+
"finanzplatz": [
|
131 |
+
"finanzplatz",
|
132 |
+
"geldwäscherei",
|
133 |
+
"nationalbank",
|
134 |
+
"geld- und währungspolitik",
|
135 |
+
"finanzmarkt",
|
136 |
+
"kapital",
|
137 |
+
"finanzrecht"
|
138 |
+
],
|
139 |
+
"steuer": [
|
140 |
+
"steuer",
|
141 |
+
"abgabe",
|
142 |
+
"steuerwettbewerb",
|
143 |
+
"steuerhinterziehung"
|
144 |
+
],
|
145 |
+
"soziale fragen": [
|
146 |
+
"geschlechterfragen",
|
147 |
+
"sport",
|
148 |
+
"familienfragen",
|
149 |
+
"kinder- und jugendfragen",
|
150 |
+
"armut / ungleichheit",
|
151 |
+
"armut/ungleichheit",
|
152 |
+
"altersfragen",
|
153 |
+
"gesellschaftsfragen",
|
154 |
+
"gewalt",
|
155 |
+
"behinderung",
|
156 |
+
"spiel"
|
157 |
+
],
|
158 |
+
"migration": [
|
159 |
+
"asylpolitik",
|
160 |
+
"flüchtling",
|
161 |
+
"ausländerpolitik",
|
162 |
+
"sans-papiers",
|
163 |
+
"migrationsbewegung"
|
164 |
+
],
|
165 |
+
"kultur": [
|
166 |
+
"kultur",
|
167 |
+
"sprache",
|
168 |
+
"religionsfragen"
|
169 |
+
],
|
170 |
+
"sozialer schutz": [
|
171 |
+
"sozialversicherung",
|
172 |
+
"invalidenversicherung",
|
173 |
+
"berufliche vorsorge",
|
174 |
+
"unfallversicherung",
|
175 |
+
"arbeitslosenversicherung",
|
176 |
+
"mutterschaftsversicherung",
|
177 |
+
"erwerbsersatzordnung",
|
178 |
+
"militärversicherung",
|
179 |
+
"ahv",
|
180 |
+
"familienzulage",
|
181 |
+
"sozialhilfe",
|
182 |
+
"sozialpolitik"
|
183 |
+
],
|
184 |
+
"gesundheit": [
|
185 |
+
"gesundheit",
|
186 |
+
"krankenversicherung",
|
187 |
+
"patient/in",
|
188 |
+
"patient",
|
189 |
+
"pflege",
|
190 |
+
"spital",
|
191 |
+
"medizinalberuf",
|
192 |
+
"sucht",
|
193 |
+
"sterben und tod",
|
194 |
+
"gesundheitspolitik",
|
195 |
+
"heil- und hilfsmittel",
|
196 |
+
"ernährung",
|
197 |
+
"fortpflanzung"
|
198 |
+
],
|
199 |
+
"raumplanung und wohnungswesen": [
|
200 |
+
"miet- und wohnungswesen",
|
201 |
+
"raumplanung",
|
202 |
+
"bauwesen/immobilien"
|
203 |
+
],
|
204 |
+
"bildung": [
|
205 |
+
"schule",
|
206 |
+
"universität, hochschule, fachhochschule",
|
207 |
+
"universität/hochschule/fachhochschule",
|
208 |
+
"weiterbildung",
|
209 |
+
"berufsbildung",
|
210 |
+
"bildung"
|
211 |
+
],
|
212 |
+
"medien und kommunikation": [
|
213 |
+
"presse",
|
214 |
+
"radio und fernsehen",
|
215 |
+
"internet und soziale medien",
|
216 |
+
"telefonie",
|
217 |
+
"medien",
|
218 |
+
"post",
|
219 |
+
"informatik",
|
220 |
+
"information",
|
221 |
+
"digitalisierung"
|
222 |
+
],
|
223 |
+
"wissenschaft und forschung": [
|
224 |
+
"forschung",
|
225 |
+
"gentechnologie"
|
226 |
+
],
|
227 |
+
"beschäftigung und arbeit": [
|
228 |
+
"arbeitsmarkt",
|
229 |
+
"gewerkschaft",
|
230 |
+
"arbeitslosigkeit",
|
231 |
+
"arbeit"
|
232 |
+
],
|
233 |
+
"verkehr": [
|
234 |
+
"strassenverkehr",
|
235 |
+
"schienenverkehr",
|
236 |
+
"luftfahrt",
|
237 |
+
"schifffahrt",
|
238 |
+
"güterverkehr",
|
239 |
+
"öffentlicher verkehr",
|
240 |
+
"verkehrspolitik"
|
241 |
+
],
|
242 |
+
"umwelt": [
|
243 |
+
"umweltpolitik",
|
244 |
+
"umweltschutz",
|
245 |
+
"artenvielfalt",
|
246 |
+
"katastrophe",
|
247 |
+
"klimafragen",
|
248 |
+
"abfall",
|
249 |
+
"wasser",
|
250 |
+
"luft",
|
251 |
+
"lärm",
|
252 |
+
"wald",
|
253 |
+
"berg",
|
254 |
+
"boden",
|
255 |
+
"tierschutz"
|
256 |
+
],
|
257 |
+
"tierwelt": [
|
258 |
+
"tierschutz",
|
259 |
+
"tierversuch",
|
260 |
+
"jagd und fischerei"
|
261 |
+
],
|
262 |
+
"landwirtschaft": [
|
263 |
+
"agrarpolitik",
|
264 |
+
"agrarproduktion"
|
265 |
+
],
|
266 |
+
"energie": [
|
267 |
+
"erneuerbare energie",
|
268 |
+
"kernenergie",
|
269 |
+
"fossile energie",
|
270 |
+
"elektrizität",
|
271 |
+
"energiepolitik"
|
272 |
+
]
|
273 |
+
}
|