pdx97 commited on
Commit
e727728
·
verified ·
1 Parent(s): a8348b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -12
app.py CHANGED
@@ -54,7 +54,7 @@ import nltk
54
 
55
  nltk.download('punkt')
56
 
57
- @tool
58
  def fetch_latest_arxiv_papers(keywords: list, num_results: int = 5) -> list:
59
  """Fetches and ranks arXiv papers using BM25 keyword relevance.
60
 
@@ -68,8 +68,8 @@ def fetch_latest_arxiv_papers(keywords: list, num_results: int = 5) -> list:
68
  try:
69
  print(f"DEBUG: Searching arXiv papers with keywords: {keywords}")
70
 
71
- # Broadly search keywords in title and abstract
72
- query = "+OR+".join([f"(ti:\"{kw}\"+OR+abs:\"{kw}\")" for kw in keywords])
73
  query_encoded = urllib.parse.quote(query)
74
  url = f"http://export.arxiv.org/api/query?search_query={query_encoded}&start=0&max_results=50&sortBy=submittedDate&sortOrder=descending"
75
 
@@ -109,7 +109,6 @@ def fetch_latest_arxiv_papers(keywords: list, num_results: int = 5) -> list:
109
  return [{"error": f"Error fetching research papers: {str(e)}"}]
110
 
111
 
112
-
113
  # AI Model
114
  model = HfApiModel(
115
  max_tokens=2096,
@@ -135,7 +134,35 @@ agent = CodeAgent(
135
  prompt_templates=prompt_templates
136
  )
137
 
138
- # Define Gradio Search Function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  def search_papers(user_input):
140
  keywords = [kw.strip() for kw in user_input.split(",") if kw.strip()] # Ensure valid keywords
141
  print(f"DEBUG: Received input keywords - {keywords}") # Debug user input
@@ -146,25 +173,28 @@ def search_papers(user_input):
146
 
147
  results = fetch_latest_arxiv_papers(keywords, num_results=3) # Fetch 3 results
148
  print(f"DEBUG: Results received - {results}") # Debug function output
149
-
 
 
 
 
 
150
  if isinstance(results, list) and results and isinstance(results[0], dict):
151
- #Format output with better readability and clarity
152
  formatted_results = "\n\n".join([
153
  f"---\n\n"
154
- f"📌 **Title:**\n{paper['title']}\n\n"
155
- f"👨‍🔬 **Authors:**\n{paper['authors']}\n\n"
156
  f"📅 **Year:** {paper['year']}\n\n"
157
- f"📖 **Abstract:**\n{paper['abstract'][:500]}... *(truncated for readability)*\n\n"
158
  f"[🔗 Read Full Paper]({paper['link']})\n\n"
159
  for paper in results
160
  ])
161
  return formatted_results
162
-
163
  print("DEBUG: No results found.")
164
  return "No results found. Try different keywords."
165
 
166
 
167
-
168
  # Create Gradio UI
169
  with gr.Blocks() as demo:
170
  gr.Markdown("# ScholarAgent")
 
54
 
55
  nltk.download('punkt')
56
 
57
+ @tool # Register the function properly as a SmolAgents tool
58
  def fetch_latest_arxiv_papers(keywords: list, num_results: int = 5) -> list:
59
  """Fetches and ranks arXiv papers using BM25 keyword relevance.
60
 
 
68
  try:
69
  print(f"DEBUG: Searching arXiv papers with keywords: {keywords}")
70
 
71
+ # Use a general keyword search (without `ti:` and `abs:`)
72
+ query = "+AND+".join([f"all:{kw}" for kw in keywords])
73
  query_encoded = urllib.parse.quote(query)
74
  url = f"http://export.arxiv.org/api/query?search_query={query_encoded}&start=0&max_results=50&sortBy=submittedDate&sortOrder=descending"
75
 
 
109
  return [{"error": f"Error fetching research papers: {str(e)}"}]
110
 
111
 
 
112
  # AI Model
113
  model = HfApiModel(
114
  max_tokens=2096,
 
134
  prompt_templates=prompt_templates
135
  )
136
 
137
+ # # Define Gradio Search Function
138
+ # def search_papers(user_input):
139
+ # keywords = [kw.strip() for kw in user_input.split(",") if kw.strip()] # Ensure valid keywords
140
+ # print(f"DEBUG: Received input keywords - {keywords}") # Debug user input
141
+
142
+ # if not keywords:
143
+ # print("DEBUG: No valid keywords provided.")
144
+ # return "Error: Please enter at least one valid keyword."
145
+
146
+ # results = fetch_latest_arxiv_papers(keywords, num_results=3) # Fetch 3 results
147
+ # print(f"DEBUG: Results received - {results}") # Debug function output
148
+
149
+ # if isinstance(results, list) and results and isinstance(results[0], dict):
150
+ # #Format output with better readability and clarity
151
+ # formatted_results = "\n\n".join([
152
+ # f"---\n\n"
153
+ # f"📌 **Title:**\n{paper['title']}\n\n"
154
+ # f"👨‍🔬 **Authors:**\n{paper['authors']}\n\n"
155
+ # f"📅 **Year:** {paper['year']}\n\n"
156
+ # f"📖 **Abstract:**\n{paper['abstract'][:500]}... *(truncated for readability)*\n\n"
157
+ # f"[🔗 Read Full Paper]({paper['link']})\n\n"
158
+ # for paper in results
159
+ # ])
160
+ # return formatted_results
161
+
162
+ # print("DEBUG: No results found.")
163
+ # return "No results found. Try different keywords."
164
+
165
+ #Search Papers
166
  def search_papers(user_input):
167
  keywords = [kw.strip() for kw in user_input.split(",") if kw.strip()] # Ensure valid keywords
168
  print(f"DEBUG: Received input keywords - {keywords}") # Debug user input
 
173
 
174
  results = fetch_latest_arxiv_papers(keywords, num_results=3) # Fetch 3 results
175
  print(f"DEBUG: Results received - {results}") # Debug function output
176
+
177
+ # ✅ Check if the API returned an error
178
+ if isinstance(results, list) and len(results) > 0 and "error" in results[0]:
179
+ return results[0]["error"] # Return the error message directly
180
+
181
+ # ✅ Format results only if valid papers exist
182
  if isinstance(results, list) and results and isinstance(results[0], dict):
 
183
  formatted_results = "\n\n".join([
184
  f"---\n\n"
185
+ f"📌 **Title:** {paper['title']}\n\n"
186
+ f"👨‍🔬 **Authors:** {paper['authors']}\n\n"
187
  f"📅 **Year:** {paper['year']}\n\n"
188
+ f"📖 **Abstract:** {paper['abstract'][:500]}... *(truncated for readability)*\n\n"
189
  f"[🔗 Read Full Paper]({paper['link']})\n\n"
190
  for paper in results
191
  ])
192
  return formatted_results
193
+
194
  print("DEBUG: No results found.")
195
  return "No results found. Try different keywords."
196
 
197
 
 
198
  # Create Gradio UI
199
  with gr.Blocks() as demo:
200
  gr.Markdown("# ScholarAgent")