yuntian-deng commited on
Commit
1d82bda
·
verified ·
1 Parent(s): 07b94f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -2
app.py CHANGED
@@ -1,13 +1,50 @@
1
  import gradio as gr
2
  import json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  # Load your validation set
5
  #with open('validation_data.json', 'r') as file:
6
  # validation_data = json.load(file)
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def predict(title, authors, abstract):
9
  # Your model prediction logic here
10
- score = 0.5 #model_inference(title, authors, abstract) # Replace this with your actual model inference
11
 
12
  # Calculate precision for scores >= the predicted score
13
  #selected = [d for d in validation_data if d['score'] >= score]
@@ -15,7 +52,7 @@ def predict(title, authors, abstract):
15
  #precision = true_positives / len(selected) if selected else 0
16
  precision = 0.2
17
 
18
- result = f"For papers with a score greater than or equal to {score:.2f}, approximately {precision * 100:.2f}% are selected by AK."
19
 
20
  return score, result
21
 
 
1
  import gradio as gr
2
  import json
3
+ import re
4
+ import torch
5
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
+
7
+ model_name = 'yuntian-deng/ak-paper-selection-deberta'
8
+ max_length = 512
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
+ model.eval()
12
+ if torch.cuda.is_available():
13
+ model.cuda()
14
+
15
+
16
+ def normalize_spaces(text):
17
+ return re.sub(r'\s+', ' ', text).strip()
18
 
19
  # Load your validation set
20
  #with open('validation_data.json', 'r') as file:
21
  # validation_data = json.load(file)
22
 
23
+ def fill_template(title, authors, abstract):
24
+ title = normalize_spaces(x['title'].replace('\n', ' '))
25
+ authors = ', '.join([author.strip() for author in authors.split(',')])
26
+ abstract = normalize_spaces(abstract.replace('\n', ' '))
27
+ text = f"""Title: {title}
28
+ Authors: {authors}
29
+ Abstract: {abstract}"""
30
+ return text
31
+
32
+ @torch.no_grad()
33
+ def model_inference(title, authors, abstract):
34
+ text = fill_template(title, authors, abstract)
35
+ print (text)
36
+ inputs = tokenizer([text], return_tensors="pt", truncation=True, max_length=max_length)
37
+ if torch.cuda.is_available():
38
+ inputs = {key: value.cuda() for key, value in inputs.items()}
39
+ outputs = model(**inputs)
40
+ logits = outputs.logits
41
+ probs = logits.softmax(dim=-1).view(-1)
42
+ score = probs[1].item()
43
+ return score
44
+
45
  def predict(title, authors, abstract):
46
  # Your model prediction logic here
47
+ score = model_inference(title, authors, abstract)
48
 
49
  # Calculate precision for scores >= the predicted score
50
  #selected = [d for d in validation_data if d['score'] >= score]
 
52
  #precision = true_positives / len(selected) if selected else 0
53
  precision = 0.2
54
 
55
+ result = f"Your score: {score:.2f}.\nFor papers with score >= {score:.2f}, {precision * 100:.2f}% are selected by AK."
56
 
57
  return score, result
58