Spaces:
Sleeping
Sleeping
enhance-research-agent
Browse filesEnhanced research capabilities and improved UI
app.py
CHANGED
|
@@ -1,69 +1,142 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from
|
| 7 |
|
|
|
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""A tool that
|
| 24 |
Args:
|
| 25 |
-
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"Error
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
-
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
|
|
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
-
name=
|
| 64 |
-
description=
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
GradioUI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
from transformers import PerplexityForTextGeneration
|
| 7 |
|
| 8 |
+
# Import additional tools
|
| 9 |
+
from tools.final_answer import FinalAnswerTool
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
| 12 |
+
# Initialize Perplexity for research capabilities
|
| 13 |
+
perplexity = PerplexityForTextGeneration()
|
| 14 |
+
|
| 15 |
+
@tool
|
| 16 |
+
def get_ai_research_papers(query: str) -> str:
|
| 17 |
+
"""A tool that fetches relevant AI research papers using Perplexity API.
|
| 18 |
+
Args:
|
| 19 |
+
query: The search query for AI research papers.
|
| 20 |
+
"""
|
| 21 |
+
try:
|
| 22 |
+
response = perplexity.search(
|
| 23 |
+
query=query,
|
| 24 |
+
max_results=8,
|
| 25 |
+
filters={"domain": "AI"}
|
| 26 |
+
)
|
| 27 |
+
if response:
|
| 28 |
+
papers = "\n".join([f"📄 {paper.title}" for paper in response])
|
| 29 |
+
return f"Found these relevant AI papers about {query}:\n{papers}"
|
| 30 |
+
return f"No relevant AI papers found for your query: {query}"
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error fetching research papers: {str(e)}"
|
| 33 |
+
|
| 34 |
@tool
|
| 35 |
+
def summarize_paper(paper_title: str) -> str:
|
| 36 |
+
"""A tool that summarizes an AI research paper.
|
|
|
|
| 37 |
Args:
|
| 38 |
+
paper_title: Title of the paper to summarize.
|
|
|
|
| 39 |
"""
|
| 40 |
+
try:
|
| 41 |
+
try:
|
| 42 |
+
response = perplexity.search(
|
| 43 |
+
query=paper_title,
|
| 44 |
+
max_results=1
|
| 45 |
+
)
|
| 46 |
+
if response:
|
| 47 |
+
paper = response[0]
|
| 48 |
+
summary = paper.summary
|
| 49 |
+
return f"Summary of '{paper_title}':\n{summary}"
|
| 50 |
+
except IndexError:
|
| 51 |
+
return f"Could not find a paper titled '{paper_title}"
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"Error summarizing paper: {str(e)}"
|
| 54 |
|
| 55 |
@tool
|
| 56 |
+
def get_citation(paper_title: str) -> str:
|
| 57 |
+
"""A tool that generates a citation for an AI research paper.
|
| 58 |
Args:
|
| 59 |
+
paper_title: Title of the paper to cite.
|
| 60 |
"""
|
| 61 |
try:
|
| 62 |
+
try:
|
| 63 |
+
response = perplexity.search(
|
| 64 |
+
query=paper_title,
|
| 65 |
+
max_results=1
|
| 66 |
+
)
|
| 67 |
+
if response:
|
| 68 |
+
paper = response[0]
|
| 69 |
+
citation = f"""{paper.title}
|
| 70 |
+
Author(s): {paper.authors}
|
| 71 |
+
Published in: {paper.venue}
|
| 72 |
+
Year: {paper.year}
|
| 73 |
+
DOI: {paper.doi if paper.doi else 'Not available}"""
|
| 74 |
+
return citation
|
| 75 |
+
except IndexError:
|
| 76 |
+
return f"Could not find a paper titled '{paper_title}"
|
| 77 |
except Exception as e:
|
| 78 |
+
return f"Error generating citation: {str(e)}"
|
| 79 |
|
| 80 |
+
@tool
|
| 81 |
+
def explain_concept(concept: str) -> str:
|
| 82 |
+
"""A tool that explains an AI-related concept in simple terms.
|
| 83 |
+
Args:
|
| 84 |
+
concept: The concept to explain.
|
| 85 |
+
"""
|
| 86 |
+
try:
|
| 87 |
+
try:
|
| 88 |
+
response = perplexity.search(
|
| 89 |
+
query=f"explain {concept}",
|
| 90 |
+
max_results=1
|
| 91 |
+
)
|
| 92 |
+
if response:
|
| 93 |
+
explanation = response[0].summary
|
| 94 |
+
return f"Explanation of {concept}:\n{explanation}"
|
| 95 |
+
except IndexError:
|
| 96 |
+
return f"Could not find an explanation for '{concept}"
|
| 97 |
+
except Exception as e:
|
| 98 |
+
return f"Error explaining concept: {str(e)}"
|
| 99 |
|
| 100 |
final_answer = FinalAnswerTool()
|
| 101 |
|
| 102 |
+
# Initialize the model
|
|
|
|
|
|
|
| 103 |
model = HfApiModel(
|
| 104 |
+
max_tokens=2096,
|
| 105 |
+
temperature=0.5,
|
| 106 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
|
|
|
| 107 |
)
|
| 108 |
|
| 109 |
+
# Load additional tools
|
|
|
|
| 110 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 111 |
|
| 112 |
with open("prompts.yaml", 'r') as stream:
|
| 113 |
prompt_templates = yaml.safe_load(stream)
|
| 114 |
+
|
| 115 |
+
# Create the enhanced research agent
|
| 116 |
agent = CodeAgent(
|
| 117 |
model=model,
|
| 118 |
+
tools=[
|
| 119 |
+
final_answer,
|
| 120 |
+
get_ai_research_papers,
|
| 121 |
+
summarize_paper,
|
| 122 |
+
get_citation,
|
| 123 |
+
explain_concept,
|
| 124 |
+
image_generation_tool,
|
| 125 |
+
],
|
| 126 |
max_steps=6,
|
| 127 |
verbosity_level=1,
|
| 128 |
grammar=None,
|
| 129 |
planning_interval=None,
|
| 130 |
+
name="AI Research Assistant",
|
| 131 |
+
description="An AI-powered research assistant for exploring AI-related papers and concepts.",
|
| 132 |
prompt_templates=prompt_templates
|
| 133 |
)
|
| 134 |
|
| 135 |
+
# Launch the enhanced UI
|
| 136 |
+
GradioUI(
|
| 137 |
+
agent,
|
| 138 |
+
theme="peach",
|
| 139 |
+
title="AI Research Assistant",
|
| 140 |
+
description="Your go-to assistant for finding and understanding AI research papers.",
|
| 141 |
+
allow_flagging="never"
|
| 142 |
+
).launch()
|