Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files
agents.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from crewai import Agent
|
2 |
+
import google.generativeai as genai
|
3 |
+
from tools import pmc_search, google_scholar_search, today_tool
|
4 |
+
import os
|
5 |
+
|
6 |
+
def get_gemini_llm():
|
7 |
+
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
|
8 |
+
model = genai.GenerativeModel('gemini-pro')
|
9 |
+
return model
|
10 |
+
|
11 |
+
def get_researcher_agent(verbose):
|
12 |
+
return Agent(
|
13 |
+
role="Research Scientist",
|
14 |
+
goal="Research and collect scientific articles on the given topic from PMC and Google Scholar.",
|
15 |
+
backstory="""You are an expert research scientist specializing in comprehensive literature review.
|
16 |
+
Your task is to collect at least 20 relevant scientific articles using PMC and Google Scholar.
|
17 |
+
You should ensure the articles are recent and relevant to the topic.""",
|
18 |
+
llm=get_gemini_llm(),
|
19 |
+
tools=[pmc_search, google_scholar_search],
|
20 |
+
allow_delegation=False,
|
21 |
+
verbose=verbose
|
22 |
+
)
|
23 |
+
|
24 |
+
def get_writer_agent(verbose):
|
25 |
+
return Agent(
|
26 |
+
role="Scientific Writer",
|
27 |
+
goal="Write a comprehensive scientific paper based on the collected research.",
|
28 |
+
backstory="""You are an experienced scientific writer with expertise in academic publishing.
|
29 |
+
Your task is to write a well-structured scientific paper following the standard format:
|
30 |
+
Introduction, Materials and Methods, Results, Discussion, Conclusion, and References.
|
31 |
+
Results and Discussion sections should be at least one page long.
|
32 |
+
Use proper APA citation format for all references.""",
|
33 |
+
llm=get_gemini_llm(),
|
34 |
+
tools=[today_tool],
|
35 |
+
allow_delegation=False,
|
36 |
+
verbose=verbose
|
37 |
+
)
|
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from crew import get_crew
|
5 |
+
import threading
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Global lock for thread safety
|
11 |
+
lock = threading.Lock()
|
12 |
+
|
13 |
+
VERBOSE = False
|
14 |
+
|
15 |
+
def invoke(topic):
|
16 |
+
"""Generate scientific paper based on the topic"""
|
17 |
+
if not topic:
|
18 |
+
raise gr.Error("Topic is required.")
|
19 |
+
|
20 |
+
with lock:
|
21 |
+
# Generate the paper
|
22 |
+
paper = str(get_crew(VERBOSE).kickoff(inputs={"topic": topic}))
|
23 |
+
return paper
|
24 |
+
|
25 |
+
# Create the Gradio interface
|
26 |
+
css = """
|
27 |
+
.gradio-container {
|
28 |
+
font-family: 'Arial', sans-serif;
|
29 |
+
}
|
30 |
+
.paper-output {
|
31 |
+
font-size: 16px;
|
32 |
+
line-height: 1.6;
|
33 |
+
padding: 20px;
|
34 |
+
background: #f9f9f9;
|
35 |
+
border-radius: 10px;
|
36 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
37 |
+
}
|
38 |
+
"""
|
39 |
+
|
40 |
+
demo = gr.Interface(
|
41 |
+
fn=invoke,
|
42 |
+
inputs=[
|
43 |
+
gr.Textbox(
|
44 |
+
label="Research Topic",
|
45 |
+
placeholder="Enter your research topic...",
|
46 |
+
lines=2
|
47 |
+
)
|
48 |
+
],
|
49 |
+
outputs=[
|
50 |
+
gr.Markdown(
|
51 |
+
label="Generated Scientific Paper",
|
52 |
+
elem_classes="paper-output"
|
53 |
+
)
|
54 |
+
],
|
55 |
+
title="AI Scientific Paper Generator",
|
56 |
+
description="""This application uses AI agents to generate comprehensive scientific papers.
|
57 |
+
The first agent researches your topic using PubMed Central and Google Scholar,
|
58 |
+
collecting at least 20 relevant articles. The second agent then synthesizes this
|
59 |
+
research into a well-structured scientific paper with proper citations.
|
60 |
+
|
61 |
+
Created by Dr. Fernando Ly""",
|
62 |
+
article="""### How it works
|
63 |
+
1. Enter your research topic
|
64 |
+
2. The Research Agent will collect relevant scientific articles
|
65 |
+
3. The Writing Agent will generate a structured paper with:
|
66 |
+
- Introduction
|
67 |
+
- Materials and Methods
|
68 |
+
- Results
|
69 |
+
- Discussion
|
70 |
+
- Conclusion
|
71 |
+
- References (APA format)
|
72 |
+
|
73 |
+
The paper will include proper citations and be based on real scientific literature.""",
|
74 |
+
css=css,
|
75 |
+
theme=gr.themes.Soft(
|
76 |
+
primary_hue="blue",
|
77 |
+
secondary_hue="gray"
|
78 |
+
)
|
79 |
+
)
|
80 |
+
|
81 |
+
# Launch the app
|
82 |
+
if __name__ == "__main__":
|
83 |
+
demo.launch()
|
crew.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from crewai import Crew, Process
|
2 |
+
from agents import get_researcher_agent, get_writer_agent
|
3 |
+
from tasks import get_research_task, get_writing_task
|
4 |
+
|
5 |
+
def get_crew(verbose=False):
|
6 |
+
# Create Agents
|
7 |
+
researcher = get_researcher_agent(verbose)
|
8 |
+
writer = get_writer_agent(verbose)
|
9 |
+
|
10 |
+
# Create Tasks
|
11 |
+
research_task = get_research_task(researcher)
|
12 |
+
writing_task = get_writing_task(writer)
|
13 |
+
|
14 |
+
# Create Crew
|
15 |
+
crew = Crew(
|
16 |
+
agents=[researcher, writer],
|
17 |
+
tasks=[research_task, writing_task],
|
18 |
+
verbose=verbose,
|
19 |
+
process=Process.sequential
|
20 |
+
)
|
21 |
+
|
22 |
+
return crew
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
crewai==0.11.0
|
2 |
+
google-generativeai==0.3.2
|
3 |
+
scholarly==1.7.11
|
4 |
+
biopython==1.83
|
5 |
+
beautifulsoup4==4.12.2
|
6 |
+
python-dotenv==1.0.0
|
7 |
+
gradio==4.8.0
|
tasks.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from crewai import Task
|
2 |
+
|
3 |
+
def get_research_task(agent):
|
4 |
+
return Task(
|
5 |
+
description="""Research the given topic extensively using PMC and Google Scholar.
|
6 |
+
1. Search for at least 20 relevant scientific articles
|
7 |
+
2. For each article collect:
|
8 |
+
- Title
|
9 |
+
- Authors
|
10 |
+
- Abstract
|
11 |
+
- Key findings
|
12 |
+
- Publication year
|
13 |
+
3. Ensure articles are recent and from reputable sources
|
14 |
+
4. Organize the information in a structured format
|
15 |
+
5. Provide a brief summary of the collective findings""",
|
16 |
+
agent=agent
|
17 |
+
)
|
18 |
+
|
19 |
+
def get_writing_task(agent):
|
20 |
+
return Task(
|
21 |
+
description="""Write a comprehensive scientific paper based on the research provided.
|
22 |
+
The paper must follow this structure:
|
23 |
+
1. Introduction:
|
24 |
+
- Background of the topic
|
25 |
+
- Research gap
|
26 |
+
- Objectives
|
27 |
+
2. Materials and Methods:
|
28 |
+
- Research methodology
|
29 |
+
- Data collection process
|
30 |
+
- Analysis methods
|
31 |
+
3. Results (at least one page):
|
32 |
+
- Key findings from the literature
|
33 |
+
- Data synthesis
|
34 |
+
- Statistical analysis if applicable
|
35 |
+
4. Discussion (at least one page):
|
36 |
+
- Interpretation of results
|
37 |
+
- Comparison with existing literature
|
38 |
+
- Implications
|
39 |
+
- Limitations
|
40 |
+
5. Conclusion:
|
41 |
+
- Summary of findings
|
42 |
+
- Future research directions
|
43 |
+
6. References:
|
44 |
+
- All citations in APA format
|
45 |
+
- Minimum 20 references
|
46 |
+
|
47 |
+
Important:
|
48 |
+
- Use proper academic writing style
|
49 |
+
- Include in-text citations
|
50 |
+
- Maintain logical flow between sections
|
51 |
+
- Support claims with evidence from the literature""",
|
52 |
+
agent=agent
|
53 |
+
)
|
tools.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.tools import tool
|
2 |
+
from scholarly import scholarly
|
3 |
+
from Bio import Entrez
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
import requests
|
6 |
+
import datetime
|
7 |
+
|
8 |
+
@tool
|
9 |
+
def pmc_search(query: str) -> str:
|
10 |
+
"""Search PubMed Central (PMC) for articles"""
|
11 |
+
Entrez.email = "[email protected]"
|
12 |
+
handle = Entrez.esearch(db="pmc", term=query, retmax=10)
|
13 |
+
record = Entrez.read(handle)
|
14 |
+
handle.close()
|
15 |
+
|
16 |
+
articles = []
|
17 |
+
for id in record["IdList"]:
|
18 |
+
handle = Entrez.efetch(db="pmc", id=id, rettype="full", retmode="xml")
|
19 |
+
article = BeautifulSoup(handle.read(), 'xml')
|
20 |
+
handle.close()
|
21 |
+
|
22 |
+
title = article.find("article-title")
|
23 |
+
title = title.text if title else "No title"
|
24 |
+
|
25 |
+
abstract = article.find("abstract")
|
26 |
+
abstract = abstract.text if abstract else "No abstract"
|
27 |
+
|
28 |
+
articles.append({
|
29 |
+
"id": id,
|
30 |
+
"title": title,
|
31 |
+
"abstract": abstract
|
32 |
+
})
|
33 |
+
|
34 |
+
return str(articles)
|
35 |
+
|
36 |
+
@tool
|
37 |
+
def google_scholar_search(query: str) -> str:
|
38 |
+
"""Search Google Scholar for articles"""
|
39 |
+
search_query = scholarly.search_pubs(query)
|
40 |
+
results = []
|
41 |
+
count = 0
|
42 |
+
|
43 |
+
for result in search_query:
|
44 |
+
if count >= 10:
|
45 |
+
break
|
46 |
+
pub = {
|
47 |
+
"title": result.bib.get('title', 'No title'),
|
48 |
+
"author": result.bib.get('author', 'No author'),
|
49 |
+
"year": result.bib.get('year', 'No year'),
|
50 |
+
"abstract": result.bib.get('abstract', 'No abstract')
|
51 |
+
}
|
52 |
+
results.append(pub)
|
53 |
+
count += 1
|
54 |
+
|
55 |
+
return str(results)
|
56 |
+
|
57 |
+
@tool
|
58 |
+
def today_tool() -> str:
|
59 |
+
"""Get today's date"""
|
60 |
+
return str(datetime.date.today())
|