import gradio as gr
import spaces
import torch
from transformers import 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
model_path = r'ssocean/NAIP'
device = 'cuda' if torch.cuda.is_available() else 'cpu'
global model, tokenizer
model = None
tokenizer = None
def fetch_arxiv_paper(arxiv_input):
"""Fetch paper details from arXiv URL or ID using requests."""
try:
# Extract arXiv ID from URL or use directly
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()
# Fetch metadata using arXiv API
api_url = f'http://export.arxiv.org/api/query?id_list={arxiv_id}'
response = requests.get(api_url)
if response.status_code != 200:
return {
"title": "",
"abstract": "",
"success": False,
"message": "Error fetching paper from arXiv API"
}
# Parse the response XML
root = ET.fromstring(response.text)
# ArXiv API uses namespaces
ns = {'arxiv': 'http://www.w3.org/2005/Atom'}
# Extract title and abstract
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: {str(e)}"
}
@spaces.GPU(duration=60, enable_queue=True)
def predict(title, abstract):
title = title.replace("\n", " ").strip().replace(''',"'")
abstract = abstract.replace("\n", " ").strip().replace(''',"'")
global model, tokenizer
if model is None:
try:
# First try loading without quantization
model = AutoModelForSequenceClassification.from_pretrained(
model_path,
num_labels=1,
device_map='auto',
torch_dtype=torch.float32 if device == 'cpu' else torch.float16
)
except Exception as e:
print(f"Standard loading failed, trying without device mapping: {str(e)}")
# Fallback to basic loading
model = AutoModelForSequenceClassification.from_pretrained(
model_path,
num_labels=1,
torch_dtype=torch.float32
)
if torch.cuda.is_available():
model = model.cuda()
tokenizer = AutoTokenizer.from_pretrained(model_path)
model.eval()
text = f'''Given a certain paper, Title: {title}\n Abstract: {abstract}. \n Predict its normalized academic impact (between 0 and 1):'''
try:
inputs = tokenizer(text, return_tensors="pt")
if torch.cuda.is_available():
inputs = {k: v.cuda() for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
probability = torch.sigmoid(outputs.logits).item()
if probability + 0.05 >= 1.0:
return round(1, 4)
return round(probability + 0.05, 4)
except Exception as e:
print(f"Prediction error: {str(e)}")
return 0.0 # Return default value in case of error
def get_grade_and_emoji(score):
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 π"
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, fundamentally changing NLP and deep learning."
},
{
"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 that demonstrated the power of large language models."
},
{
"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 research paper with useful findings but more limited scope and impact."
}
]
def validate_input(title, abstract):
title = title.replace("\n", " ").strip().replace(''',"'")
abstract = abstract.replace("\n", " ").strip().replace(''',"'")
non_latin_pattern = re.compile(r'[^\u0000-\u007F]')
non_latin_in_title = non_latin_pattern.findall(title)
non_latin_in_abstract = non_latin_pattern.findall(abstract)
if len(title.strip().split(' ')) < 3:
return False, "The title must be at least 3 words long."
if len(abstract.strip().split(' ')) < 50:
return False, "The abstract must be at least 50 words long."
if len((title + abstract).split(' ')) > 1024:
return True, "Warning, the input length is approaching tokenization limits (1024) and may be truncated without further warning!"
if non_latin_in_title:
return False, f"The title contains invalid characters: {', '.join(non_latin_in_title)}. Only English letters and special symbols are allowed."
if non_latin_in_abstract:
return False, f"The abstract contains invalid characters: {', '.join(non_latin_in_abstract)}. Only English letters and special symbols are allowed."
return True, "Inputs are valid!"
def update_button_status(title, abstract):
valid, message = validate_input(title, abstract)
if not valid:
return gr.update(value="Error: " + message), gr.update(interactive=False)
return gr.update(value=message), gr.update(interactive=True)
def process_arxiv_input(arxiv_input):
"""Process arXiv input and update title/abstract fields."""
if not arxiv_input.strip():
return "", "", "Please enter an arXiv URL or ID"
result = fetch_arxiv_paper(arxiv_input)
if result["success"]:
return result["title"], result["abstract"], result["message"]
else:
return "", "", result["message"]
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;
}
.sub-title {
text-align: center;
color: #4b5563;
font-size: 1.5rem !important;
margin-bottom: 2rem !important;
}
.input-section {
background: white;
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
}
.result-section {
background: #f8fafc;
padding: 2rem;
border-radius: 1rem;
margin-top: 2rem;
}
.methodology-section {
background: #ecfdf5;
padding: 2rem;
border-radius: 1rem;
margin-top: 2rem;
}
.example-section {
background: #fff7ed;
padding: 2rem;
border-radius: 1rem;
margin-top: 2rem;
}
.grade-display {
font-size: 3rem;
text-align: center;
margin: 1rem 0;
}
.arxiv-input {
margin-bottom: 1.5rem;
padding: 1rem;
background: #f3f4f6;
border-radius: 0.5rem;
}
.arxiv-link {
color: #2563eb;
text-decoration: underline;
font-size: 0.9em;
margin-top: 0.5em;
}
.arxiv-note {
color: #666;
font-size: 0.9em;
margin-top: 0.5em;
margin-bottom: 0.5em;
}
"""
with gr.Blocks(theme=gr.themes.Default(), css=css) as iface:
gr.Markdown(
"""
# Papers Impact: AI-Powered Research Impact Predictor
## https://discord.gg/openfreeai
"""
)
# Visitor Badge - λ€μ¬μ°κΈ° μμ
gr.HTML("""
""")
with gr.Row():
with gr.Column(elem_classes="input-section"):
# arXiv Input
with gr.Group(elem_classes="arxiv-input"):
gr.Markdown("### π Import from arXiv")
arxiv_input = gr.Textbox(
lines=1,
placeholder="Enter arXiv URL or ID (e.g., 2501.09751)",
label="arXiv Paper URL/ID",
value="https://arxiv.org/pdf/2502.07316" # Default example URL
)
gr.Markdown("""
Click input field to use example paper or browse papers at arxiv.org
""") fetch_button = gr.Button("π Fetch Paper Details", variant="secondary") gr.Markdown("### π Or Enter Paper Details Manually") title_input = gr.Textbox( lines=2, placeholder="Enter Paper Title (minimum 3 words)...", label="Paper Title" ) abstract_input = gr.Textbox( lines=5, placeholder="Enter Paper Abstract (minimum 50 words)...", label="Paper Abstract" ) validation_status = gr.Textbox(label="βοΈ Validation Status", interactive=False) submit_button = gr.Button("π― Predict Impact", interactive=False, variant="primary") with gr.Column(elem_classes="result-section"): with gr.Group(): score_output = gr.Number(label="π― Impact Score") grade_output = gr.Textbox(label="π Grade", value="", elem_classes="grade-display") with gr.Row(elem_classes="methodology-section"): gr.Markdown( """ ### π¬ Scientific Methodology - **Training Data**: Model trained on extensive dataset of published papers from CS.CV, CS.CL(NLP), and CS.AI fields - **Optimization**: NDCG optimization with Sigmoid activation and MSE loss function - **Validation**: Cross-validated against historical paper impact data - **Architecture**: Advanced transformer-based deep textual analysis - **Metrics**: Quantitative analysis of citation patterns and research influence """ ) with gr.Row(): gr.Markdown( """ ### π Rating Scale | Grade | Score Range | Description | Indicator | |-------|-------------|-------------|-----------| | AAA | 0.900-1.000 | Exceptional Impact | π | | AA | 0.800-0.899 | Very High Impact | β | | A | 0.650-0.799 | High Impact | β¨ | | BBB | 0.600-0.649 | Above Average Impact | π΅ | | BB | 0.550-0.599 | Moderate Impact | π | | B | 0.500-0.549 | Average Impact | π | | CCC | 0.400-0.499 | Below Average Impact | π | | CC | 0.300-0.399 | Low Impact | βοΈ | | C | < 0.299 | Limited Impact | π | """ ) # Example Papers Section with gr.Row(elem_classes="example-section"): gr.Markdown("### π Example Papers") for paper in example_papers: gr.Markdown( f""" #### {paper['title']} **Score**: {paper.get('score', 'N/A')} | **Grade**: {get_grade_and_emoji(paper.get('score', 0))} {paper['abstract']} *{paper['note']}* --- """) # Event handlers title_input.change( update_button_status, inputs=[title_input, abstract_input], outputs=[validation_status, submit_button] ) abstract_input.change( update_button_status, inputs=[title_input, abstract_input], outputs=[validation_status, submit_button] ) fetch_button.click( process_arxiv_input, inputs=[arxiv_input], outputs=[title_input, abstract_input, validation_status] ) def process_prediction(title, abstract): score = predict(title, abstract) grade = get_grade_and_emoji(score) return score, grade submit_button.click( process_prediction, inputs=[title_input, abstract_input], outputs=[score_output, grade_output] ) if __name__ == "__main__": iface.launch()