Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
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
|
@@ -9,10 +8,10 @@ import requests
|
|
9 |
import pytz
|
10 |
import yaml
|
11 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
12 |
|
13 |
-
|
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.
|
@@ -53,6 +52,8 @@ def generate_visuals(query):
|
|
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)
|
@@ -60,18 +61,13 @@ def generate_visuals(query):
|
|
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 |
-
|
70 |
-
|
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(
|
@@ -80,7 +76,6 @@ def generate_visuals(query):
|
|
80 |
|
81 |
return markdown_text, bar_chart_path, wordcloud_path
|
82 |
|
83 |
-
|
84 |
@tool
|
85 |
def summarize_text(text: str) -> str:
|
86 |
"""Summarizes long academic papers or articles.
|
@@ -112,26 +107,21 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
112 |
|
113 |
final_answer = FinalAnswerTool()
|
114 |
|
115 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
116 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
117 |
-
|
118 |
model = HfApiModel(
|
119 |
-
max_tokens=2096,
|
120 |
-
temperature=0.5,
|
121 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
122 |
-
custom_role_conversions=None,
|
123 |
)
|
124 |
|
125 |
-
|
126 |
# Import tool from Hub
|
127 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
128 |
|
129 |
with open("prompts.yaml", 'r') as stream:
|
130 |
prompt_templates = yaml.safe_load(stream)
|
131 |
-
|
132 |
agent = CodeAgent(
|
133 |
model=model,
|
134 |
-
tools=[final_answer, search_arxiv, summarize_text],
|
135 |
max_steps=6,
|
136 |
verbosity_level=1,
|
137 |
grammar=None,
|
@@ -141,6 +131,7 @@ agent = CodeAgent(
|
|
141 |
prompt_templates=prompt_templates
|
142 |
)
|
143 |
|
|
|
144 |
iface = gr.Interface(
|
145 |
fn=generate_visuals,
|
146 |
inputs="text",
|
@@ -150,6 +141,8 @@ iface = gr.Interface(
|
|
150 |
examples=[["Machine Learning"], ["Quantum Computing"], ["Climate Change"]]
|
151 |
)
|
152 |
|
|
|
153 |
iface.launch()
|
154 |
|
155 |
-
|
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import xml.etree.ElementTree as ET
|
|
|
3 |
from wordcloud import WordCloud
|
4 |
from collections import Counter
|
5 |
import re
|
|
|
8 |
import pytz
|
9 |
import yaml
|
10 |
from tools.final_answer import FinalAnswerTool
|
11 |
+
import gradio as gr
|
12 |
+
import os
|
13 |
|
14 |
+
# Below is an example of a tool that does nothing. Amaze us with your creativity!
|
|
|
|
|
15 |
@tool
|
16 |
def search_arxiv(query: str):
|
17 |
"""Searches arXiv for academic papers and returns structured results.
|
|
|
52 |
words = [word.lower() for title in titles for word in re.findall(r'\b\w+\b', title) if len(word) > 3]
|
53 |
word_counts = Counter(words).most_common(10)
|
54 |
|
55 |
+
# Save Bar Chart Image
|
56 |
+
bar_chart_path = "/tmp/bar_chart.png"
|
57 |
plt.figure(figsize=(8, 5))
|
58 |
plt.bar(*zip(*word_counts), color='skyblue')
|
59 |
plt.xticks(rotation=45)
|
|
|
61 |
plt.xlabel("Keywords")
|
62 |
plt.ylabel("Frequency")
|
63 |
plt.tight_layout()
|
|
|
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 |
+
wordcloud_path = "/tmp/wordcloud.png"
|
70 |
+
wordcloud.to_file(wordcloud_path)
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Display Search Results as Clickable Links
|
73 |
markdown_text = "\n\n".join(
|
|
|
76 |
|
77 |
return markdown_text, bar_chart_path, wordcloud_path
|
78 |
|
|
|
79 |
@tool
|
80 |
def summarize_text(text: str) -> str:
|
81 |
"""Summarizes long academic papers or articles.
|
|
|
107 |
|
108 |
final_answer = FinalAnswerTool()
|
109 |
|
|
|
|
|
|
|
110 |
model = HfApiModel(
|
111 |
+
max_tokens=2096,
|
112 |
+
temperature=0.5,
|
113 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
|
|
114 |
)
|
115 |
|
|
|
116 |
# Import tool from Hub
|
117 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
118 |
|
119 |
with open("prompts.yaml", 'r') as stream:
|
120 |
prompt_templates = yaml.safe_load(stream)
|
121 |
+
|
122 |
agent = CodeAgent(
|
123 |
model=model,
|
124 |
+
tools=[final_answer, search_arxiv, summarize_text], # add your tools here (don't remove final answer)
|
125 |
max_steps=6,
|
126 |
verbosity_level=1,
|
127 |
grammar=None,
|
|
|
131 |
prompt_templates=prompt_templates
|
132 |
)
|
133 |
|
134 |
+
# Gradio Interface for arXiv research search and visualization
|
135 |
iface = gr.Interface(
|
136 |
fn=generate_visuals,
|
137 |
inputs="text",
|
|
|
141 |
examples=[["Machine Learning"], ["Quantum Computing"], ["Climate Change"]]
|
142 |
)
|
143 |
|
144 |
+
# Launch Gradio Interface
|
145 |
iface.launch()
|
146 |
|
147 |
+
# The Gradio UI component (not needed if you already have the Gradio interface launched above)
|
148 |
+
# GradioUI(agent).launch()
|