Spaces:
Running
Running
Add illframes to the demo
Browse files- interfaces/illframes.py +75 -0
interfaces/illframes.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
from transformers import AutoModelForSequenceClassification
|
8 |
+
from transformers import AutoTokenizer
|
9 |
+
from huggingface_hub import HfApi
|
10 |
+
|
11 |
+
from label_dicts import ILLFRAMES_MIGRATION_LABEL_NAMES, ILLFRAMES_COVID_LABEL_NAMES
|
12 |
+
|
13 |
+
HF_TOKEN = os.environ["hf_read"]
|
14 |
+
|
15 |
+
languages = [
|
16 |
+
"English"
|
17 |
+
]
|
18 |
+
|
19 |
+
domains = {
|
20 |
+
"Covid": "covid",
|
21 |
+
"Migration": "migration"
|
22 |
+
}
|
23 |
+
|
24 |
+
def check_huggingface_path(checkpoint_path: str):
|
25 |
+
try:
|
26 |
+
hf_api = HfApi(token=HF_TOKEN)
|
27 |
+
hf_api.model_info(checkpoint_path, token=HF_TOKEN)
|
28 |
+
return True
|
29 |
+
except:
|
30 |
+
return False
|
31 |
+
|
32 |
+
def build_huggingface_path(domain: str):
|
33 |
+
return f"poltextlab/xlm-roberta-large-english-ILLFRAMES-{domain}"
|
34 |
+
|
35 |
+
def predict(text, model_id, tokenizer_id, label_names):
|
36 |
+
device = torch.device("cpu")
|
37 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_id, low_cpu_mem_usage=True, device_map="auto", offload_folder="offload", token=HF_TOKEN)
|
38 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
|
39 |
+
|
40 |
+
inputs = tokenizer(text,
|
41 |
+
max_length=256,
|
42 |
+
truncation=True,
|
43 |
+
padding="do_not_pad",
|
44 |
+
return_tensors="pt").to(device)
|
45 |
+
model.eval()
|
46 |
+
|
47 |
+
with torch.no_grad():
|
48 |
+
logits = model(**inputs).logits
|
49 |
+
|
50 |
+
probs = torch.nn.functional.softmax(logits, dim=1).cpu().numpy().flatten()
|
51 |
+
|
52 |
+
NUMS_DICT = {i: key for i, key in enumerate(sorted(label_names.keys()))}
|
53 |
+
|
54 |
+
output_pred = {f"[{NUMS_DICT[i]}] {label_names[NUMS_DICT[i]]}": probs[i] for i in np.argsort(probs)[::-1]}
|
55 |
+
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>'
|
56 |
+
return output_pred, output_info
|
57 |
+
|
58 |
+
def predict_illframes(text, domain):
|
59 |
+
domain = domains[domain]
|
60 |
+
model_id = build_huggingface_path(domain)
|
61 |
+
tokenizer_id = "xlm-roberta-large"
|
62 |
+
|
63 |
+
if domain == "migration":
|
64 |
+
label_names = ILLFRAMES_MIGRATION_LABEL_NAMES
|
65 |
+
else:
|
66 |
+
label_names = ILLFRAMES_COVID_LABEL_NAMES
|
67 |
+
|
68 |
+
return predict(text, model_id, tokenizer_id, label_names)
|
69 |
+
|
70 |
+
demo = gr.Interface(
|
71 |
+
fn=predict_cap,
|
72 |
+
inputs=[gr.Textbox(lines=6, label="Input"),
|
73 |
+
gr.Dropdown(languages, label="Language"),
|
74 |
+
gr.Dropdown(domains.keys(), label="Domain")],
|
75 |
+
outputs=[gr.Label(num_top_classes=5, label="Output"), gr.Markdown()])
|