Spaces:
Running
on
Zero
Running
on
Zero
241204
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
import torch.nn.functional as F
|
5 |
+
|
6 |
+
model_path = "ssocean/NAIP" # 更换为你的模型路径
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path, num_labels=1, load_in_8bit=True)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
9 |
+
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
model = model.to(device)
|
12 |
+
model.eval()
|
13 |
+
|
14 |
+
def predict(title, abstract):
|
15 |
+
# 将标题和摘要处理为一个单一的字符串
|
16 |
+
text = f"Given a certain paper, Title: {title}\nAbstract: {abstract}.\nPredict its normalized academic impact (between 0 and 1):"
|
17 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
18 |
+
with torch.no_grad():
|
19 |
+
outputs = model(**inputs.to(device))
|
20 |
+
# 应用 Sigmoid 函数来获取概率输出
|
21 |
+
probability = torch.sigmoid(outputs.logits).item()
|
22 |
+
return {"Impact Probability": probability}
|
23 |
+
|
24 |
+
# 创建 Gradio 界面
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=predict,
|
27 |
+
inputs=[gr.inputs.Textbox(lines=2, placeholder="Enter Paper Title Here..."),
|
28 |
+
gr.inputs.Textbox(lines=5, placeholder="Enter Paper Abstract Here...")],
|
29 |
+
outputs=[gr.outputs.Label(num_top_classes=1)],
|
30 |
+
title="Newborn Article Impact Prediction based on LLM",
|
31 |
+
description="Predict the normalized academic impact of a paper based on its title and abstract."
|
32 |
+
)
|
33 |
+
|
34 |
+
iface.launch()
|