Spaces:
Runtime error
Runtime error
Ahsen Khaliq
commited on
Commit
·
bc39e07
1
Parent(s):
9841cab
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,54 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained('allenai/longformer-scico')
|
6 |
+
model = AutoModelForSequenceClassification.from_pretrained('allenai/longformer-scico')
|
7 |
+
|
8 |
+
start_token = tokenizer.convert_tokens_to_ids("<m>")
|
9 |
+
end_token = tokenizer.convert_tokens_to_ids("</m>")
|
10 |
+
|
11 |
+
def get_global_attention(input_ids):
|
12 |
+
global_attention_mask = torch.zeros(input_ids.shape)
|
13 |
+
global_attention_mask[:, 0] = 1 # global attention to the CLS token
|
14 |
+
start = torch.nonzero(input_ids == start_token) # global attention to the <m> token
|
15 |
+
end = torch.nonzero(input_ids == end_token) # global attention to the </m> token
|
16 |
+
globs = torch.cat((start, end))
|
17 |
+
value = torch.ones(globs.shape[0])
|
18 |
+
global_attention_mask.index_put_(tuple(globs.t()), value)
|
19 |
+
return global_attention_mask
|
20 |
+
|
21 |
+
def inference(m1,m2):
|
22 |
+
b = {}
|
23 |
+
m1 = m1
|
24 |
+
m2 = m2
|
25 |
+
|
26 |
+
inputs = m1 + " </s></s> " + m2
|
27 |
+
|
28 |
+
tokens = tokenizer(inputs, return_tensors='pt')
|
29 |
+
global_attention_mask = get_global_attention(tokens['input_ids'])
|
30 |
+
|
31 |
+
with torch.no_grad():
|
32 |
+
output = model(tokens['input_ids'], tokens['attention_mask'], global_attention_mask)
|
33 |
+
|
34 |
+
scores = torch.softmax(output.logits, dim=-1)
|
35 |
+
listscore = scores.tolist()
|
36 |
+
b['not related'] = listscore[0]
|
37 |
+
b['coref'] = listscore[1]
|
38 |
+
b['parent'] = listscore[2]
|
39 |
+
b['child'] = listscore[3]
|
40 |
+
return b
|
41 |
+
|
42 |
+
title = "Longformer-scico"
|
43 |
+
description = "demo for Anime2Sketch. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
44 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2104.05703'>Adversarial Open Domain Adaption for Sketch-to-Photo Synthesis</a> | <a href='https://github.com/Mukosame/Anime2Sketch'>Github Repo</a></p>"
|
45 |
+
|
46 |
+
gr.Interface(
|
47 |
+
inference,
|
48 |
+
gr.inputs.Textbox(label="Input")],
|
49 |
+
gr.outputs.Label(label="Output"),
|
50 |
+
title=title,
|
51 |
+
description=description,
|
52 |
+
article=article,
|
53 |
+
enable_queue=True
|
54 |
+
).launch(debug=True)
|