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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from crewai import Agent, Task, Crew
|
| 3 |
+
from crewai_tools import SerperDevTool, BaseTool
|
| 4 |
+
import arxiv
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Custom ArxivSearchTool
|
| 8 |
+
class ArxivSearchTool(BaseTool):
|
| 9 |
+
name: str = "ArxivSearch"
|
| 10 |
+
description: str = "Tool to search scientific papers from arXiv"
|
| 11 |
+
|
| 12 |
+
def _run(self, query: str) -> str:
|
| 13 |
+
results = list(arxiv.Search(query=query, max_results=3).results())
|
| 14 |
+
return "\n".join(f"{r.title} - {r.entry_id}" for r in results)
|
| 15 |
+
|
| 16 |
+
# Custom FileIOTool
|
| 17 |
+
class FileIOTool(BaseTool):
|
| 18 |
+
name: str = "FileIOTool"
|
| 19 |
+
description: str = "Tool to read from and write to files"
|
| 20 |
+
|
| 21 |
+
def _run(self, action: str, filename: str, content: str = None) -> str:
|
| 22 |
+
if action == "read":
|
| 23 |
+
try:
|
| 24 |
+
with open(filename, 'r') as f:
|
| 25 |
+
return f.read()
|
| 26 |
+
except FileNotFoundError:
|
| 27 |
+
return f"Error: File {filename} not found."
|
| 28 |
+
elif action == "write":
|
| 29 |
+
with open(filename, 'w') as f:
|
| 30 |
+
f.write(content)
|
| 31 |
+
return f"Content written to {filename}"
|
| 32 |
+
else:
|
| 33 |
+
return "Error: Invalid action. Use 'read' or 'write'."
|
| 34 |
+
|
| 35 |
+
# Streamlit App
|
| 36 |
+
st.title("ResearchSpark: Generate Novel Problem Statements")
|
| 37 |
+
|
| 38 |
+
# Load API keys from Streamlit secrets
|
| 39 |
+
if "SERPER_API_KEY" in st.secrets:
|
| 40 |
+
os.environ["SERPER_API_KEY"] = st.secrets["SERPER_API_KEY"]
|
| 41 |
+
else:
|
| 42 |
+
st.error("SERPER_API_KEY not found in secrets.")
|
| 43 |
+
|
| 44 |
+
if "OPENAI_API_KEY" in st.secrets:
|
| 45 |
+
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
|
| 46 |
+
else:
|
| 47 |
+
st.error("OPENAI_API_KEY not found in secrets.")
|
| 48 |
+
|
| 49 |
+
# User Inputs Form
|
| 50 |
+
with st.form(key='user_inputs_form'):
|
| 51 |
+
field = st.text_input("Field of Study", value="Biology")
|
| 52 |
+
interest = st.text_input("Specific Interest", value="Genetics, CRISPR")
|
| 53 |
+
academic_level = st.text_input("Academic Level", value="Undergraduate")
|
| 54 |
+
resources = st.text_input("Available Resources", value="Python, bioinformatics tools, open-source datasets")
|
| 55 |
+
scope = st.text_input("Project Scope", value="3-month project")
|
| 56 |
+
preference = st.text_input("Preference", value="Climate change solutions")
|
| 57 |
+
submit_button = st.form_submit_button(label="Generate Problem Statement")
|
| 58 |
+
|
| 59 |
+
if submit_button:
|
| 60 |
+
# User inputs dictionary
|
| 61 |
+
user_inputs = {
|
| 62 |
+
"field": field,
|
| 63 |
+
"interest": interest,
|
| 64 |
+
"academic_level": academic_level,
|
| 65 |
+
"resources": resources,
|
| 66 |
+
"scope": scope,
|
| 67 |
+
"preference": preference
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
# Instantiate tools
|
| 71 |
+
search_tool = SerperDevTool()
|
| 72 |
+
file_io_tool = FileIOTool()
|
| 73 |
+
arxiv_tool = ArxivSearchTool()
|
| 74 |
+
|
| 75 |
+
# Define Agents
|
| 76 |
+
researcher = Agent(
|
| 77 |
+
role='Researcher',
|
| 78 |
+
goal='Find open-access genetics papers from arXiv and Semantic Scholar',
|
| 79 |
+
backstory='Expert in sourcing academic literature from archives.',
|
| 80 |
+
tools=[search_tool, arxiv_tool, file_io_tool],
|
| 81 |
+
llm="openai/gpt-4o-mini",
|
| 82 |
+
verbose=True
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
analyst = Agent(
|
| 86 |
+
role='Analyst',
|
| 87 |
+
goal='Identify novel research gaps for undergraduate projects',
|
| 88 |
+
backstory='Skilled at spotting underexplored areas in research.',
|
| 89 |
+
tools=[file_io_tool],
|
| 90 |
+
llm="openai/gpt-4o-mini",
|
| 91 |
+
verbose=True
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
writer = Agent(
|
| 95 |
+
role='Writer',
|
| 96 |
+
goal='Craft clear, novel problem statements for students',
|
| 97 |
+
backstory='Expert in translating research gaps into actionable project aims.',
|
| 98 |
+
tools=[file_io_tool],
|
| 99 |
+
llm="openai/gpt-4o-mini",
|
| 100 |
+
verbose=True
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
validator = Agent(
|
| 104 |
+
role='Validator',
|
| 105 |
+
goal='Ensure the novelty of the problem statement',
|
| 106 |
+
backstory='Expert in verifying originality by cross-checking with existing research.',
|
| 107 |
+
tools=[search_tool, arxiv_tool],
|
| 108 |
+
llm="openai/gpt-4o-mini",
|
| 109 |
+
verbose=True
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Define Tasks
|
| 113 |
+
research_task = Task(
|
| 114 |
+
description=f'Search arXiv and Semantic Scholar for open-access papers on {user_inputs["interest"]} from 2024–2025. Save abstracts to a file.',
|
| 115 |
+
expected_output='A text file with 3–5 paper summaries.',
|
| 116 |
+
agent=researcher,
|
| 117 |
+
output_file='summaries.txt'
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
analysis_task = Task(
|
| 121 |
+
description=f'Analyze summaries.txt to identify a novel research gap suitable for an {user_inputs["academic_level"]} in {user_inputs["field"]}.',
|
| 122 |
+
expected_output='A clear description of a research gap.',
|
| 123 |
+
agent=analyst
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
writing_task = Task(
|
| 127 |
+
description=f'Generate a problem statement for an {user_inputs["academic_level"]} in {user_inputs["field"]} interested in {user_inputs["interest"]}, using the identified gap. Include feasibility for {user_inputs["resources"]} and {user_inputs["scope"]}.',
|
| 128 |
+
expected_output='A problem statement saved to a file in the format: "This project aims to [goal] by [approach], addressing [gap] in [context]."',
|
| 129 |
+
agent=writer,
|
| 130 |
+
output_file='problem_statement.txt'
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
validation_task = Task(
|
| 134 |
+
description='Search arXiv and Semantic Scholar to ensure the problem statement in problem_statement.txt is novel and not duplicated in existing research.',
|
| 135 |
+
expected_output='A confirmation that the problem statement is novel, or suggestions for refinement if duplicates are found.',
|
| 136 |
+
agent=validator,
|
| 137 |
+
output_file='validation_result.txt'
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
# Assemble Crew
|
| 141 |
+
crew = Crew(
|
| 142 |
+
agents=[researcher, analyst, writer, validator],
|
| 143 |
+
tasks=[research_task, analysis_task, writing_task, validation_task],
|
| 144 |
+
verbose=True
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# Run Crew and display results
|
| 148 |
+
with st.spinner("Generating Problem Statement..."):
|
| 149 |
+
result = crew.kickoff()
|
| 150 |
+
|
| 151 |
+
# Display results
|
| 152 |
+
st.subheader("Problem Statement")
|
| 153 |
+
with open('problem_statement.txt', 'r') as f:
|
| 154 |
+
st.write(f.read())
|
| 155 |
+
|
| 156 |
+
st.subheader("Validation Result")
|
| 157 |
+
with open('validation_result.txt', 'r') as f:
|
| 158 |
+
st.write(f.read())
|
| 159 |
+
|
| 160 |
+
st.subheader("Summaries (References)")
|
| 161 |
+
with open('summaries.txt', 'r') as f:
|
| 162 |
+
st.write(f.read())
|
| 163 |
+
|
| 164 |
+
st.success("Generation complete!")
|