openfree commited on
Commit
d94f82b
Β·
verified Β·
1 Parent(s): 6f3973a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -0
app.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+ import torch.nn.functional as F
6
+ import torch.nn as nn
7
+ import re
8
+ model_path = r'ssocean/NAIP'
9
+ device = 'cuda:0'
10
+
11
+ global model, tokenizer
12
+ model = None
13
+ tokenizer = None
14
+
15
+ @spaces.GPU(duration=60, enable_queue=True)
16
+ def predict(title, abstract):
17
+ title = title.replace("\n", " ").strip().replace(''',"'")
18
+ abstract = abstract.replace("\n", " ").strip().replace(''',"'")
19
+ global model, tokenizer
20
+ if model is None:
21
+ model = AutoModelForSequenceClassification.from_pretrained(
22
+ model_path,
23
+ num_labels=1,
24
+ load_in_8bit=True,)
25
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
26
+ model.eval()
27
+ text = f'''Given a certain paper, Title: {title}\n Abstract: {abstract}. \n Predict its normalized academic impact (between 0 and 1):'''
28
+ inputs = tokenizer(text, return_tensors="pt").to(device)
29
+ with torch.no_grad():
30
+ outputs = model(**inputs)
31
+ probability = torch.sigmoid(outputs.logits).item()
32
+ if probability + 0.05 >= 1.0:
33
+ return round(1, 4)
34
+ return round(probability + 0.05, 4)
35
+
36
+ def get_grade_and_emoji(score):
37
+ if score >= 0.900: return "AAA 🌟"
38
+ if score >= 0.800: return "AA ⭐"
39
+ if score >= 0.650: return "A ✨"
40
+ if score >= 0.600: return "BBB πŸ”΅"
41
+ if score >= 0.550: return "BB πŸ“˜"
42
+ if score >= 0.500: return "B πŸ“–"
43
+ if score >= 0.400: return "CCC πŸ“"
44
+ if score >= 0.300: return "CC ✏️"
45
+ return "C πŸ“‘"
46
+
47
+ example_papers = [
48
+ {
49
+ "title": "Attention Is All You Need",
50
+ "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.",
51
+ "score": 0.982,
52
+ "grade": "AAA 🌟",
53
+ "note": "Revolutionary paper that introduced the Transformer architecture, fundamentally changing NLP and deep learning."
54
+ },
55
+ {
56
+ "title": "Language Models are Few-Shot Learners",
57
+ "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.",
58
+ "score": 0.956,
59
+ "grade": "AAA 🌟",
60
+ "note": "Groundbreaking GPT-3 paper that demonstrated the power of large language models."
61
+ },
62
+ {
63
+ "title": "An Empirical Study of Neural Network Training Protocols",
64
+ "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.",
65
+ "score": 0.623,
66
+ "grade": "BBB πŸ”΅",
67
+ "note": "Solid research paper with useful findings but more limited scope and impact."
68
+ }
69
+ ]
70
+
71
+ def validate_input(title, abstract):
72
+ title = title.replace("\n", " ").strip().replace(''',"'")
73
+ abstract = abstract.replace("\n", " ").strip().replace(''',"'")
74
+
75
+ non_latin_pattern = re.compile(r'[^\u0000-\u007F]')
76
+ non_latin_in_title = non_latin_pattern.findall(title)
77
+ non_latin_in_abstract = non_latin_pattern.findall(abstract)
78
+
79
+ if len(title.strip().split(' ')) < 3:
80
+ return False, "The title must be at least 3 words long."
81
+ if len(abstract.strip().split(' ')) < 50:
82
+ return False, "The abstract must be at least 50 words long."
83
+ if len((title + abstract).split(' ')) > 1024:
84
+ return True, "Warning, the input length is approaching tokenization limits (1024) and may be truncated without further warning!"
85
+ if non_latin_in_title:
86
+ return False, f"The title contains invalid characters: {', '.join(non_latin_in_title)}. Only English letters and special symbols are allowed."
87
+ if non_latin_in_abstract:
88
+ return False, f"The abstract contains invalid characters: {', '.join(non_latin_in_abstract)}. Only English letters and special symbols are allowed."
89
+
90
+ return True, "Inputs are valid! Good to go!"
91
+
92
+ def update_button_status(title, abstract):
93
+ valid, message = validate_input(title, abstract)
94
+ if not valid:
95
+ return gr.update(value="Error: " + message), gr.update(interactive=False)
96
+ return gr.update(value=message), gr.update(interactive=True)
97
+
98
+ with gr.Blocks(theme=gr.themes.Default()) as iface:
99
+ gr.Markdown("""
100
+ # PaperImpact: AI-Powered Research Impact Predictor
101
+ ### Estimate the future academic impact from the title and abstract with advanced AI analysis
102
+ """)
103
+
104
+ with gr.Row():
105
+ with gr.Column():
106
+ title_input = gr.Textbox(
107
+ lines=2,
108
+ placeholder="Enter Paper Title (minimum 3 words)...",
109
+ label="Paper Title"
110
+ )
111
+ abstract_input = gr.Textbox(
112
+ lines=5,
113
+ placeholder="Enter Paper Abstract (minimum 50 words)...",
114
+ label="Paper Abstract"
115
+ )
116
+ validation_status = gr.Textbox(label="Validation Status", interactive=False)
117
+ submit_button = gr.Button("Predict Impact", interactive=False)
118
+
119
+ with gr.Column():
120
+ score_output = gr.Number(label="Impact Score")
121
+ grade_output = gr.Textbox(label="Grade", value="")
122
+
123
+ # Scientific Methodology Section
124
+ gr.Markdown("""
125
+ ### πŸ”¬ Scientific Methodology
126
+ - Training Data: Model trained on extensive dataset of published papers from CS.CV, CS.CL(NLP), and CS.AI fields with verified citation impacts
127
+ - Optimization Method: Uses NDCG (Normalized Discounted Cumulative Gain) optimization with Sigmoid activation and MSE loss function
128
+ - Validation Process: Cross-validated against historical paper impact data to ensure prediction accuracy
129
+ - Model Architecture: Utilizes advanced transformer-based architecture for deep textual analysis
130
+ - Objective Metrics: Predictions based on quantitative analysis of citation patterns and research influence
131
+ """)
132
+
133
+ # Rating Scale Section
134
+ gr.Markdown("""
135
+ ### πŸ“Š Rating Scale
136
+ - AAA (0.900-1.000) 🌟 - Exceptional Impact
137
+ - AA (0.800-0.899) ⭐ - Very High Impact
138
+ - A (0.650-0.799) ✨ - High Impact
139
+ - BBB (0.600-0.649) πŸ”΅ - Above Average Impact
140
+ - BB (0.550-0.599) πŸ“˜ - Moderate Impact
141
+ - B (0.500-0.549) πŸ“– - Average Impact
142
+ - CCC (0.400-0.499) πŸ“ - Below Average Impact
143
+ - CC (0.300-0.399) ✏️ - Low Impact
144
+ - C (below 0.299) πŸ“‘ - Limited Impact
145
+ """)
146
+
147
+ # Example Papers Section
148
+ gr.Markdown("""
149
+ ### πŸ“‹ Example Papers
150
+ """)
151
+ for paper in example_papers:
152
+ gr.Markdown(f"""
153
+ #### {paper['title']} - {paper['grade']} ({paper['score']})
154
+ {paper['abstract']}
155
+ *Note: {paper['note']}*
156
+ ---
157
+ """)
158
+
159
+ # Important Notes Section
160
+ gr.Markdown("""
161
+ ### πŸ“Œ Important Notes
162
+ - This tool is designed for research in Computer Vision (CV), Natural Language Processing (NLP), and AI fields only
163
+ - Predictions are based on title and abstract analysis using advanced AI models
164
+ - Scores reflect potential academic impact, not paper quality or novelty
165
+ - For research and educational purposes only
166
+ """)
167
+
168
+ # Event handlers
169
+ title_input.change(
170
+ update_button_status,
171
+ inputs=[title_input, abstract_input],
172
+ outputs=[validation_status, submit_button]
173
+ )
174
+ abstract_input.change(
175
+ update_button_status,
176
+ inputs=[title_input, abstract_input],
177
+ outputs=[validation_status, submit_button]
178
+ )
179
+
180
+ def process_prediction(title, abstract):
181
+ score = predict(title, abstract)
182
+ grade = get_grade_and_emoji(score)
183
+ return score, grade
184
+
185
+ submit_button.click(
186
+ process_prediction,
187
+ inputs=[title_input, abstract_input],
188
+ outputs=[score_output, grade_output]
189
+ )
190
+
191
+ if __name__ == "__main__":
192
+ iface.launch()