ssocean commited on
Commit
f56954d
·
verified ·
1 Parent(s): 50758b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -7,21 +7,32 @@ import torch.nn as nn
7
  import re
8
  model_path = r'ssocean/NAIP'
9
 
10
- model = AutoModelForSequenceClassification.from_pretrained(model_path, num_labels=1, load_in_8bit=True)
11
-
12
- tokenizer = AutoTokenizer.from_pretrained(model_path)
 
 
 
 
 
 
 
 
 
13
 
 
14
 
15
  @spaces.GPU
16
  def predict(title, abstract):
17
- global model
18
- torch.cuda.set_device(torch.device('cuda:0'))
19
- model.eval()
20
  text = f'''Given a certain paper, Title: {title}\n Abstract: {abstract}. \n Predict its normalized academic impact (between 0 and 1):'''
21
- inputs = tokenizer(text, return_tensors="pt")
22
- inputs = inputs.to(torch.device('cuda:0'))
23
  with torch.no_grad():
24
  outputs = model(**inputs)
 
25
  probability = torch.sigmoid(outputs.logits).item()
26
  # reason for +0.05: We observed that the predicted values in the web demo are generally around 0.05 lower than those in the local deployment (due to differences in software/hardware environments). Therefore, we applied the following compensation in the web demo. Please do not use this in the local deployment.
27
  if probability + 0.05 >=1.0:
 
7
  import re
8
  model_path = r'ssocean/NAIP'
9
 
10
+ @spaces.GPU
11
+ def init_model():
12
+ global model, tokenizer
13
+ model = AutoModelForSequenceClassification.from_pretrained(
14
+ model_path,
15
+ num_labels=1,
16
+ load_in_8bit=True,
17
+ device_map="auto"
18
+ )
19
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
20
+ model.eval()
21
+ return model, tokenizer
22
 
23
+ model, tokenizer = init_model()
24
 
25
  @spaces.GPU
26
  def predict(title, abstract):
27
+ global model, tokenizer
28
+
29
+ model_device = next(model.parameters()).device
30
  text = f'''Given a certain paper, Title: {title}\n Abstract: {abstract}. \n Predict its normalized academic impact (between 0 and 1):'''
31
+ inputs = tokenizer(text, return_tensors="pt").to(model_device)
32
+
33
  with torch.no_grad():
34
  outputs = model(**inputs)
35
+
36
  probability = torch.sigmoid(outputs.logits).item()
37
  # reason for +0.05: We observed that the predicted values in the web demo are generally around 0.05 lower than those in the local deployment (due to differences in software/hardware environments). Therefore, we applied the following compensation in the web demo. Please do not use this in the local deployment.
38
  if probability + 0.05 >=1.0: