Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,9 @@
|
|
1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
|
|
|
|
|
|
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
@@ -9,31 +14,71 @@ from Gradio_UI import GradioUI
|
|
9 |
|
10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
-
def search_arxiv(query: str)
|
13 |
-
"""Searches arXiv for academic papers.
|
14 |
|
15 |
Args:
|
16 |
query (str): The topic or keywords to search for.
|
17 |
|
18 |
-
|
19 |
Returns:
|
20 |
-
|
21 |
"""
|
22 |
-
max_results=5
|
23 |
url = f"http://export.arxiv.org/api/query?search_query={query}&max_results={max_results}"
|
24 |
response = requests.get(url)
|
25 |
|
26 |
if response.status_code == 200:
|
27 |
papers = []
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
return
|
37 |
|
38 |
|
39 |
@tool
|
@@ -96,5 +141,15 @@ agent = CodeAgent(
|
|
96 |
prompt_templates=prompt_templates
|
97 |
)
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
GradioUI(agent).launch()
|
|
|
1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
+
import xml.etree.ElementTree as ET
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from wordcloud import WordCloud
|
5 |
+
from collections import Counter
|
6 |
+
import re
|
7 |
import datetime
|
8 |
import requests
|
9 |
import pytz
|
|
|
14 |
|
15 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
16 |
@tool
|
17 |
+
def search_arxiv(query: str):
|
18 |
+
"""Searches arXiv for academic papers and returns structured results.
|
19 |
|
20 |
Args:
|
21 |
query (str): The topic or keywords to search for.
|
22 |
|
|
|
23 |
Returns:
|
24 |
+
list: A list of tuples containing titles, summaries, and links.
|
25 |
"""
|
26 |
+
max_results = 5
|
27 |
url = f"http://export.arxiv.org/api/query?search_query={query}&max_results={max_results}"
|
28 |
response = requests.get(url)
|
29 |
|
30 |
if response.status_code == 200:
|
31 |
papers = []
|
32 |
+
root = ET.fromstring(response.text)
|
33 |
+
for entry in root.findall("{http://www.w3.org/2005/Atom}entry"):
|
34 |
+
title = entry.find("{http://www.w3.org/2005/Atom}title").text
|
35 |
+
summary = entry.find("{http://www.w3.org/2005/Atom}summary").text
|
36 |
+
link = entry.find("{http://www.w3.org/2005/Atom}id").text
|
37 |
+
papers.append((title, summary, link))
|
38 |
+
|
39 |
+
return papers
|
40 |
+
|
41 |
+
return []
|
42 |
+
|
43 |
+
def generate_visuals(query):
|
44 |
+
results = search_arxiv(query)
|
45 |
+
if not results:
|
46 |
+
return "No papers found.", None, None
|
47 |
+
|
48 |
+
# Extract text data
|
49 |
+
titles = [title for title, _, _ in results]
|
50 |
+
summaries = " ".join(summary for _, summary, _ in results)
|
51 |
+
|
52 |
+
# Generate Bar Chart for Keyword Frequency in Titles
|
53 |
+
words = [word.lower() for title in titles for word in re.findall(r'\b\w+\b', title) if len(word) > 3]
|
54 |
+
word_counts = Counter(words).most_common(10)
|
55 |
+
|
56 |
+
plt.figure(figsize=(8, 5))
|
57 |
+
plt.bar(*zip(*word_counts), color='skyblue')
|
58 |
+
plt.xticks(rotation=45)
|
59 |
+
plt.title("Top Keywords in Titles")
|
60 |
+
plt.xlabel("Keywords")
|
61 |
+
plt.ylabel("Frequency")
|
62 |
+
plt.tight_layout()
|
63 |
+
bar_chart_path = "bar_chart.png"
|
64 |
+
plt.savefig(bar_chart_path)
|
65 |
+
plt.close()
|
66 |
+
|
67 |
+
# Generate Word Cloud for Summary Text
|
68 |
+
wordcloud = WordCloud(width=500, height=300, background_color="white").generate(summaries)
|
69 |
+
plt.figure(figsize=(8, 5))
|
70 |
+
plt.imshow(wordcloud, interpolation="bilinear")
|
71 |
+
plt.axis("off")
|
72 |
+
wordcloud_path = "wordcloud.png"
|
73 |
+
plt.savefig(wordcloud_path)
|
74 |
+
plt.close()
|
75 |
+
|
76 |
+
# Display Search Results as Clickable Links
|
77 |
+
markdown_text = "\n\n".join(
|
78 |
+
[f"**[{title}]({link})**\n\n{summary}" for title, summary, link in results]
|
79 |
+
)
|
80 |
|
81 |
+
return markdown_text, bar_chart_path, wordcloud_path
|
82 |
|
83 |
|
84 |
@tool
|
|
|
141 |
prompt_templates=prompt_templates
|
142 |
)
|
143 |
|
144 |
+
iface = gr.Interface(
|
145 |
+
fn=generate_visuals,
|
146 |
+
inputs="text",
|
147 |
+
outputs=["markdown", "image", "image"],
|
148 |
+
title="🔎 arXiv Research Paper Search",
|
149 |
+
description="Enter a topic or keywords to search for academic papers on arXiv. Get a list of papers with visual analysis.",
|
150 |
+
examples=[["Machine Learning"], ["Quantum Computing"], ["Climate Change"]]
|
151 |
+
)
|
152 |
+
|
153 |
+
iface.launch()
|
154 |
|
155 |
GradioUI(agent).launch()
|