Spaces:
Running
Running
File size: 17,610 Bytes
79b8e3b 6cdbdc2 79b8e3b 6cdbdc2 79b8e3b 6cdbdc2 79b8e3b 6cdbdc2 79b8e3b 6cdbdc2 79b8e3b 6cdbdc2 79b8e3b 6cdbdc2 79b8e3b 6cdbdc2 ebd9098 79b8e3b 8267210 79b8e3b 8267210 79b8e3b 8267210 79b8e3b 8267210 79b8e3b 422754d 79b8e3b 28fc30b 79b8e3b 422754d 79b8e3b 28fc30b 79b8e3b a1941ae 47b1f89 41b209f a1941ae 41b209f a1941ae 41b209f 47b1f89 6cdbdc2 47b1f89 41b209f a1941ae 41b209f 85e3933 47b1f89 a1941ae 79b8e3b 0d55049 a1941ae 0d55049 d54a2bd 1d916f0 0d55049 41b209f 6dda0fd d54a2bd 41b209f d54a2bd 47b1f89 d54a2bd 41b209f d54a2bd 41b209f d54a2bd 47b1f89 41b209f d54a2bd 41b209f 0f668a0 d54a2bd 41b209f d54a2bd 41b209f d54a2bd ebd9098 41b209f d54a2bd 85e3933 d54a2bd 41b209f d54a2bd a1941ae 41b209f d54a2bd 41b209f a1941ae 47b1f89 d54a2bd 48aa5eb a1941ae 47b1f89 48aa5eb 47b1f89 a1941ae 85e3933 47b1f89 a1941ae 47b1f89 a1941ae 47b1f89 41b209f 47b1f89 2e6775a 41b209f 2e6775a 47b1f89 2e6775a c02e3db 47b1f89 2e6775a a1941ae 2e6775a a1941ae 2e6775a 47b1f89 a1941ae 41b209f a1941ae 47b1f89 a1941ae 47b1f89 a1941ae cde10e2 47b1f89 cde10e2 a1941ae 41b209f 47b1f89 41b209f bcdfde6 47b1f89 |
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 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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# # import feedparser
# # import urllib.parse
# # import yaml
# # import gradio as gr
# # from smolagents import CodeAgent, HfApiModel, tool
# # from tools.final_answer import FinalAnswerTool
# # @tool
# # def fetch_latest_arxiv_papers(keywords: list, num_results: int = 3) -> list:
# # """Fetches the latest research papers from arXiv based on provided keywords.
# # Args:
# # keywords: A list of keywords to search for relevant papers.
# # num_results: The number of papers to fetch (default is 3).
# # Returns:
# # A list of dictionaries containing:
# # - "title": The title of the research paper.
# # - "authors": The authors of the paper.
# # - "year": The publication year.
# # - "abstract": A summary of the research paper.
# # - "link": A direct link to the paper on arXiv.
# # """
# # try:
# # print(f"DEBUG: Searching arXiv papers with keywords: {keywords}") # Debug input
# # #Properly format query with +AND+ for multiple keywords
# # query = "+AND+".join([f"all:{kw}" for kw in keywords])
# # query_encoded = urllib.parse.quote(query) # Encode spaces and special characters
# # url = f"http://export.arxiv.org/api/query?search_query={query_encoded}&start=0&max_results={num_results}&sortBy=submittedDate&sortOrder=descending"
# # print(f"DEBUG: Query URL - {url}") # Debug URL
# # feed = feedparser.parse(url)
# # papers = []
# # for entry in feed.entries:
# # papers.append({
# # "title": entry.title,
# # "authors": ", ".join(author.name for author in entry.authors),
# # "year": entry.published[:4], # Extract year
# # "abstract": entry.summary,
# # "link": entry.link
# # })
# # return papers
# # except Exception as e:
# # print(f"ERROR: {str(e)}") # Debug errors
# # return [f"Error fetching research papers: {str(e)}"]
# #"""------Applied BM25 search for paper retrival------"""
# # from rank_bm25 import BM25Okapi
# # import nltk
# # import os
# # import shutil
# # nltk_data_path = os.path.join(nltk.data.path[0], "tokenizers", "punkt")
# # if os.path.exists(nltk_data_path):
# # shutil.rmtree(nltk_data_path) # Remove corrupted version
# # print("Removed old NLTK 'punkt' data. Reinstalling...")
# # # Step 2: Download the correct 'punkt' tokenizer
# # nltk.download("punkt_tab")
# # print("Successfully installed 'punkt'!")
# # @tool # Register the function properly as a SmolAgents tool
# # def fetch_latest_arxiv_papers(keywords: list, num_results: int = 5) -> list:
# # """Fetches and ranks arXiv papers using BM25 keyword relevance.
# # Args:
# # keywords: List of keywords for search.
# # num_results: Number of results to return.
# # Returns:
# # List of the most relevant papers based on BM25 ranking.
# # """
# # try:
# # print(f"DEBUG: Searching arXiv papers with keywords: {keywords}")
# # # Use a general keyword search (without `ti:` and `abs:`)
# # query = "+AND+".join([f"all:{kw}" for kw in keywords])
# # query_encoded = urllib.parse.quote(query)
# # url = f"http://export.arxiv.org/api/query?search_query={query_encoded}&start=0&max_results=50&sortBy=submittedDate&sortOrder=descending"
# # print(f"DEBUG: Query URL - {url}")
# # feed = feedparser.parse(url)
# # papers = []
# # # Extract papers from arXiv
# # for entry in feed.entries:
# # papers.append({
# # "title": entry.title,
# # "authors": ", ".join(author.name for author in entry.authors),
# # "year": entry.published[:4],
# # "abstract": entry.summary,
# # "link": entry.link
# # })
# # if not papers:
# # return [{"error": "No results found. Try different keywords."}]
# # # Apply BM25 ranking
# # tokenized_corpus = [nltk.word_tokenize(paper["title"].lower() + " " + paper["abstract"].lower()) for paper in papers]
# # bm25 = BM25Okapi(tokenized_corpus)
# # tokenized_query = nltk.word_tokenize(" ".join(keywords).lower())
# # scores = bm25.get_scores(tokenized_query)
# # # Sort papers based on BM25 score
# # ranked_papers = sorted(zip(papers, scores), key=lambda x: x[1], reverse=True)
# # # Return the most relevant ones
# # return [paper[0] for paper in ranked_papers[:num_results]]
# # except Exception as e:
# # print(f"ERROR: {str(e)}")
# # return [{"error": f"Error fetching research papers: {str(e)}"}]
# """------Applied TF-IDF for better semantic search------"""
# import feedparser
# import urllib.parse
# import yaml
# from tools.final_answer import FinalAnswerTool
# import numpy as np
# from sklearn.feature_extraction.text import TfidfVectorizer
# from sklearn.metrics.pairwise import cosine_similarity
# import gradio as gr
# from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
# import nltk
# import datetime
# import requests
# import pytz
# from tools.final_answer import FinalAnswerTool
# from Gradio_UI import GradioUI
# nltk.download("stopwords")
# from nltk.corpus import stopwords
# @tool # ✅ Register the function properly as a SmolAgents tool
# def fetch_latest_arxiv_papers(keywords: list, num_results: int = 5) -> list:
# """Fetches and ranks arXiv papers using TF-IDF and Cosine Similarity.
# Args:
# keywords: List of keywords for search.
# num_results: Number of results to return.
# Returns:
# List of the most relevant papers based on TF-IDF ranking.
# """
# try:
# print(f"DEBUG: Searching arXiv papers with keywords: {keywords}")
# # Use a general keyword search
# query = "+AND+".join([f"all:{kw}" for kw in keywords])
# query_encoded = urllib.parse.quote(query)
# url = f"http://export.arxiv.org/api/query?search_query={query_encoded}&start=0&max_results=50&sortBy=submittedDate&sortOrder=descending"
# print(f"DEBUG: Query URL - {url}")
# feed = feedparser.parse(url)
# papers = []
# # Extract papers from arXiv
# for entry in feed.entries:
# papers.append({
# "title": entry.title,
# "authors": ", ".join(author.name for author in entry.authors),
# "year": entry.published[:4],
# "abstract": entry.summary,
# "link": entry.link
# })
# if not papers:
# return [{"error": "No results found. Try different keywords."}]
# # Prepare TF-IDF Vectorization
# corpus = [paper["title"] + " " + paper["abstract"] for paper in papers]
# vectorizer = TfidfVectorizer(stop_words=stopwords.words('english')) # Remove stopwords
# tfidf_matrix = vectorizer.fit_transform(corpus)
# # Transform Query into TF-IDF Vector
# query_str = " ".join(keywords)
# query_vec = vectorizer.transform([query_str])
# #Compute Cosine Similarity
# similarity_scores = cosine_similarity(query_vec, tfidf_matrix).flatten()
# #Sort papers based on similarity score
# ranked_papers = sorted(zip(papers, similarity_scores), key=lambda x: x[1], reverse=True)
# # Return the most relevant papers
# return [paper[0] for paper in ranked_papers[:num_results]]
# except Exception as e:
# print(f"ERROR: {str(e)}")
# return [{"error": f"Error fetching research papers: {str(e)}"}]
# @tool
# def get_current_time_in_timezone(timezone: str) -> str:
# """A tool that fetches the current local time in a specified timezone.
# Args:
# timezone: A string representing a valid timezone (e.g., 'America/New_York').
# """
# try:
# # Create timezone object
# tz = pytz.timezone(timezone)
# # Get current time in that timezone
# local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
# return f"The current local time in {timezone} is: {local_time}"
# except Exception as e:
# return f"Error fetching time for timezone '{timezone}': {str(e)}"
# final_answer = FinalAnswerTool()
# # AI Model
# model = HfApiModel(
# max_tokens=2096,
# temperature=0.5,
# model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
# custom_role_conversions=None,
# )
# # Import tool from Hub
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# # Load prompt templates
# with open("prompts.yaml", 'r') as stream:
# prompt_templates = yaml.safe_load(stream)
# # Create the AI Agent
# agent = CodeAgent(
# model=model,
# tools=[final_answer,fetch_latest_arxiv_papers], # Add your tools here
# max_steps=6,
# verbosity_level=1,
# grammar=None,
# planning_interval=None,
# name="ScholarAgent",
# description="An AI agent that fetches the latest research papers from arXiv based on user-defined keywords and filters.",
# prompt_templates=prompt_templates
# )
# #Search Papers
# def search_papers(user_input):
# keywords = [kw.strip() for kw in user_input.split(",") if kw.strip()] # Ensure valid keywords
# print(f"DEBUG: Received input keywords - {keywords}") # Debug user input
# if not keywords:
# print("DEBUG: No valid keywords provided.")
# return "Error: Please enter at least one valid keyword."
# results = fetch_latest_arxiv_papers(keywords, num_results=3) # Fetch 3 results
# print(f"DEBUG: Results received - {results}") # Debug function output
# # Check if the API returned an error
# if isinstance(results, list) and len(results) > 0 and "error" in results[0]:
# return results[0]["error"] # Return the error message directly
# # Format results only if valid papers exist
# if isinstance(results, list) and results and isinstance(results[0], dict):
# formatted_results = "\n\n".join([
# f"---\n\n"
# f"📌 **Title:** {paper['title']}\n\n"
# f"👨🔬 **Authors:** {paper['authors']}\n\n"
# f"📅 **Year:** {paper['year']}\n\n"
# f"📖 **Abstract:** {paper['abstract'][:500]}... *(truncated for readability)*\n\n"
# f"[🔗 Read Full Paper]({paper['link']})\n\n"
# for paper in results
# ])
# return formatted_results
# print("DEBUG: No results found.")
# return "No results found. Try different keywords."
# # Create Gradio UI
# with gr.Blocks() as demo:
# gr.Markdown("# ScholarAgent")
# keyword_input = gr.Textbox(label="Enter keywords(comma-separated) or even full sentences ", placeholder="e.g., deep learning, reinforcement learning or NLP in finance or Deep learning in Medicine")
# output_display = gr.Markdown()
# search_button = gr.Button("Search")
# search_button.click(search_papers, inputs=[keyword_input], outputs=[output_display])
# print("DEBUG: Gradio UI is running. Waiting for user input...")
# # Launch Gradio App
# demo.launch()
"""------Enhanced ScholarAgent with Fixes and Features-----"""
import feedparser
import urllib.parse
import yaml
import requests
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import gradio as gr
from smolagents import CodeAgent, HfApiModel, tool
import nltk
from transformers import pipeline
# ✅ Ensure necessary NLTK data is downloaded
nltk.download("stopwords")
nltk.download("punkt")
from nltk.corpus import stopwords
# ✅ GPT Summarization Pipeline
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
@tool
def fetch_latest_arxiv_papers(keywords: list, num_results: int = 5) -> list:
"""
Fetches and ranks arXiv papers using optimized TF-IDF and Cosine Similarity.
Args:
keywords: List of keywords for search.
num_results: Number of results to return.
Returns:
List of the most relevant papers based on TF-IDF ranking.
"""
try:
# ✅ Correct URL encoding for spaces and special characters
query = "+AND+".join([f"all:{kw}" for kw in keywords])
query_encoded = urllib.parse.quote_plus(query) # ✅ FIXED: Correct encoding
url = f"http://export.arxiv.org/api/query?search_query={query_encoded}&start=0&max_results=5&sortBy=submittedDate&sortOrder=descending"
print(f"DEBUG: Query URL - {url}") # ✅ Debugging
feed = feedparser.parse(url)
print(f"DEBUG: API Response - {feed.entries}")
papers = []
for entry in feed.entries:
papers.append({
"title": entry.title,
"authors": ", ".join(author.name for author in entry.authors),
"year": entry.published[:4],
"abstract": entry.summary,
"link": entry.link
})
if not papers:
print("DEBUG: No results from ArXiv API")
return [{"error": "No results found. Try different keywords."}]
# ✅ Debug Corpus before TF-IDF
corpus = [paper["title"] + " " + paper["abstract"] for paper in papers]
print(f"DEBUG: Corpus - {corpus}")
vectorizer = TfidfVectorizer(stop_words=stopwords.words('english'), max_features=2000)
tfidf_matrix = vectorizer.fit_transform(corpus)
print(f"DEBUG: TF-IDF Matrix Shape - {tfidf_matrix.shape}")
query_vec = vectorizer.transform([" ".join(keywords)])
print(f"DEBUG: Query Vector Shape - {query_vec.shape}")
similarity_scores = cosine_similarity(query_vec, tfidf_matrix).flatten()
print(f"DEBUG: Similarity Scores - {similarity_scores}")
# ✅ Rank papers by similarity score
ranked_papers = sorted(zip(papers, similarity_scores), key=lambda x: x[1], reverse=True)
return [paper[0] for paper in ranked_papers[:num_results]]
except Exception as e:
print(f"ERROR: {str(e)}")
return [{"error": f"Error fetching research papers: {str(e)}"}]
@tool
def get_citation_count(paper_title: str) -> int:
"""
Fetches citation count from Semantic Scholar API.
Args:
paper_title (str): Title of the research paper.
Returns:
int: Citation count (default 0 if not found).
"""
try:
base_url = "https://api.semanticscholar.org/graph/v1/paper/search"
params = {"query": paper_title, "fields": "citationCount"}
response = requests.get(base_url, params=params).json()
if "data" in response and response["data"]:
return response["data"][0].get("citationCount", 0)
return 0 # Default to 0 if no data found
except Exception as e:
print(f"ERROR fetching citation count: {e}")
return 0
@tool
def rank_papers_by_citations(papers: list) -> list:
"""
Ranks papers based on citation count and TF-IDF similarity.
Args:
papers (list): List of research papers.
Returns:
list: Papers sorted by citation count and TF-IDF score.
"""
for paper in papers:
paper["citations"] = get_citation_count(paper["title"])
return sorted(papers, key=lambda x: (x["citations"], x.get("tfidf_score", 0)), reverse=True)
# ✅ AI Model
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)
# ✅ Load prompt templates
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# ✅ Create the AI Agent
agent = CodeAgent(
model=model,
tools=[fetch_latest_arxiv_papers, get_citation_count, rank_papers_by_citations],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name="ScholarAgent",
description="An AI agent that fetches and ranks the latest research papers based on citations and relevance.",
prompt_templates=prompt_templates
)
# ✅ Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# ScholarAgent")
keyword_input = gr.Textbox(label="Enter keywords or full sentences", placeholder="e.g., deep learning, reinforcement learning")
output_display = gr.Markdown()
search_button = gr.Button("Search")
def search_papers(user_input):
keywords = [kw.strip() for kw in user_input.split(",") if kw.strip()]
results = fetch_latest_arxiv_papers(keywords, num_results=3)
if isinstance(results, list) and results and "error" in results[0]:
return results[0]["error"]
return "\n\n".join([
f"---\n\n"
f"📌 **Title:** {paper['title']}\n\n"
f"👨🔬 **Authors:** {paper['authors']}\n\n"
f"📅 **Year:** {paper['year']}\n\n"
f"📖 **Summary:** {paper.get('summary', 'No summary available')[:500]}... *(truncated)*\n\n"
f"🔢 **Citations:** {paper.get('citations', 0)}\n\n"
f"[🔗 Read Full Paper]({paper['link']})\n\n"
for paper in results
])
search_button.click(search_papers, inputs=[keyword_input], outputs=[output_display])
print("DEBUG: Gradio UI is running. Waiting for user input...")
# ✅ Launch Gradio App
demo.launch()
|