File size: 14,327 Bytes
d94f82b
 
 
a00a5a2
e12f2d0
 
d94f82b
3544bdd
a12abfd
 
3544bdd
3643f99
 
 
822a9a7
 
d94f82b
 
 
 
3643f99
 
 
3544bdd
822a9a7
3643f99
822a9a7
3544bdd
822a9a7
a12abfd
a00a5a2
822a9a7
a12abfd
 
a00a5a2
822a9a7
 
 
a00a5a2
 
 
 
3643f99
a00a5a2
 
822a9a7
 
 
a12abfd
3643f99
822a9a7
 
 
3643f99
a00a5a2
 
 
 
3643f99
a00a5a2
3544bdd
a00a5a2
 
 
 
3643f99
a00a5a2
3544bdd
3643f99
 
 
d94f82b
 
822a9a7
3643f99
822a9a7
d94f82b
 
3643f99
a00a5a2
3643f99
12c85f1
3643f99
 
 
12c85f1
3643f99
a00a5a2
3643f99
 
a00a5a2
12c85f1
 
 
a00a5a2
 
3643f99
 
a00a5a2
3643f99
 
822a9a7
3643f99
 
 
a00a5a2
fd896e6
822a9a7
 
fd896e6
1a19be7
a00a5a2
 
 
 
 
3643f99
 
822a9a7
3643f99
a00a5a2
 
3643f99
 
d94f82b
3643f99
 
 
d94f82b
12c85f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d94f82b
 
3643f99
 
 
d94f82b
822a9a7
3643f99
 
822a9a7
 
8cbcecb
822a9a7
8cbcecb
822a9a7
 
 
 
 
 
d94f82b
 
3643f99
822a9a7
d94f82b
822a9a7
 
d94f82b
3643f99
 
 
3544bdd
822a9a7
3643f99
822a9a7
3544bdd
e12f2d0
3643f99
 
 
 
3544bdd
3643f99
 
 
3544bdd
822a9a7
8cbcecb
3643f99
 
 
 
a00a5a2
8cbcecb
3643f99
 
8cbcecb
 
3643f99
 
8cbcecb
 
3643f99
8cbcecb
 
3643f99
 
8cbcecb
 
3643f99
8cbcecb
3544bdd
1a19be7
3643f99
 
 
822a9a7
 
 
 
 
 
 
 
 
 
 
 
 
3643f99
822a9a7
 
 
 
 
 
 
 
 
3643f99
822a9a7
 
 
 
 
3643f99
822a9a7
 
 
 
 
 
 
 
 
 
 
3643f99
822a9a7
 
 
3643f99
 
 
3544bdd
3643f99
 
89d4a33
d94f82b
3544bdd
3643f99
e12f2d0
 
 
3643f99
 
e12f2d0
 
12c85f1
 
 
 
 
 
 
 
3643f99
822a9a7
3643f99
d94f82b
 
3643f99
e12f2d0
3334620
3643f99
d94f82b
3643f99
e12f2d0
3334620
3643f99
 
822a9a7
3544bdd
3643f99
 
3544bdd
12c85f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3643f99
 
 
d94f82b
3643f99
 
822a9a7
3643f99
 
 
 
d94f82b
3643f99
d94f82b
3643f99
 
 
d94f82b
1a19be7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import gradio as gr
import spaces
import torch
from transformers import AutoConfig, AutoTokenizer, AutoModelForSequenceClassification
import torch.nn.functional as F
import torch.nn as nn
import re
import requests
from urllib.parse import urlparse
import xml.etree.ElementTree as ET

##################################################
# Global setup
##################################################
model_path = "ssocean/NAIP"
device = "cuda" if torch.cuda.is_available() else "cpu"

model = None
tokenizer = None

##################################################
# Fetch paper info from arXiv
##################################################
def fetch_arxiv_paper(arxiv_input):
    """
    Fetch paper title & abstract from an arXiv URL or ID.
    """
    try:
        if "arxiv.org" in arxiv_input:
            parsed = urlparse(arxiv_input)
            path = parsed.path
            arxiv_id = path.split("/")[-1].replace(".pdf", "")
        else:
            arxiv_id = arxiv_input.strip()

        api_url = f"http://export.arxiv.org/api/query?id_list={arxiv_id}"
        resp = requests.get(api_url)
        if resp.status_code != 200:
            return {
                "title": "",
                "abstract": "",
                "success": False,
                "message": "Error fetching paper from arXiv API",
            }

        root = ET.fromstring(resp.text)
        ns = {"arxiv": "http://www.w3.org/2005/Atom"}
        entry = root.find(".//arxiv:entry", ns)
        if entry is None:
            return {"title": "", "abstract": "", "success": False, "message": "Paper not found"}

        title = entry.find("arxiv:title", ns).text.strip()
        abstract = entry.find("arxiv:summary", ns).text.strip()

        return {
            "title": title,
            "abstract": abstract,
            "success": True,
            "message": "Paper fetched successfully!",
        }
    except Exception as e:
        return {
            "title": "",
            "abstract": "",
            "success": False,
            "message": f"Error fetching paper: {e}",
        }

##################################################
# Prediction function
##################################################
@spaces.GPU(duration=60, enable_queue=True)
def predict(title, abstract):
    """
    Predict a normalized academic impact score (0–1) from title & abstract.
    """
    global model, tokenizer
    if model is None:
        # 1) Load config
        config = AutoConfig.from_pretrained(model_path)
        
        # 2) Remove quantization_config if it exists (avoid NoneType error in PEFT)
        if hasattr(config, "quantization_config"):
            del config.quantization_config

        # 3) Optionally set number of labels
        config.num_labels = 1

        # 4) Load the model
        model_loaded = AutoModelForSequenceClassification.from_pretrained(
            model_path,
            config=config,
            torch_dtype=torch.float32,  # float32 for stable cublasLt
            device_map=None,
            low_cpu_mem_usage=False
        )
        model_loaded.to(device)
        model_loaded.eval()

        # 5) Load tokenizer
        tokenizer_loaded = AutoTokenizer.from_pretrained(model_path)

        # Assign to globals
        model, tokenizer = model_loaded, tokenizer_loaded

    text = (
        f"Given a certain paper,\n"
        f"Title: {title.strip()}\n"
        f"Abstract: {abstract.strip()}\n"
        f"Predict its normalized academic impact (0~1):"
    )

    try:
        inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=1024)
        inputs = {k: v.to(device) for k, v in inputs.items()}
        with torch.no_grad():
            outputs = model(**inputs)
        logits = outputs.logits
        prob = torch.sigmoid(logits).item()
        score = min(1.0, prob + 0.05)
        return round(score, 4)
    except Exception as e:
        print("Prediction error:", e)
        return 0.0

##################################################
# Grading
##################################################
def get_grade_and_emoji(score):
    """Map a 0–1 score to an A/B/C style grade with an emoji indicator."""
    if score >= 0.900:
        return "AAA 🌟"
    if score >= 0.800:
        return "AA ⭐"
    if score >= 0.650:
        return "A ✨"
    if score >= 0.600:
        return "BBB πŸ”΅"
    if score >= 0.550:
        return "BB πŸ“˜"
    if score >= 0.500:
        return "B πŸ“–"
    if score >= 0.400:
        return "CCC πŸ“"
    if score >= 0.300:
        return "CC ✏️"
    return "C πŸ“‘"

##################################################
# Validation
##################################################
def validate_input(title, abstract):
    """
    Ensure the title has at least 3 words, the abstract at least 50,
    and check for ASCII-only characters.
    """
    non_ascii = re.compile(r"[^\x00-\x7F]")
    if len(title.split()) < 3:
        return False, "Title must be at least 3 words."
    if len(abstract.split()) < 50:
        return False, "Abstract must be at least 50 words."
    if non_ascii.search(title):
        return False, "Title contains non-ASCII characters."
    if non_ascii.search(abstract):
        return False, "Abstract contains non-ASCII characters."
    return True, "Inputs look good."

def update_button_status(title, abstract):
    """Enable or disable the predict button based on validation."""
    valid, msg = validate_input(title, abstract)
    if not valid:
        return gr.update(value="Error: " + msg), gr.update(interactive=False)
    return gr.update(value=msg), gr.update(interactive=True)

##################################################
# Process arXiv input
##################################################
def process_arxiv_input(arxiv_input):
    """
    Called when user clicks 'Fetch Paper Details' to fill in title/abstract from arXiv.
    """
    if not arxiv_input.strip():
        return "", "", "Please enter an arXiv URL or ID"
    res = fetch_arxiv_paper(arxiv_input)
    if res["success"]:
        return res["title"], res["abstract"], res["message"]
    return "", "", res["message"]

##################################################
# Custom CSS
##################################################
css = """
.gradio-container { font-family: Arial, sans-serif; }
.main-title {
    text-align: center; color: #2563eb; font-size: 2.5rem!important;
    margin-bottom:1rem!important;
    background: linear-gradient(45deg,#2563eb,#1d4ed8);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent;
}
.input-section {
    background:#fff; padding:1.5rem; border-radius:0.5rem;
    box-shadow:0 4px 6px rgba(0,0,0,0.1);
}
.result-section {
    background:#f7f9fc; padding:1.5rem; border-radius:0.5rem;
    margin-top:2rem;
}
.grade-display {
    font-size:2.5rem; text-align:center; margin-top:1rem;
}
.arxiv-input {
    margin-bottom:1.5rem; padding:1rem; background:#f3f4f6;
    border-radius:0.5rem;
}
.arxiv-link {
    color:#2563eb; text-decoration: underline;
}
"""

##################################################
# Example Papers
##################################################
example_papers = [
    {
        "title": "Attention Is All You Need",
        "abstract": (
            "The dominant sequence transduction models are based on complex recurrent or "
            "convolutional neural networks that include an encoder and a decoder. The best performing "
            "models also connect the encoder and decoder through an attention mechanism. We propose a "
            "new simple network architecture, the Transformer, based solely on attention mechanisms, "
            "dispensing with recurrence and convolutions entirely. Experiments on two machine "
            "translation tasks show these models to be superior in quality while being more "
            "parallelizable and requiring significantly less time to train."
        ),
        "score": 0.982,
        "note": "Revolutionary paper that introduced the Transformer architecture."
    },
    {
        "title": "Language Models are Few-Shot Learners",
        "abstract": (
            "Recent work has demonstrated substantial gains on many NLP tasks and benchmarks by "
            "pre-training on a large corpus of text followed by fine-tuning on a specific task. While "
            "typically task-agnostic in architecture, this method still requires task-specific "
            "fine-tuning datasets of thousands or tens of thousands of examples. By contrast, humans "
            "can generally perform a new language task from only a few examples or from simple "
            "instructionsβ€”something which current NLP systems still largely struggle to do. Here we "
            "show that scaling up language models greatly improves task-agnostic, few-shot "
            "performance, sometimes even reaching competitiveness with prior state-of-the-art "
            "fine-tuning approaches."
        ),
        "score": 0.956,
        "note": "Groundbreaking GPT-3 paper on few-shot learning."
    },
    {
        "title": "An Empirical Study of Neural Network Training Protocols",
        "abstract": (
            "This paper presents a comparative analysis of different training protocols for neural "
            "networks across various architectures. We examine the effects of learning rate schedules, "
            "batch size selection, and optimization algorithms on model convergence and final "
            "performance. Our experiments span multiple datasets and model sizes, providing practical "
            "insights for deep learning practitioners."
        ),
        "score": 0.623,
        "note": "Solid empirical comparison of training protocols."
    }
]

##################################################
# Build the Gradio Interface
##################################################
with gr.Blocks(theme=gr.themes.Default(), css=css) as iface:
    gr.Markdown("<div class='main-title'>Papers Impact: AI-Powered Research Impact Predictor</div>")
    gr.Markdown("**Predict the potential research impact (0–1) from title & abstract.**")

    with gr.Row():
        with gr.Column(elem_classes="input-section"):
            gr.Markdown("### Import from arXiv")
            with gr.Group(elem_classes="arxiv-input"):
                arxiv_input = gr.Textbox(
                    lines=1,
                    placeholder="e.g. 2504.11651",
                    label="arXiv URL or ID",
                    value="2504.11651"
                )
                gr.Markdown(
                    """
                    <p>
                      Enter an arXiv ID or URL. For example: 
                      <code>2504.11651</code> or <code>https://arxiv.org/pdf/2504.11651</code>
                    </p>
                    """
                )
                fetch_btn = gr.Button("πŸ” Fetch Paper Details", variant="secondary")

            gr.Markdown("### Or Enter Manually")
            title_input = gr.Textbox(
                lines=2,
                placeholder="Paper title (β‰₯3 words)...",
                label="Paper Title"
            )
            abs_input = gr.Textbox(
                lines=5,
                placeholder="Paper abstract (β‰₯50 words)...",
                label="Paper Abstract"
            )
            status_box = gr.Textbox(label="Validation Status", interactive=False)
            predict_btn = gr.Button("🎯 Predict Impact", interactive=False, variant="primary")

        with gr.Column(elem_classes="result-section"):
            score_box = gr.Number(label="Impact Score")
            grade_box = gr.Textbox(label="Grade", elem_classes="grade-display")

    ############## METHODOLOGY EXPLANATION ##############
    gr.Markdown(
        """
        ### Scientific Methodology
        - **Training Data**: Model trained on an extensive dataset of published papers in CS.CV, CS.CL, CS.AI
        - **Optimization**: NDCG optimization with Sigmoid activation and MSE loss
        - **Validation**: Cross-validated against historical citation data
        - **Architecture**: Advanced transformer-based (LLaMA derivative) textual encoder
        - **Metrics**: Quantitative analysis of citation patterns and research influence
        """
    )

    ############## RATING SCALE ##############
    gr.Markdown(
        """
        ### Rating Scale
        | Grade | Score Range | Description         | Emoji |
        |-------|-------------|---------------------|-------|
        | AAA   | 0.900–1.000 | **Exceptional**     | 🌟    |
        | AA    | 0.800–0.899 | **Very High**       | ⭐    |
        | A     | 0.650–0.799 | **High**            | ✨    |
        | BBB   | 0.600–0.649 | **Above Average**   | πŸ”΅    |
        | BB    | 0.550–0.599 | **Moderate**        | πŸ“˜    |
        | B     | 0.500–0.549 | **Average**         | πŸ“–    |
        | CCC   | 0.400–0.499 | **Below Average**   | πŸ“    |
        | CC    | 0.300–0.399 | **Low**             | ✏️    |
        | C     | <0.300      | **Limited**         | πŸ“‘    |
        """
    )

    ############## EXAMPLE PAPERS ##############
    gr.Markdown("### Example Papers")
    for paper in example_papers:
        gr.Markdown(
            f"**{paper['title']}**  \n"
            f"Score: {paper['score']} | Grade: {get_grade_and_emoji(paper['score'])}  \n"
            f"{paper['abstract']}  \n"
            f"*{paper['note']}*\n---"
        )

    ##################################################
    # Events
    ##################################################
    # Validation triggers
    title_input.change(update_button_status, [title_input, abs_input], [status_box, predict_btn])
    abs_input.change(update_button_status, [title_input, abs_input], [status_box, predict_btn])

    # arXiv fetch
    fetch_btn.click(process_arxiv_input, [arxiv_input], [title_input, abs_input, status_box])

    # Predict handler
    def run_predict(t, a):
        s = predict(t, a)
        return s, get_grade_and_emoji(s)

    predict_btn.click(run_predict, [title_input, abs_input], [score_box, grade_box])

##################################################
# Launch
##################################################
if __name__ == "__main__":
    iface.launch()