Spaces:
Sleeping
Sleeping
changes
Browse files
app.py
CHANGED
|
@@ -9,8 +9,21 @@ from transformers import PerplexityForTextGeneration
|
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
| 12 |
-
# Initialize Perplexity
|
| 13 |
-
perplexity =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@tool
|
| 16 |
def get_ai_research_papers(query: str) -> str:
|
|
@@ -18,14 +31,17 @@ def get_ai_research_papers(query: str) -> str:
|
|
| 18 |
Args:
|
| 19 |
query: The search query for AI research papers.
|
| 20 |
"""
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
response = perplexity.search(
|
| 23 |
query=query,
|
| 24 |
-
max_results=
|
| 25 |
filters={"domain": "AI"}
|
| 26 |
)
|
| 27 |
if response:
|
| 28 |
-
papers = "\n".join([f"
|
| 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:
|
|
@@ -37,6 +53,9 @@ def summarize_paper(paper_title: str) -> str:
|
|
| 37 |
Args:
|
| 38 |
paper_title: Title of the paper to summarize.
|
| 39 |
"""
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
try:
|
| 42 |
response = perplexity.search(
|
|
@@ -58,6 +77,9 @@ def get_citation(paper_title: str) -> str:
|
|
| 58 |
Args:
|
| 59 |
paper_title: Title of the paper to cite.
|
| 60 |
"""
|
|
|
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
try:
|
| 63 |
response = perplexity.search(
|
|
@@ -70,7 +92,7 @@ def get_citation(paper_title: str) -> str:
|
|
| 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}"
|
|
@@ -83,6 +105,9 @@ def explain_concept(concept: str) -> str:
|
|
| 83 |
Args:
|
| 84 |
concept: The concept to explain.
|
| 85 |
"""
|
|
|
|
|
|
|
|
|
|
| 86 |
try:
|
| 87 |
try:
|
| 88 |
response = perplexity.search(
|
|
@@ -117,6 +142,7 @@ agent = CodeAgent(
|
|
| 117 |
model=model,
|
| 118 |
tools=[
|
| 119 |
final_answer,
|
|
|
|
| 120 |
get_ai_research_papers,
|
| 121 |
summarize_paper,
|
| 122 |
get_citation,
|
|
@@ -132,11 +158,66 @@ agent = CodeAgent(
|
|
| 132 |
prompt_templates=prompt_templates
|
| 133 |
)
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
# Launch the enhanced UI
|
| 136 |
-
|
| 137 |
-
|
| 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()
|
|
|
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
| 12 |
+
# Initialize Perplexity without API key - we'll get it from the user
|
| 13 |
+
perplexity = None
|
| 14 |
+
|
| 15 |
+
@tool
|
| 16 |
+
def initialize_perplexity(api_key: str) -> str:
|
| 17 |
+
"""Initialize Perplexity API with your API key.
|
| 18 |
+
Args:
|
| 19 |
+
api_key: Your Perplexity API key
|
| 20 |
+
"""
|
| 21 |
+
global perplexity
|
| 22 |
+
try:
|
| 23 |
+
perplexity = PerplexityForTextGeneration(api_key=api_key)
|
| 24 |
+
return "Perplexity API initialized successfully!"
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error initializing Perplexity API: {str(e)}"
|
| 27 |
|
| 28 |
@tool
|
| 29 |
def get_ai_research_papers(query: str) -> str:
|
|
|
|
| 31 |
Args:
|
| 32 |
query: The search query for AI research papers.
|
| 33 |
"""
|
| 34 |
+
if not perplexity:
|
| 35 |
+
return "Error: Perplexity API not initialized. Please initialize it first."
|
| 36 |
+
|
| 37 |
try:
|
| 38 |
response = perplexity.search(
|
| 39 |
query=query,
|
| 40 |
+
max_results=3,
|
| 41 |
filters={"domain": "AI"}
|
| 42 |
)
|
| 43 |
if response:
|
| 44 |
+
papers = "\n".join([f" {paper.title}" for paper in response])
|
| 45 |
return f"Found these relevant AI papers about {query}:\n{papers}"
|
| 46 |
return f"No relevant AI papers found for your query: {query}"
|
| 47 |
except Exception as e:
|
|
|
|
| 53 |
Args:
|
| 54 |
paper_title: Title of the paper to summarize.
|
| 55 |
"""
|
| 56 |
+
if not perplexity:
|
| 57 |
+
return "Error: Perplexity API not initialized. Please initialize it first."
|
| 58 |
+
|
| 59 |
try:
|
| 60 |
try:
|
| 61 |
response = perplexity.search(
|
|
|
|
| 77 |
Args:
|
| 78 |
paper_title: Title of the paper to cite.
|
| 79 |
"""
|
| 80 |
+
if not perplexity:
|
| 81 |
+
return "Error: Perplexity API not initialized. Please initialize it first."
|
| 82 |
+
|
| 83 |
try:
|
| 84 |
try:
|
| 85 |
response = perplexity.search(
|
|
|
|
| 92 |
Author(s): {paper.authors}
|
| 93 |
Published in: {paper.venue}
|
| 94 |
Year: {paper.year}
|
| 95 |
+
DOI: {paper.doi if paper.doi else 'Not available'}"""
|
| 96 |
return citation
|
| 97 |
except IndexError:
|
| 98 |
return f"Could not find a paper titled '{paper_title}"
|
|
|
|
| 105 |
Args:
|
| 106 |
concept: The concept to explain.
|
| 107 |
"""
|
| 108 |
+
if not perplexity:
|
| 109 |
+
return "Error: Perplexity API not initialized. Please initialize it first."
|
| 110 |
+
|
| 111 |
try:
|
| 112 |
try:
|
| 113 |
response = perplexity.search(
|
|
|
|
| 142 |
model=model,
|
| 143 |
tools=[
|
| 144 |
final_answer,
|
| 145 |
+
initialize_perplexity,
|
| 146 |
get_ai_research_papers,
|
| 147 |
summarize_paper,
|
| 148 |
get_citation,
|
|
|
|
| 158 |
prompt_templates=prompt_templates
|
| 159 |
)
|
| 160 |
|
| 161 |
+
# Modified UI with API key input
|
| 162 |
+
def create_ui():
|
| 163 |
+
from gradio import Blocks
|
| 164 |
+
with Blocks() as demo:
|
| 165 |
+
with gradio.Row():
|
| 166 |
+
gradio.Markdown("# AI Research Assistant")
|
| 167 |
+
gradio.Markdown("Your AI-powered research companion")
|
| 168 |
+
|
| 169 |
+
with gradio.Row():
|
| 170 |
+
input_api_key = gradio.Textbox(label="Enter your Perplexity API Key:")
|
| 171 |
+
initialize_button = gradio.Button("Initialize Perplexity API")
|
| 172 |
+
|
| 173 |
+
initialize_button.click(
|
| 174 |
+
fn=initialize_perplexity,
|
| 175 |
+
inputs=input_api_key,
|
| 176 |
+
outputs=gradio.Output(label="Initialization Status")
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
with gradio.Row():
|
| 180 |
+
input_query = gradio.Textbox(label="Search query for AI papers:")
|
| 181 |
+
search_button = gradio.Button("Search Papers")
|
| 182 |
+
|
| 183 |
+
search_button.click(
|
| 184 |
+
fn=get_ai_research_papers,
|
| 185 |
+
inputs=input_query,
|
| 186 |
+
outputs=gradio.Output(label="Search Results")
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
with gradio.Row():
|
| 190 |
+
input_paper_title = gradio.Textbox(label="Paper title to summarize:")
|
| 191 |
+
summarize_button = gradio.Button("Summarize Paper")
|
| 192 |
+
|
| 193 |
+
summarize_button.click(
|
| 194 |
+
fn=summarize_paper,
|
| 195 |
+
inputs=input_paper_title,
|
| 196 |
+
outputs=gradio.Output(label="Paper Summary")
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
with gradio.Row():
|
| 200 |
+
input_citation_title = gradio.Textbox(label="Paper title to cite:")
|
| 201 |
+
citation_button = gradio.Button("Generate Citation")
|
| 202 |
+
|
| 203 |
+
citation_button.click(
|
| 204 |
+
fn=get_citation,
|
| 205 |
+
inputs=input_citation_title,
|
| 206 |
+
outputs=gradio.Output(label="Citation")
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
with gradio.Row():
|
| 210 |
+
input_concept = gradio.Textbox(label="Concept to explain:")
|
| 211 |
+
explain_button = gradio.Button("Explain Concept")
|
| 212 |
+
|
| 213 |
+
explain_button.click(
|
| 214 |
+
fn=explain_concept,
|
| 215 |
+
inputs=input_concept,
|
| 216 |
+
outputs=gradio.Output(label="Explanation")
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
return demo
|
| 220 |
+
|
| 221 |
# Launch the enhanced UI
|
| 222 |
+
if __name__ == "__main__":
|
| 223 |
+
create_ui()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|