Spaces:
Running
Running
try HTML approach
Browse files- interfaces/cap_minor.py +24 -5
interfaces/cap_minor.py
CHANGED
@@ -72,8 +72,30 @@ def predict(text, model_id, tokenizer_id):
|
|
72 |
|
73 |
probs = torch.nn.functional.softmax(logits, dim=1).cpu().numpy().flatten()
|
74 |
output_pred = {f"[{'999' if str(CAP_MIN_NUM_DICT[i]) == '999' else str(CAP_MIN_NUM_DICT[i])[:-2]}]{convert_minor_to_major(CAP_MIN_NUM_DICT[i])} [{CAP_MIN_NUM_DICT[i]}]{CAP_MIN_LABEL_NAMES[CAP_MIN_NUM_DICT[i]]}": probs[i] for i in np.argsort(probs)[::-1]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
output_info = f'<p style="text-align: center; display: block">Prediction was made using the <a href="https://huggingface.co/{model_id}">{model_id}</a> model.</p>'
|
76 |
-
return
|
77 |
|
78 |
def predict_cap(text, language, domain):
|
79 |
domain = domains[domain]
|
@@ -86,13 +108,10 @@ def predict_cap(text, language, domain):
|
|
86 |
|
87 |
return predict(text, model_id, tokenizer_id)
|
88 |
|
89 |
-
css = '''
|
90 |
-
.info {text-align: center; font-size: 3em; !important}
|
91 |
-
'''
|
92 |
demo = gr.Interface(
|
93 |
title="CAP Minor Topics Babel Demo",
|
94 |
fn=predict_cap,
|
95 |
inputs=[gr.Textbox(lines=6, label="Input"),
|
96 |
gr.Dropdown(languages, label="Language"),
|
97 |
gr.Dropdown(domains.keys(), label="Domain")],
|
98 |
-
outputs=[gr.
|
|
|
72 |
|
73 |
probs = torch.nn.functional.softmax(logits, dim=1).cpu().numpy().flatten()
|
74 |
output_pred = {f"[{'999' if str(CAP_MIN_NUM_DICT[i]) == '999' else str(CAP_MIN_NUM_DICT[i])[:-2]}]{convert_minor_to_major(CAP_MIN_NUM_DICT[i])} [{CAP_MIN_NUM_DICT[i]}]{CAP_MIN_LABEL_NAMES[CAP_MIN_NUM_DICT[i]]}": probs[i] for i in np.argsort(probs)[::-1]}
|
75 |
+
|
76 |
+
|
77 |
+
output_pred = dict(sorted(output_pred.items(), key=lambda item: item[1]))
|
78 |
+
first_n_items = take(5, output_pred.items())
|
79 |
+
|
80 |
+
html = ""
|
81 |
+
first = True
|
82 |
+
for label, prob in first_n_items.items():
|
83 |
+
bar_color = "#e0d890" if first else "#ccc"
|
84 |
+
text_color = "black"
|
85 |
+
bar_width = int(prob * 100)
|
86 |
+
|
87 |
+
html += f"""
|
88 |
+
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px;">
|
89 |
+
<span style="color: {text_color};">{label}</span>
|
90 |
+
<span style="color: {text_color};">{int(prob * 100)}%</span>
|
91 |
+
</div>
|
92 |
+
<div style="height: 4px; background-color: {bar_color}; width: {bar_width}%; margin-bottom: 8px;"></div>
|
93 |
+
"""
|
94 |
+
first = False
|
95 |
+
|
96 |
+
|
97 |
output_info = f'<p style="text-align: center; display: block">Prediction was made using the <a href="https://huggingface.co/{model_id}">{model_id}</a> model.</p>'
|
98 |
+
return html, output_info
|
99 |
|
100 |
def predict_cap(text, language, domain):
|
101 |
domain = domains[domain]
|
|
|
108 |
|
109 |
return predict(text, model_id, tokenizer_id)
|
110 |
|
|
|
|
|
|
|
111 |
demo = gr.Interface(
|
112 |
title="CAP Minor Topics Babel Demo",
|
113 |
fn=predict_cap,
|
114 |
inputs=[gr.Textbox(lines=6, label="Input"),
|
115 |
gr.Dropdown(languages, label="Language"),
|
116 |
gr.Dropdown(domains.keys(), label="Domain")],
|
117 |
+
outputs=[gr.HTML(label="Output"), gr.Markdown()])
|