Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
PaperSearchPro - STRICT Hierarchical Keyword Search
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
|
| 6 |
import requests
|
|
@@ -10,6 +10,264 @@ from datetime import datetime
|
|
| 10 |
import gradio as gr
|
| 11 |
import tempfile
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class PaperSearchMobile:
|
| 14 |
def __init__(self):
|
| 15 |
self.base_url = "https://api.openalex.org/works"
|
|
@@ -45,10 +303,6 @@ class PaperSearchMobile:
|
|
| 45 |
page = 1
|
| 46 |
max_attempts = 50
|
| 47 |
|
| 48 |
-
print(f"\nπ Searching: {query}")
|
| 49 |
-
print(f"π
Year range: {year_from} - {year_to}")
|
| 50 |
-
print(f"π Max papers: {max_papers}")
|
| 51 |
-
|
| 52 |
# Fetch papers
|
| 53 |
while len(all_papers) < max_papers and page <= max_attempts:
|
| 54 |
params = {
|
|
@@ -67,11 +321,8 @@ class PaperSearchMobile:
|
|
| 67 |
results = data.get("results", [])
|
| 68 |
|
| 69 |
if not results:
|
| 70 |
-
print(f" βΉοΈ No more results at page {page}")
|
| 71 |
break
|
| 72 |
|
| 73 |
-
print(f" π₯ Page {page}: Got {len(results)} papers")
|
| 74 |
-
|
| 75 |
for work in results:
|
| 76 |
paper = self._extract_paper_info(work)
|
| 77 |
all_papers.append(paper)
|
|
@@ -80,23 +331,15 @@ class PaperSearchMobile:
|
|
| 80 |
time.sleep(0.3)
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
-
print(f" β Error on page {page}: {e}")
|
| 84 |
break
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# ========== STRICT HIERARCHICAL FILTERING ==========
|
| 89 |
keyword_list = []
|
| 90 |
if keywords and keywords.strip():
|
| 91 |
keyword_list = [k.strip().lower() for k in keywords.split(',') if k.strip()]
|
| 92 |
|
| 93 |
if len(keyword_list) >= 2:
|
| 94 |
-
print(f"\nπ STRICT Filtering: MUST contain BOTH '{keyword_list[0]}' AND '{keyword_list[1]}'")
|
| 95 |
-
|
| 96 |
scored_papers = []
|
| 97 |
-
papers_missing = 0
|
| 98 |
-
papers_with_abstract = 0
|
| 99 |
-
|
| 100 |
for paper in all_papers:
|
| 101 |
title_text = paper['Title'].lower()
|
| 102 |
abstract_text = paper.get('Abstract', '').lower()
|
|
@@ -109,12 +352,10 @@ class PaperSearchMobile:
|
|
| 109 |
kw2_present = keyword_list[1] in combined_text
|
| 110 |
|
| 111 |
if not (kw1_present and kw2_present):
|
| 112 |
-
papers_missing += 1
|
| 113 |
continue
|
| 114 |
|
| 115 |
# Check abstract
|
| 116 |
if (keyword_list[0] in abstract_text) or (keyword_list[1] in abstract_text):
|
| 117 |
-
papers_with_abstract += 1
|
| 118 |
paper['abstract_match'] = "Yes"
|
| 119 |
else:
|
| 120 |
paper['abstract_match'] = "No"
|
|
@@ -131,9 +372,7 @@ class PaperSearchMobile:
|
|
| 131 |
if paper['abstract_match'] == "Yes":
|
| 132 |
score += 1
|
| 133 |
|
| 134 |
-
# Truncate abstract for display (but keep full for export)
|
| 135 |
paper['Abstract_Short'] = paper['Abstract'][:200] + "..." if len(paper['Abstract']) > 200 else paper['Abstract']
|
| 136 |
-
|
| 137 |
paper['score'] = score
|
| 138 |
paper['matched'] = ', '.join(matched)
|
| 139 |
paper['required_matched'] = f"β {keyword_list[0]}, β {keyword_list[1]}"
|
|
@@ -142,10 +381,6 @@ class PaperSearchMobile:
|
|
| 142 |
|
| 143 |
scored_papers.sort(key=lambda x: x['score'], reverse=True)
|
| 144 |
all_papers = scored_papers
|
| 145 |
-
|
| 146 |
-
print(f" β
Found {len(all_papers)} papers with BOTH required keywords")
|
| 147 |
-
print(f" π Papers with keywords in abstract: {papers_with_abstract}")
|
| 148 |
-
print(f" β Papers filtered out: {papers_missing}")
|
| 149 |
|
| 150 |
# Prepare display
|
| 151 |
if all_papers:
|
|
@@ -153,35 +388,21 @@ class PaperSearchMobile:
|
|
| 153 |
p['Rank'] = i + 1
|
| 154 |
|
| 155 |
df = pd.DataFrame(all_papers)
|
| 156 |
-
|
| 157 |
-
# Display columns (with truncated abstract)
|
| 158 |
display_cols = ["Rank", "score", "Title", "Authors", "Year", "Citations", "Journal", "abstract_match", "Abstract_Short"]
|
| 159 |
-
|
| 160 |
display_df = df[display_cols]
|
| 161 |
|
| 162 |
rename_dict = {
|
| 163 |
"score": "Relevance",
|
| 164 |
"abstract_match": "In Abstract",
|
| 165 |
-
"Abstract_Short": "Abstract
|
| 166 |
}
|
| 167 |
-
|
| 168 |
display_df = display_df.rename(columns=rename_dict)
|
| 169 |
|
| 170 |
-
# Create summary
|
| 171 |
years_list = [p['Year'] for p in all_papers if p['Year'] != 'N/A']
|
| 172 |
|
| 173 |
-
|
| 174 |
-
### π Search Results
|
| 175 |
-
- **{len(all_papers)} papers** found (contain BOTH '{keyword_list[0]}' AND '{keyword_list[1]}')
|
| 176 |
-
- **{df['abstract_match'].value_counts().get('Yes', 0)} papers** have keywords in abstract
|
| 177 |
-
- **Average relevance score:** {df['score'].mean():.1f}
|
| 178 |
-
- **Year range:** {min(years_list)} - {max(years_list)}
|
| 179 |
-
- **Sort order:** {sort_by} ({sort_order})
|
| 180 |
-
"""
|
| 181 |
-
|
| 182 |
-
return display_df, summary, all_papers
|
| 183 |
|
| 184 |
-
return pd.DataFrame(),
|
| 185 |
|
| 186 |
def _extract_paper_info(self, work):
|
| 187 |
def get_authors(authorships):
|
|
@@ -194,7 +415,7 @@ class PaperSearchMobile:
|
|
| 194 |
authors.append(name)
|
| 195 |
return ", ".join(authors) + ("..." if len(authorships) > 3 else "")
|
| 196 |
|
| 197 |
-
# Get FULL abstract
|
| 198 |
abstract = ""
|
| 199 |
if work.get("abstract_inverted_index"):
|
| 200 |
words = []
|
|
@@ -214,30 +435,21 @@ class PaperSearchMobile:
|
|
| 214 |
"Journal": (work.get("primary_location", {}).get("source") or {}).get("display_name", "Unknown"),
|
| 215 |
"DOI": work.get("doi", "N/A"),
|
| 216 |
"URL": work.get("id", "N/A"),
|
| 217 |
-
"Abstract": abstract,
|
| 218 |
"matched": ""
|
| 219 |
}
|
| 220 |
|
| 221 |
-
# ============================================
|
| 222 |
-
# EXPORT FUNCTION - INCLUDES FULL ABSTRACT
|
| 223 |
-
# ============================================
|
| 224 |
def download_results(papers, fname, fmt):
|
| 225 |
-
"""Create downloadable file with FULL abstract included"""
|
| 226 |
if not papers:
|
| 227 |
return None, "No papers to export"
|
| 228 |
|
| 229 |
-
# Create DataFrame with ALL fields including full abstract
|
| 230 |
df = pd.DataFrame(papers)
|
| 231 |
-
|
| 232 |
-
# Reorder columns for better readability
|
| 233 |
column_order = ['Rank', 'score', 'Title', 'Authors', 'Year', 'Citations', 'Journal',
|
| 234 |
'DOI', 'URL', 'Abstract', 'matched', 'required_matched', 'abstract_match']
|
| 235 |
|
| 236 |
-
# Only include columns that exist
|
| 237 |
available_cols = [col for col in column_order if col in df.columns]
|
| 238 |
df = df[available_cols]
|
| 239 |
|
| 240 |
-
# Rename columns for export
|
| 241 |
df = df.rename(columns={
|
| 242 |
'score': 'Relevance_Score',
|
| 243 |
'matched': 'All_Keywords_Found',
|
|
@@ -251,154 +463,227 @@ def download_results(papers, fname, fmt):
|
|
| 251 |
if fmt == "CSV":
|
| 252 |
filename = f"{fname}_with_abstracts_{timestamp}.csv"
|
| 253 |
df.to_csv(tmp.name, index=False)
|
| 254 |
-
else:
|
| 255 |
filename = f"{fname}_with_abstracts_{timestamp}.xlsx"
|
| 256 |
-
|
| 257 |
-
# Create Excel with multiple sheets
|
| 258 |
with pd.ExcelWriter(tmp.name, engine='openpyxl') as writer:
|
| 259 |
-
# Main results sheet
|
| 260 |
df.to_excel(writer, sheet_name='Results', index=False)
|
| 261 |
|
| 262 |
-
# Summary sheet
|
| 263 |
summary_data = {
|
| 264 |
-
'Metric': ['Total Papers', 'Date Exported', '
|
| 265 |
-
'Value': [
|
| 266 |
-
len(papers),
|
| 267 |
-
datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
| 268 |
-
'User query',
|
| 269 |
-
', '.join(set([k for p in papers for k in p.get('matched', '').split(', ') if k]))
|
| 270 |
-
]
|
| 271 |
}
|
| 272 |
pd.DataFrame(summary_data).to_excel(writer, sheet_name='Summary', index=False)
|
| 273 |
|
| 274 |
-
return tmp.name, f"β
Ready to download: {filename}
|
| 275 |
|
| 276 |
# ============================================
|
| 277 |
-
# GRADIO INTERFACE
|
| 278 |
# ============================================
|
| 279 |
def create_interface():
|
| 280 |
-
with gr.Blocks(title="PaperSearchPro", theme=gr.themes.Soft()) as demo:
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
<h3>π Search Scope:</h3>
|
| 287 |
-
<p>β
<strong>Searching in:</strong> TITLE + ABSTRACT + AUTHORS</p>
|
| 288 |
-
<p>π <strong>Abstracts:</strong> FULL abstracts included in downloadable files</p>
|
| 289 |
</div>
|
| 290 |
""")
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
keywords = gr.Textbox(
|
| 299 |
-
label="
|
| 300 |
-
|
| 301 |
-
|
|
|
|
| 302 |
)
|
| 303 |
|
|
|
|
| 304 |
with gr.Row():
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
|
|
|
|
|
|
| 325 |
|
|
|
|
| 326 |
with gr.Row():
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
-
# Results
|
| 345 |
results = gr.Dataframe(
|
| 346 |
-
label="
|
| 347 |
-
wrap=True,
|
| 348 |
-
row_count=15
|
|
|
|
| 349 |
)
|
| 350 |
papers_state = gr.State([])
|
| 351 |
|
| 352 |
-
# Export section
|
| 353 |
-
gr.Markdown("---")
|
| 354 |
-
gr.Markdown("### πΎ Download Results (FULL ABSTRACTS INCLUDED)")
|
| 355 |
-
with gr.Row():
|
| 356 |
-
filename = gr.Textbox(
|
| 357 |
-
label="Filename",
|
| 358 |
-
value="papers_results",
|
| 359 |
-
placeholder="Enter filename without extension"
|
| 360 |
-
)
|
| 361 |
-
format_type = gr.Dropdown(
|
| 362 |
-
label="Format",
|
| 363 |
-
choices=["CSV", "Excel"],
|
| 364 |
-
value="CSV"
|
| 365 |
-
)
|
| 366 |
-
|
| 367 |
with gr.Row():
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
)
|
|
|
|
|
|
|
| 383 |
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
)
|
| 393 |
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
return demo
|
| 404 |
|
|
@@ -407,16 +692,4 @@ def create_interface():
|
|
| 407 |
# ============================================
|
| 408 |
if __name__ == "__main__":
|
| 409 |
demo = create_interface()
|
| 410 |
-
|
| 411 |
-
print("\n" + "="*90)
|
| 412 |
-
print("π PaperSearchPro - WITH FULL ABSTRACTS IN EXPORT")
|
| 413 |
-
print("="*90)
|
| 414 |
-
print("\nβ
NEW FEATURES:")
|
| 415 |
-
print(" β’ FULL abstracts included in CSV/Excel downloads")
|
| 416 |
-
print(" β’ Abstract preview shown in results table")
|
| 417 |
-
print(" β’ Excel format includes Summary sheet")
|
| 418 |
-
print(" β’ All paper metadata preserved")
|
| 419 |
-
print("\nπ Deployed on Hugging Face Spaces - permanent link!")
|
| 420 |
-
print("="*90 + "\n")
|
| 421 |
-
|
| 422 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
PaperSearchPro - STRICT Hierarchical Keyword Search
|
| 3 |
+
PREMIUM GUI with White Background and Modern Design
|
| 4 |
"""
|
| 5 |
|
| 6 |
import requests
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
import tempfile
|
| 12 |
|
| 13 |
+
# Custom CSS for premium white background design
|
| 14 |
+
custom_css = """
|
| 15 |
+
:root {
|
| 16 |
+
--primary-color: #2563eb;
|
| 17 |
+
--secondary-color: #7c3aed;
|
| 18 |
+
--success-color: #10b981;
|
| 19 |
+
--warning-color: #f59e0b;
|
| 20 |
+
--danger-color: #ef4444;
|
| 21 |
+
--text-primary: #1f2937;
|
| 22 |
+
--text-secondary: #6b7280;
|
| 23 |
+
--border-color: #e5e7eb;
|
| 24 |
+
--bg-white: #ffffff;
|
| 25 |
+
--bg-light: #f9fafb;
|
| 26 |
+
--shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
| 27 |
+
--shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 28 |
+
--shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
|
| 29 |
+
--radius-sm: 0.375rem;
|
| 30 |
+
--radius-md: 0.5rem;
|
| 31 |
+
--radius-lg: 0.75rem;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
/* Main container */
|
| 35 |
+
.gradio-container {
|
| 36 |
+
background-color: var(--bg-light) !important;
|
| 37 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
/* White cards */
|
| 41 |
+
.white-card {
|
| 42 |
+
background-color: var(--bg-white);
|
| 43 |
+
border-radius: var(--radius-lg);
|
| 44 |
+
padding: 1.5rem;
|
| 45 |
+
box-shadow: var(--shadow-sm);
|
| 46 |
+
border: 1px solid var(--border-color);
|
| 47 |
+
transition: box-shadow 0.3s ease;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.white-card:hover {
|
| 51 |
+
box-shadow: var(--shadow-md);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
/* Headers */
|
| 55 |
+
h1, h2, h3 {
|
| 56 |
+
color: var(--text-primary) !important;
|
| 57 |
+
font-weight: 600 !important;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
h1 {
|
| 61 |
+
font-size: 2rem !important;
|
| 62 |
+
margin-bottom: 1rem !important;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
h1 span {
|
| 66 |
+
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
|
| 67 |
+
-webkit-background-clip: text;
|
| 68 |
+
-webkit-text-fill-color: transparent;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/* Input fields */
|
| 72 |
+
.gr-box, .gr-input, .gr-textarea {
|
| 73 |
+
background-color: var(--bg-white) !important;
|
| 74 |
+
border: 1px solid var(--border-color) !important;
|
| 75 |
+
border-radius: var(--radius-md) !important;
|
| 76 |
+
padding: 0.5rem !important;
|
| 77 |
+
transition: border-color 0.3s, box-shadow 0.3s !important;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
.gr-box:focus, .gr-input:focus, .gr-textarea:focus {
|
| 81 |
+
border-color: var(--primary-color) !important;
|
| 82 |
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1) !important;
|
| 83 |
+
outline: none !important;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
/* Buttons */
|
| 87 |
+
.gr-button-primary {
|
| 88 |
+
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important;
|
| 89 |
+
border: none !important;
|
| 90 |
+
color: white !important;
|
| 91 |
+
font-weight: 600 !important;
|
| 92 |
+
padding: 0.75rem 1.5rem !important;
|
| 93 |
+
border-radius: var(--radius-md) !important;
|
| 94 |
+
transition: transform 0.2s, box-shadow 0.2s !important;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
.gr-button-primary:hover {
|
| 98 |
+
transform: translateY(-2px);
|
| 99 |
+
box-shadow: var(--shadow-md) !important;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
.gr-button-primary:active {
|
| 103 |
+
transform: translateY(0);
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
.gr-button-secondary {
|
| 107 |
+
background-color: var(--bg-white) !important;
|
| 108 |
+
border: 1px solid var(--border-color) !important;
|
| 109 |
+
color: var(--text-primary) !important;
|
| 110 |
+
font-weight: 500 !important;
|
| 111 |
+
padding: 0.75rem 1.5rem !important;
|
| 112 |
+
border-radius: var(--radius-md) !important;
|
| 113 |
+
transition: all 0.2s !important;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
.gr-button-secondary:hover {
|
| 117 |
+
background-color: var(--bg-light) !important;
|
| 118 |
+
border-color: var(--text-secondary) !important;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
/* Labels and text */
|
| 122 |
+
label {
|
| 123 |
+
color: var(--text-secondary) !important;
|
| 124 |
+
font-size: 0.875rem !important;
|
| 125 |
+
font-weight: 500 !important;
|
| 126 |
+
margin-bottom: 0.25rem !important;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
/* Dataframe/Table */
|
| 130 |
+
.gr-dataframe {
|
| 131 |
+
background-color: var(--bg-white) !important;
|
| 132 |
+
border: 1px solid var(--border-color) !important;
|
| 133 |
+
border-radius: var(--radius-lg) !important;
|
| 134 |
+
overflow: hidden !important;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
.gr-dataframe table {
|
| 138 |
+
border-collapse: separate !important;
|
| 139 |
+
border-spacing: 0 !important;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
.gr-dataframe th {
|
| 143 |
+
background-color: var(--bg-light) !important;
|
| 144 |
+
color: var(--text-primary) !important;
|
| 145 |
+
font-weight: 600 !important;
|
| 146 |
+
font-size: 0.875rem !important;
|
| 147 |
+
padding: 0.75rem !important;
|
| 148 |
+
border-bottom: 2px solid var(--border-color) !important;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
.gr-dataframe td {
|
| 152 |
+
padding: 0.75rem !important;
|
| 153 |
+
border-bottom: 1px solid var(--border-color) !important;
|
| 154 |
+
color: var(--text-primary) !important;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
.gr-dataframe tr:last-child td {
|
| 158 |
+
border-bottom: none !important;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
.gr-dataframe tr:hover td {
|
| 162 |
+
background-color: var(--bg-light) !important;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
/* Slider */
|
| 166 |
+
.gr-slider {
|
| 167 |
+
background: linear-gradient(90deg, var(--primary-color), var(--secondary-color)) !important;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
/* Checkbox */
|
| 171 |
+
.gr-checkbox {
|
| 172 |
+
accent-color: var(--primary-color) !important;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
/* Tabs */
|
| 176 |
+
.gr-tabs {
|
| 177 |
+
background-color: var(--bg-white) !important;
|
| 178 |
+
border-radius: var(--radius-lg) !important;
|
| 179 |
+
padding: 0.5rem !important;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
.gr-tabs .tab-nav {
|
| 183 |
+
border-bottom: 2px solid var(--border-color) !important;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
.gr-tabs .tab-nav button {
|
| 187 |
+
color: var(--text-secondary) !important;
|
| 188 |
+
font-weight: 500 !important;
|
| 189 |
+
padding: 0.75rem 1.5rem !important;
|
| 190 |
+
border: none !important;
|
| 191 |
+
background: transparent !important;
|
| 192 |
+
transition: all 0.2s !important;
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
.gr-tabs .tab-nav button.selected {
|
| 196 |
+
color: var(--primary-color) !important;
|
| 197 |
+
border-bottom: 2px solid var(--primary-color) !important;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
/* Info boxes */
|
| 201 |
+
.info-box {
|
| 202 |
+
background: linear-gradient(135deg, #f0f9ff, #e6f7ff);
|
| 203 |
+
border-left: 4px solid var(--primary-color);
|
| 204 |
+
border-radius: var(--radius-md);
|
| 205 |
+
padding: 1rem;
|
| 206 |
+
margin: 1rem 0;
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
.warning-box {
|
| 210 |
+
background: linear-gradient(135deg, #fef3c7, #fffbeb);
|
| 211 |
+
border-left: 4px solid var(--warning-color);
|
| 212 |
+
border-radius: var(--radius-md);
|
| 213 |
+
padding: 1rem;
|
| 214 |
+
margin: 1rem 0;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
.success-box {
|
| 218 |
+
background: linear-gradient(135deg, #d1fae5, #ecfdf5);
|
| 219 |
+
border-left: 4px solid var(--success-color);
|
| 220 |
+
border-radius: var(--radius-md);
|
| 221 |
+
padding: 1rem;
|
| 222 |
+
margin: 1rem 0;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
/* Keyword badges */
|
| 226 |
+
.keyword-badge {
|
| 227 |
+
display: inline-block;
|
| 228 |
+
padding: 0.25rem 0.75rem;
|
| 229 |
+
border-radius: 9999px;
|
| 230 |
+
font-size: 0.875rem;
|
| 231 |
+
font-weight: 500;
|
| 232 |
+
margin: 0.25rem;
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
.keyword-required {
|
| 236 |
+
background-color: #fee2e2;
|
| 237 |
+
color: var(--danger-color);
|
| 238 |
+
border: 1px solid #fecaca;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
.keyword-optional {
|
| 242 |
+
background-color: #dcfce7;
|
| 243 |
+
color: var(--success-color);
|
| 244 |
+
border: 1px solid #bbf7d0;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
/* Metrics cards */
|
| 248 |
+
.metric-card {
|
| 249 |
+
background-color: var(--bg-white);
|
| 250 |
+
border-radius: var(--radius-lg);
|
| 251 |
+
padding: 1.25rem;
|
| 252 |
+
box-shadow: var(--shadow-sm);
|
| 253 |
+
border: 1px solid var(--border-color);
|
| 254 |
+
text-align: center;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
.metric-value {
|
| 258 |
+
font-size: 1.875rem;
|
| 259 |
+
font-weight: 700;
|
| 260 |
+
color: var(--primary-color);
|
| 261 |
+
line-height: 1;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
.metric-label {
|
| 265 |
+
font-size: 0.875rem;
|
| 266 |
+
color: var(--text-secondary);
|
| 267 |
+
margin-top: 0.5rem;
|
| 268 |
+
}
|
| 269 |
+
"""
|
| 270 |
+
|
| 271 |
class PaperSearchMobile:
|
| 272 |
def __init__(self):
|
| 273 |
self.base_url = "https://api.openalex.org/works"
|
|
|
|
| 303 |
page = 1
|
| 304 |
max_attempts = 50
|
| 305 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
# Fetch papers
|
| 307 |
while len(all_papers) < max_papers and page <= max_attempts:
|
| 308 |
params = {
|
|
|
|
| 321 |
results = data.get("results", [])
|
| 322 |
|
| 323 |
if not results:
|
|
|
|
| 324 |
break
|
| 325 |
|
|
|
|
|
|
|
| 326 |
for work in results:
|
| 327 |
paper = self._extract_paper_info(work)
|
| 328 |
all_papers.append(paper)
|
|
|
|
| 331 |
time.sleep(0.3)
|
| 332 |
|
| 333 |
except Exception as e:
|
|
|
|
| 334 |
break
|
| 335 |
|
| 336 |
+
# Strict hierarchical filtering
|
|
|
|
|
|
|
| 337 |
keyword_list = []
|
| 338 |
if keywords and keywords.strip():
|
| 339 |
keyword_list = [k.strip().lower() for k in keywords.split(',') if k.strip()]
|
| 340 |
|
| 341 |
if len(keyword_list) >= 2:
|
|
|
|
|
|
|
| 342 |
scored_papers = []
|
|
|
|
|
|
|
|
|
|
| 343 |
for paper in all_papers:
|
| 344 |
title_text = paper['Title'].lower()
|
| 345 |
abstract_text = paper.get('Abstract', '').lower()
|
|
|
|
| 352 |
kw2_present = keyword_list[1] in combined_text
|
| 353 |
|
| 354 |
if not (kw1_present and kw2_present):
|
|
|
|
| 355 |
continue
|
| 356 |
|
| 357 |
# Check abstract
|
| 358 |
if (keyword_list[0] in abstract_text) or (keyword_list[1] in abstract_text):
|
|
|
|
| 359 |
paper['abstract_match'] = "Yes"
|
| 360 |
else:
|
| 361 |
paper['abstract_match'] = "No"
|
|
|
|
| 372 |
if paper['abstract_match'] == "Yes":
|
| 373 |
score += 1
|
| 374 |
|
|
|
|
| 375 |
paper['Abstract_Short'] = paper['Abstract'][:200] + "..." if len(paper['Abstract']) > 200 else paper['Abstract']
|
|
|
|
| 376 |
paper['score'] = score
|
| 377 |
paper['matched'] = ', '.join(matched)
|
| 378 |
paper['required_matched'] = f"β {keyword_list[0]}, β {keyword_list[1]}"
|
|
|
|
| 381 |
|
| 382 |
scored_papers.sort(key=lambda x: x['score'], reverse=True)
|
| 383 |
all_papers = scored_papers
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
# Prepare display
|
| 386 |
if all_papers:
|
|
|
|
| 388 |
p['Rank'] = i + 1
|
| 389 |
|
| 390 |
df = pd.DataFrame(all_papers)
|
|
|
|
|
|
|
| 391 |
display_cols = ["Rank", "score", "Title", "Authors", "Year", "Citations", "Journal", "abstract_match", "Abstract_Short"]
|
|
|
|
| 392 |
display_df = df[display_cols]
|
| 393 |
|
| 394 |
rename_dict = {
|
| 395 |
"score": "Relevance",
|
| 396 |
"abstract_match": "In Abstract",
|
| 397 |
+
"Abstract_Short": "Abstract Preview"
|
| 398 |
}
|
|
|
|
| 399 |
display_df = display_df.rename(columns=rename_dict)
|
| 400 |
|
|
|
|
| 401 |
years_list = [p['Year'] for p in all_papers if p['Year'] != 'N/A']
|
| 402 |
|
| 403 |
+
return display_df, all_papers, years_list, keyword_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
+
return pd.DataFrame(), [], [], []
|
| 406 |
|
| 407 |
def _extract_paper_info(self, work):
|
| 408 |
def get_authors(authorships):
|
|
|
|
| 415 |
authors.append(name)
|
| 416 |
return ", ".join(authors) + ("..." if len(authorships) > 3 else "")
|
| 417 |
|
| 418 |
+
# Get FULL abstract
|
| 419 |
abstract = ""
|
| 420 |
if work.get("abstract_inverted_index"):
|
| 421 |
words = []
|
|
|
|
| 435 |
"Journal": (work.get("primary_location", {}).get("source") or {}).get("display_name", "Unknown"),
|
| 436 |
"DOI": work.get("doi", "N/A"),
|
| 437 |
"URL": work.get("id", "N/A"),
|
| 438 |
+
"Abstract": abstract,
|
| 439 |
"matched": ""
|
| 440 |
}
|
| 441 |
|
|
|
|
|
|
|
|
|
|
| 442 |
def download_results(papers, fname, fmt):
|
|
|
|
| 443 |
if not papers:
|
| 444 |
return None, "No papers to export"
|
| 445 |
|
|
|
|
| 446 |
df = pd.DataFrame(papers)
|
|
|
|
|
|
|
| 447 |
column_order = ['Rank', 'score', 'Title', 'Authors', 'Year', 'Citations', 'Journal',
|
| 448 |
'DOI', 'URL', 'Abstract', 'matched', 'required_matched', 'abstract_match']
|
| 449 |
|
|
|
|
| 450 |
available_cols = [col for col in column_order if col in df.columns]
|
| 451 |
df = df[available_cols]
|
| 452 |
|
|
|
|
| 453 |
df = df.rename(columns={
|
| 454 |
'score': 'Relevance_Score',
|
| 455 |
'matched': 'All_Keywords_Found',
|
|
|
|
| 463 |
if fmt == "CSV":
|
| 464 |
filename = f"{fname}_with_abstracts_{timestamp}.csv"
|
| 465 |
df.to_csv(tmp.name, index=False)
|
| 466 |
+
else:
|
| 467 |
filename = f"{fname}_with_abstracts_{timestamp}.xlsx"
|
|
|
|
|
|
|
| 468 |
with pd.ExcelWriter(tmp.name, engine='openpyxl') as writer:
|
|
|
|
| 469 |
df.to_excel(writer, sheet_name='Results', index=False)
|
| 470 |
|
|
|
|
| 471 |
summary_data = {
|
| 472 |
+
'Metric': ['Total Papers', 'Date Exported', 'Export Format'],
|
| 473 |
+
'Value': [len(papers), datetime.now().strftime('%Y-%m-%d %H:%M:%S'), fmt]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
}
|
| 475 |
pd.DataFrame(summary_data).to_excel(writer, sheet_name='Summary', index=False)
|
| 476 |
|
| 477 |
+
return tmp.name, f"β
Ready to download: {filename}"
|
| 478 |
|
| 479 |
# ============================================
|
| 480 |
+
# PREMIUM GRADIO INTERFACE
|
| 481 |
# ============================================
|
| 482 |
def create_interface():
|
| 483 |
+
with gr.Blocks(title="PaperSearchPro", theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 484 |
+
# Header with gradient title
|
| 485 |
+
gr.HTML("""
|
| 486 |
+
<div style='text-align: center; padding: 2rem 0;'>
|
| 487 |
+
<h1><span>π± PaperSearchPro</span></h1>
|
| 488 |
+
<p style='color: #6b7280; font-size: 1.1rem;'>Academic Paper Search with Hierarchical Keywords</p>
|
|
|
|
|
|
|
|
|
|
| 489 |
</div>
|
| 490 |
""")
|
| 491 |
|
| 492 |
+
# Main content in white card
|
| 493 |
+
with gr.Column(elem_classes="white-card"):
|
| 494 |
+
# Search section
|
| 495 |
+
with gr.Row():
|
| 496 |
+
with gr.Column(scale=2):
|
| 497 |
+
query = gr.Textbox(
|
| 498 |
+
label="π Search Topic",
|
| 499 |
+
placeholder="Enter your research topic...",
|
| 500 |
+
value="photoacoustic imaging",
|
| 501 |
+
elem_classes="gr-input"
|
| 502 |
+
)
|
| 503 |
+
|
| 504 |
+
with gr.Column(scale=1):
|
| 505 |
+
max_papers = gr.Slider(
|
| 506 |
+
label="π Max Papers",
|
| 507 |
+
minimum=100,
|
| 508 |
+
maximum=10000,
|
| 509 |
+
value=2000,
|
| 510 |
+
step=100,
|
| 511 |
+
elem_classes="gr-slider"
|
| 512 |
+
)
|
| 513 |
+
|
| 514 |
+
# Keywords section with badges
|
| 515 |
+
gr.HTML("""
|
| 516 |
+
<div style='margin: 1rem 0;'>
|
| 517 |
+
<h3 style='margin-bottom: 0.5rem;'>π Keyword Hierarchy</h3>
|
| 518 |
+
<div>
|
| 519 |
+
<span class='keyword-badge keyword-required'>Keyword 1: REQUIRED</span>
|
| 520 |
+
<span class='keyword-badge keyword-required'>Keyword 2: REQUIRED</span>
|
| 521 |
+
<span class='keyword-badge keyword-optional'>Keyword 3+: Optional</span>
|
| 522 |
+
</div>
|
| 523 |
+
</div>
|
| 524 |
+
""")
|
| 525 |
|
| 526 |
keywords = gr.Textbox(
|
| 527 |
+
label="Enter keywords (comma-separated)",
|
| 528 |
+
placeholder="blood, photoacoustics, imaging, cancer",
|
| 529 |
+
value="blood, photoacoustics, imaging",
|
| 530 |
+
elem_classes="gr-input"
|
| 531 |
)
|
| 532 |
|
| 533 |
+
# Filters in a grid
|
| 534 |
with gr.Row():
|
| 535 |
+
with gr.Column():
|
| 536 |
+
sort_by = gr.Dropdown(
|
| 537 |
+
label="Sort By",
|
| 538 |
+
choices=["relevance", "citations", "year"],
|
| 539 |
+
value="relevance",
|
| 540 |
+
elem_classes="gr-input"
|
| 541 |
+
)
|
| 542 |
+
|
| 543 |
+
with gr.Column():
|
| 544 |
+
sort_order = gr.Dropdown(
|
| 545 |
+
label="Order",
|
| 546 |
+
choices=["descending", "ascending"],
|
| 547 |
+
value="descending",
|
| 548 |
+
elem_classes="gr-input"
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
+
with gr.Column():
|
| 552 |
+
open_access = gr.Checkbox(
|
| 553 |
+
label="Open Access Only",
|
| 554 |
+
value=False,
|
| 555 |
+
elem_classes="gr-checkbox"
|
| 556 |
+
)
|
| 557 |
|
| 558 |
+
# Year range
|
| 559 |
with gr.Row():
|
| 560 |
+
with gr.Column():
|
| 561 |
+
year_from = gr.Number(
|
| 562 |
+
label="Year From",
|
| 563 |
+
value=2000,
|
| 564 |
+
minimum=1900,
|
| 565 |
+
maximum=2026,
|
| 566 |
+
elem_classes="gr-input"
|
| 567 |
+
)
|
| 568 |
+
|
| 569 |
+
with gr.Column():
|
| 570 |
+
year_to = gr.Number(
|
| 571 |
+
label="Year To",
|
| 572 |
+
value=2026,
|
| 573 |
+
minimum=1900,
|
| 574 |
+
maximum=2026,
|
| 575 |
+
elem_classes="gr-input"
|
| 576 |
+
)
|
| 577 |
|
| 578 |
+
# Search button
|
| 579 |
+
search_btn = gr.Button(
|
| 580 |
+
"π SEARCH PAPERS",
|
| 581 |
+
variant="primary",
|
| 582 |
+
size="lg",
|
| 583 |
+
elem_classes="gr-button-primary"
|
| 584 |
+
)
|
| 585 |
|
| 586 |
+
# Metrics cards (will be updated after search)
|
| 587 |
+
with gr.Row():
|
| 588 |
+
with gr.Column():
|
| 589 |
+
total_papers = gr.Markdown("**0**", elem_classes="metric-value")
|
| 590 |
+
gr.Markdown("Total Papers", elem_classes="metric-label")
|
| 591 |
+
|
| 592 |
+
with gr.Column():
|
| 593 |
+
avg_relevance = gr.Markdown("**0.0**", elem_classes="metric-value")
|
| 594 |
+
gr.Markdown("Avg Relevance", elem_classes="metric-label")
|
| 595 |
+
|
| 596 |
+
with gr.Column():
|
| 597 |
+
year_range = gr.Markdown("**N/A**", elem_classes="metric-value")
|
| 598 |
+
gr.Markdown("Year Range", elem_classes="metric-label")
|
| 599 |
|
| 600 |
+
# Results
|
| 601 |
results = gr.Dataframe(
|
| 602 |
+
label="π Results",
|
| 603 |
+
wrap=True,
|
| 604 |
+
row_count=15,
|
| 605 |
+
elem_classes="gr-dataframe"
|
| 606 |
)
|
| 607 |
papers_state = gr.State([])
|
| 608 |
|
| 609 |
+
# Export section
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
with gr.Row():
|
| 611 |
+
with gr.Column(scale=2):
|
| 612 |
+
filename = gr.Textbox(
|
| 613 |
+
label="Filename",
|
| 614 |
+
value="papers_results",
|
| 615 |
+
placeholder="Enter filename",
|
| 616 |
+
elem_classes="gr-input"
|
| 617 |
+
)
|
| 618 |
+
|
| 619 |
+
with gr.Column(scale=1):
|
| 620 |
+
format_type = gr.Dropdown(
|
| 621 |
+
label="Format",
|
| 622 |
+
choices=["CSV", "Excel"],
|
| 623 |
+
value="CSV",
|
| 624 |
+
elem_classes="gr-input"
|
| 625 |
+
)
|
| 626 |
|
| 627 |
+
export_btn = gr.Button(
|
| 628 |
+
"π₯ DOWNLOAD WITH ABSTRACTS",
|
| 629 |
+
variant="secondary",
|
| 630 |
+
elem_classes="gr-button-secondary"
|
| 631 |
)
|
| 632 |
+
export_status = gr.Textbox(label="", visible=True)
|
| 633 |
+
download_file = gr.File(label="Download Link", visible=False)
|
| 634 |
|
| 635 |
+
# Info box
|
| 636 |
+
gr.HTML("""
|
| 637 |
+
<div class='info-box'>
|
| 638 |
+
<strong>π Note:</strong> Downloaded files include FULL abstracts.
|
| 639 |
+
Excel format includes a Summary sheet with metadata.
|
| 640 |
+
</div>
|
| 641 |
+
""")
|
| 642 |
+
|
| 643 |
+
# Initialize searcher
|
| 644 |
+
searcher = PaperSearchMobile()
|
| 645 |
+
|
| 646 |
+
# Search function
|
| 647 |
+
def search_wrapper(query, keywords, sort_by, sort_order, max_papers, year_from, year_to, open_access):
|
| 648 |
+
df, papers, years, keyword_list = searcher.search_papers(
|
| 649 |
+
query, keywords, sort_by, sort_order, max_papers, year_from, year_to, open_access
|
| 650 |
)
|
| 651 |
|
| 652 |
+
if papers:
|
| 653 |
+
# Update metrics
|
| 654 |
+
total = len(papers)
|
| 655 |
+
avg_score = sum(p.get('score', 0) for p in papers) / total
|
| 656 |
+
year_min = min(years) if years else 'N/A'
|
| 657 |
+
year_max = max(years) if years else 'N/A'
|
| 658 |
+
|
| 659 |
+
total_md = f"**{total}**"
|
| 660 |
+
avg_md = f"**{avg_score:.1f}**"
|
| 661 |
+
year_md = f"**{year_min} - {year_max}**"
|
| 662 |
+
else:
|
| 663 |
+
total_md = "**0**"
|
| 664 |
+
avg_md = "**0.0**"
|
| 665 |
+
year_md = "**N/A**"
|
| 666 |
+
|
| 667 |
+
return df, papers, total_md, avg_md, year_md
|
| 668 |
+
|
| 669 |
+
search_btn.click(
|
| 670 |
+
search_wrapper,
|
| 671 |
+
inputs=[query, keywords, sort_by, sort_order, max_papers, year_from, year_to, open_access],
|
| 672 |
+
outputs=[results, papers_state, total_papers, avg_relevance, year_range]
|
| 673 |
+
)
|
| 674 |
+
|
| 675 |
+
# Export function
|
| 676 |
+
def export_wrapper(papers, fname, fmt):
|
| 677 |
+
if not papers:
|
| 678 |
+
return None, "No papers to export", gr.File(visible=False)
|
| 679 |
+
file_path, message = download_results(papers, fname, fmt)
|
| 680 |
+
return file_path, message, gr.File(visible=True)
|
| 681 |
+
|
| 682 |
+
export_btn.click(
|
| 683 |
+
export_wrapper,
|
| 684 |
+
inputs=[papers_state, filename, format_type],
|
| 685 |
+
outputs=[download_file, export_status, download_file]
|
| 686 |
+
)
|
| 687 |
|
| 688 |
return demo
|
| 689 |
|
|
|
|
| 692 |
# ============================================
|
| 693 |
if __name__ == "__main__":
|
| 694 |
demo = create_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
demo.launch()
|