Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from reactagent.agents.agent_research import ResearchAgent
|
|
| 5 |
from reactagent.runner import create_parser
|
| 6 |
from reactagent import llm
|
| 7 |
from reactagent.users.user import User
|
|
|
|
| 8 |
import json
|
| 9 |
|
| 10 |
|
|
@@ -25,22 +26,29 @@ example_text = [
|
|
| 25 |
# Load example JSON file
|
| 26 |
def load_example_data():
|
| 27 |
with open("example/example_data.json", "r") as json_file:
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
example_data = load_example_data()
|
| 31 |
|
| 32 |
-
with open("example/ex1_init.py", "r") as f:
|
| 33 |
-
predefined_code = f.read()
|
| 34 |
-
|
| 35 |
-
with open("example/ex1_final.py", "r") as f:
|
| 36 |
-
final_code = f.read()
|
| 37 |
-
|
| 38 |
# Function to handle the selection of an example and populate the respective fields
|
| 39 |
def load_example(example_id):
|
| 40 |
global index_ex
|
| 41 |
index_ex = str(example_id)
|
| 42 |
example = example_data[index_ex]
|
| 43 |
-
paper_text = 'Title:\t' + example['title'] + '\nAbstract:\t' + example['abstract']
|
| 44 |
return paper_text
|
| 45 |
|
| 46 |
example_text = [load_example(1), load_example(2)]
|
|
@@ -58,9 +66,8 @@ def load_example_and_set_index(paper_text_input):
|
|
| 58 |
########## Phase 1 ##############
|
| 59 |
|
| 60 |
def extract_research_elements(paper_text):
|
| 61 |
-
global state_extract
|
| 62 |
state_extract = True
|
| 63 |
-
global index_ex
|
| 64 |
example = example_data[index_ex]
|
| 65 |
tasks = example['research_tasks']
|
| 66 |
gaps = example['research_gaps']
|
|
@@ -73,9 +80,8 @@ def extract_research_elements(paper_text):
|
|
| 73 |
def generate_and_store(tasks, gaps, keywords, recent_works):
|
| 74 |
if (not state_extract):
|
| 75 |
return "", "", "", ""
|
| 76 |
-
global state_generate
|
| 77 |
state_generate = True
|
| 78 |
-
global index_ex
|
| 79 |
hypothesis = example_data[index_ex]['hypothesis']
|
| 80 |
experiment_plan = example_data[index_ex]['experiment_plan']
|
| 81 |
return hypothesis, experiment_plan, hypothesis, experiment_plan
|
|
@@ -83,17 +89,18 @@ def generate_and_store(tasks, gaps, keywords, recent_works):
|
|
| 83 |
########## Phase 2 & 3 ##############
|
| 84 |
def start_experiment_agent(hypothesis, plan):
|
| 85 |
if (not state_extract or not state_generate):
|
| 86 |
-
return "", ""
|
| 87 |
-
global state_agent
|
| 88 |
state_agent = True
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
| 92 |
def submit_feedback(user_feedback, history, previous_response):
|
| 93 |
if (not state_extract or not state_generate or not state_agent):
|
| 94 |
return "", "", ""
|
| 95 |
-
global step_index
|
| 96 |
-
global state_complete
|
| 97 |
step_index += 1
|
| 98 |
msg = history
|
| 99 |
if step_index < len(process_steps):
|
|
@@ -108,7 +115,7 @@ def submit_feedback(user_feedback, history, previous_response):
|
|
| 108 |
response = "Agent Finished."
|
| 109 |
|
| 110 |
|
| 111 |
-
return msg, response,
|
| 112 |
|
| 113 |
def load_phase_2_inputs(hypothesis, plan):
|
| 114 |
return hypothesis, plan, "# Code implementation will be displayed here after Start ExperimentAgent."
|
|
@@ -121,7 +128,7 @@ predefined_action_log = """
|
|
| 121 |
Input: {"script_name": "train.py", "start_line_number": "1", "end_line_number": "74"}
|
| 122 |
Objective: Understand the training script, including data processing, [...]
|
| 123 |
[Observation]: The train.py script imports [...]. Sets random seeds [...]. Defines [...] Placeholder functions [...] exist without implementation. [...]
|
| 124 |
-
[Feedback]: The script structure is clear, but key functions (train_model, predict) need proper implementation for proposed model training and prediction
|
| 125 |
"""
|
| 126 |
|
| 127 |
|
|
@@ -131,7 +138,7 @@ Train MSE: 0.543,
|
|
| 131 |
Test MSE: 0.688
|
| 132 |
Epoch [2/10],
|
| 133 |
Train MSE: 0.242,
|
| 134 |
-
Test MSE: 0.493
|
| 135 |
"""
|
| 136 |
|
| 137 |
# Initialize the global step_index and history
|
|
@@ -198,9 +205,13 @@ def handle_example_click(example_index):
|
|
| 198 |
return load_example(index_ex) # Simply return the text to display it in the textbox
|
| 199 |
|
| 200 |
# Gradio Interface
|
| 201 |
-
with gr.Blocks() as app:
|
| 202 |
gr.Markdown("# MLR- Copilot: Machine Learning Research based on LLM Agents")
|
| 203 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
|
| 206 |
# Use state variables to store generated hypothesis and experiment plan
|
|
@@ -208,51 +219,35 @@ with gr.Blocks() as app:
|
|
| 208 |
experiment_plan_state = gr.State("")
|
| 209 |
|
| 210 |
########## Phase 1: Research Idea Generation Tab ##############
|
| 211 |
-
with gr.Tab("
|
| 212 |
gr.Markdown("### Extract Research Elements and Generate Research Ideas")
|
| 213 |
|
| 214 |
with gr.Row():
|
| 215 |
with gr.Column():
|
| 216 |
-
paper_text_input = gr.Textbox(value=load_example(1), lines=10, label="Research Paper Text")
|
| 217 |
-
extract_button = gr.Button("Extract Research Elements")
|
| 218 |
with gr.Row():
|
| 219 |
tasks_output = gr.Textbox(placeholder="Research task definition", label="Research Tasks", lines=2, interactive=True)
|
| 220 |
gaps_output = gr.Textbox(placeholder="Research gaps of current works", label="Research Gaps", lines=2, interactive=True)
|
| 221 |
keywords_output = gr.Textbox(placeholder="Paper keywords", label="Keywords", lines=2, interactive=True)
|
| 222 |
recent_works_output = gr.Textbox(placeholder="Recent works extracted from Semantic Scholar", label="Recent Works", lines=2, interactive=True)
|
| 223 |
with gr.Column():
|
| 224 |
-
with gr.Row(): # Move the button to the top
|
| 225 |
-
generate_button = gr.Button("Generate Research Hypothesis & Experiment Plan")
|
| 226 |
with gr.Group():
|
| 227 |
-
gr.Markdown("### Research Idea")
|
| 228 |
with gr.Row():
|
| 229 |
hypothesis_output = gr.Textbox(label="Generated Hypothesis", lines=20, interactive=False)
|
| 230 |
experiment_plan_output = gr.Textbox(label="Generated Experiment Plan", lines=20, interactive=False)
|
| 231 |
|
| 232 |
-
# with gr.Row():
|
| 233 |
-
# example_1_button = gr.Button("Load Example 1: " + example_data["1"]["title"])
|
| 234 |
-
# example_2_button = gr.Button("Load Example 2: " + example_data["2"]["title"])
|
| 235 |
-
# Example buttons
|
| 236 |
gr.Examples(
|
| 237 |
examples=example_text,
|
| 238 |
inputs=[paper_text_input],
|
| 239 |
outputs=[paper_text_input, tasks_output, gaps_output, keywords_output, recent_works_output, hypothesis_output, experiment_plan_output],
|
| 240 |
fn=load_example_and_set_index,
|
| 241 |
run_on_click = True,
|
| 242 |
-
label="Click an example to load"
|
| 243 |
)
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
# # Pre-step: load example
|
| 247 |
-
# example_1_button.click(
|
| 248 |
-
# fn=lambda: load_example(1), x
|
| 249 |
-
# outputs=[paper_text_input]
|
| 250 |
-
# )
|
| 251 |
-
|
| 252 |
-
# example_2_button.click(
|
| 253 |
-
# fn=lambda: load_example(2),
|
| 254 |
-
# outputs=[paper_text_input]
|
| 255 |
-
# )
|
| 256 |
|
| 257 |
# Step 1: Extract Research Elements
|
| 258 |
extract_button.click(
|
|
@@ -270,24 +265,27 @@ with gr.Blocks() as app:
|
|
| 270 |
|
| 271 |
|
| 272 |
########## Phase 2 & 3: Experiment implementation and execution ##############
|
| 273 |
-
with gr.Tab("
|
| 274 |
gr.Markdown("### Interact with the ExperimentAgent")
|
| 275 |
|
| 276 |
with gr.Row():
|
| 277 |
with gr.Column():
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
with gr.Column():
|
| 282 |
-
start_exp_agnet = gr.Button("Start ExperimentAgent", elem_classes=["agent-btn"])
|
| 283 |
with gr.Group():
|
| 284 |
gr.Markdown("### Implementation + Execution Log")
|
| 285 |
-
log = gr.Textbox(label="Execution Log", lines=20, interactive=False)
|
| 286 |
-
code_display = gr.Code(label="Implementation", language="python", interactive=False)
|
| 287 |
|
| 288 |
with gr.Column():
|
| 289 |
-
response = gr.Textbox(label="ExperimentAgent Response", lines=30, interactive=False)
|
| 290 |
-
feedback = gr.Textbox(placeholder="N/A", label="User Feedback", lines=3, interactive=True)
|
| 291 |
submit_button = gr.Button("Submit", elem_classes=["Submit-btn"])
|
| 292 |
|
| 293 |
hypothesis_state.change(
|
|
@@ -300,7 +298,7 @@ with gr.Blocks() as app:
|
|
| 300 |
start_exp_agnet.click(
|
| 301 |
fn=start_experiment_agent,
|
| 302 |
inputs=[hypothesis_state, experiment_plan_state],
|
| 303 |
-
outputs=[code_display, log]
|
| 304 |
)
|
| 305 |
|
| 306 |
submit_button.click(
|
|
@@ -311,4 +309,4 @@ with gr.Blocks() as app:
|
|
| 311 |
|
| 312 |
if __name__ == "__main__":
|
| 313 |
step_index = 0
|
| 314 |
-
app.launch(share=True)
|
|
|
|
| 5 |
from reactagent.runner import create_parser
|
| 6 |
from reactagent import llm
|
| 7 |
from reactagent.users.user import User
|
| 8 |
+
import os
|
| 9 |
import json
|
| 10 |
|
| 11 |
|
|
|
|
| 26 |
# Load example JSON file
|
| 27 |
def load_example_data():
|
| 28 |
with open("example/example_data.json", "r") as json_file:
|
| 29 |
+
example_data = json.load(json_file)
|
| 30 |
+
|
| 31 |
+
for idx in example_data.keys():
|
| 32 |
+
try:
|
| 33 |
+
with open(os.path.join("example", example_data[idx]["code_init"]), "r") as f:
|
| 34 |
+
example_data[idx]["code_init"] = f.read()
|
| 35 |
+
except FileNotFoundError:
|
| 36 |
+
print(f"File not found: {example_data[idx]["code_init"]}. Skipping key: {idx}")
|
| 37 |
+
try:
|
| 38 |
+
with open(os.path.join("example", example_data[idx]["code_final"]), "r") as f:
|
| 39 |
+
example_data[idx]["code_final"] = f.read()
|
| 40 |
+
except FileNotFoundError:
|
| 41 |
+
print(f"File not found: {example_data[idx]["code_final"]}. Skipping key: {idx}")
|
| 42 |
+
return example_data
|
| 43 |
|
| 44 |
example_data = load_example_data()
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# Function to handle the selection of an example and populate the respective fields
|
| 47 |
def load_example(example_id):
|
| 48 |
global index_ex
|
| 49 |
index_ex = str(example_id)
|
| 50 |
example = example_data[index_ex]
|
| 51 |
+
paper_text = 'Title:\t' + example['title'] + '\n\nAbstract:\t' + example['abstract']
|
| 52 |
return paper_text
|
| 53 |
|
| 54 |
example_text = [load_example(1), load_example(2)]
|
|
|
|
| 66 |
########## Phase 1 ##############
|
| 67 |
|
| 68 |
def extract_research_elements(paper_text):
|
| 69 |
+
global state_extract, index_ex
|
| 70 |
state_extract = True
|
|
|
|
| 71 |
example = example_data[index_ex]
|
| 72 |
tasks = example['research_tasks']
|
| 73 |
gaps = example['research_gaps']
|
|
|
|
| 80 |
def generate_and_store(tasks, gaps, keywords, recent_works):
|
| 81 |
if (not state_extract):
|
| 82 |
return "", "", "", ""
|
| 83 |
+
global state_generate, index_ex
|
| 84 |
state_generate = True
|
|
|
|
| 85 |
hypothesis = example_data[index_ex]['hypothesis']
|
| 86 |
experiment_plan = example_data[index_ex]['experiment_plan']
|
| 87 |
return hypothesis, experiment_plan, hypothesis, experiment_plan
|
|
|
|
| 89 |
########## Phase 2 & 3 ##############
|
| 90 |
def start_experiment_agent(hypothesis, plan):
|
| 91 |
if (not state_extract or not state_generate):
|
| 92 |
+
return "", "", ""
|
| 93 |
+
global state_agent, step_index, state_complete
|
| 94 |
state_agent = True
|
| 95 |
+
step_index = 0
|
| 96 |
+
state_complete = False
|
| 97 |
+
# predefined_message = f"Implement the following hypothesis and experiment plan:\n\nHypothesis:\n{hypothesis}\n\nExperiment Plan:\n{plan}"
|
| 98 |
+
return example_data[index_ex]['code_init'], predefined_action_log, ""
|
| 99 |
|
| 100 |
def submit_feedback(user_feedback, history, previous_response):
|
| 101 |
if (not state_extract or not state_generate or not state_agent):
|
| 102 |
return "", "", ""
|
| 103 |
+
global step_index, state_complete
|
|
|
|
| 104 |
step_index += 1
|
| 105 |
msg = history
|
| 106 |
if step_index < len(process_steps):
|
|
|
|
| 115 |
response = "Agent Finished."
|
| 116 |
|
| 117 |
|
| 118 |
+
return msg, response, example_data[index_ex]['code_init'] if state_complete else example_data[index_ex]['code_final']
|
| 119 |
|
| 120 |
def load_phase_2_inputs(hypothesis, plan):
|
| 121 |
return hypothesis, plan, "# Code implementation will be displayed here after Start ExperimentAgent."
|
|
|
|
| 128 |
Input: {"script_name": "train.py", "start_line_number": "1", "end_line_number": "74"}
|
| 129 |
Objective: Understand the training script, including data processing, [...]
|
| 130 |
[Observation]: The train.py script imports [...]. Sets random seeds [...]. Defines [...] Placeholder functions [...] exist without implementation. [...]
|
| 131 |
+
[Feedback]: The script structure is clear, but key functions (train_model, predict) need proper implementation for proposed model training and prediction.\n
|
| 132 |
"""
|
| 133 |
|
| 134 |
|
|
|
|
| 138 |
Test MSE: 0.688
|
| 139 |
Epoch [2/10],
|
| 140 |
Train MSE: 0.242,
|
| 141 |
+
Test MSE: 0.493\n
|
| 142 |
"""
|
| 143 |
|
| 144 |
# Initialize the global step_index and history
|
|
|
|
| 205 |
return load_example(index_ex) # Simply return the text to display it in the textbox
|
| 206 |
|
| 207 |
# Gradio Interface
|
| 208 |
+
with gr.Blocks(theme=gr.themes.Default()) as app:
|
| 209 |
gr.Markdown("# MLR- Copilot: Machine Learning Research based on LLM Agents")
|
| 210 |
+
gr.Markdown("### ")
|
| 211 |
+
|
| 212 |
+
gr.Markdown("MLR-Copilot is a framework where LLMs mimic researchers’ thought processes, designed to enhance the productivity of machine learning research by automating the generation and implementation of research ideas. It begins with a research paper, autonomously generating and validating these ideas, while incorporating human feedback to help reach executable research outcomes.")
|
| 213 |
+
|
| 214 |
+
|
| 215 |
|
| 216 |
|
| 217 |
# Use state variables to store generated hypothesis and experiment plan
|
|
|
|
| 219 |
experiment_plan_state = gr.State("")
|
| 220 |
|
| 221 |
########## Phase 1: Research Idea Generation Tab ##############
|
| 222 |
+
with gr.Tab("💡Stage 1: Research Idea Generation"):
|
| 223 |
gr.Markdown("### Extract Research Elements and Generate Research Ideas")
|
| 224 |
|
| 225 |
with gr.Row():
|
| 226 |
with gr.Column():
|
| 227 |
+
paper_text_input = gr.Textbox(value=load_example(1), lines=10, label="📑 Research Paper Text")
|
| 228 |
+
extract_button = gr.Button("🔍 Extract Research Elements")
|
| 229 |
with gr.Row():
|
| 230 |
tasks_output = gr.Textbox(placeholder="Research task definition", label="Research Tasks", lines=2, interactive=True)
|
| 231 |
gaps_output = gr.Textbox(placeholder="Research gaps of current works", label="Research Gaps", lines=2, interactive=True)
|
| 232 |
keywords_output = gr.Textbox(placeholder="Paper keywords", label="Keywords", lines=2, interactive=True)
|
| 233 |
recent_works_output = gr.Textbox(placeholder="Recent works extracted from Semantic Scholar", label="Recent Works", lines=2, interactive=True)
|
| 234 |
with gr.Column():
|
| 235 |
+
with gr.Row(): # Move the button to the top
|
| 236 |
+
generate_button = gr.Button("✍️ Generate Research Hypothesis & Experiment Plan")
|
| 237 |
with gr.Group():
|
| 238 |
+
gr.Markdown("### 🌟 Research Idea")
|
| 239 |
with gr.Row():
|
| 240 |
hypothesis_output = gr.Textbox(label="Generated Hypothesis", lines=20, interactive=False)
|
| 241 |
experiment_plan_output = gr.Textbox(label="Generated Experiment Plan", lines=20, interactive=False)
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
gr.Examples(
|
| 244 |
examples=example_text,
|
| 245 |
inputs=[paper_text_input],
|
| 246 |
outputs=[paper_text_input, tasks_output, gaps_output, keywords_output, recent_works_output, hypothesis_output, experiment_plan_output],
|
| 247 |
fn=load_example_and_set_index,
|
| 248 |
run_on_click = True,
|
| 249 |
+
label="⬇️ Click an example to load"
|
| 250 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
# Step 1: Extract Research Elements
|
| 253 |
extract_button.click(
|
|
|
|
| 265 |
|
| 266 |
|
| 267 |
########## Phase 2 & 3: Experiment implementation and execution ##############
|
| 268 |
+
with gr.Tab("🧪 Stage 2 & Stage 3: Experiment implementation and execution"):
|
| 269 |
gr.Markdown("### Interact with the ExperimentAgent")
|
| 270 |
|
| 271 |
with gr.Row():
|
| 272 |
with gr.Column():
|
| 273 |
+
with gr.Group():
|
| 274 |
+
gr.Markdown("### 🌟 Generated Research Idea")
|
| 275 |
+
with gr.Row():
|
| 276 |
+
idea_input = gr.Textbox(label="Generated Research Hypothesis", lines=30, interactive=False)
|
| 277 |
+
plan_input = gr.Textbox(label="Generated Experiment Plan", lines=30, interactive=False)
|
| 278 |
|
| 279 |
with gr.Column():
|
| 280 |
+
start_exp_agnet = gr.Button("⚙️ Start / Reset ExperimentAgent", elem_classes=["agent-btn"])
|
| 281 |
with gr.Group():
|
| 282 |
gr.Markdown("### Implementation + Execution Log")
|
| 283 |
+
log = gr.Textbox(label="📖 Execution Log", lines=20, interactive=False)
|
| 284 |
+
code_display = gr.Code(label="🧑💻 Implementation", language="python", interactive=False)
|
| 285 |
|
| 286 |
with gr.Column():
|
| 287 |
+
response = gr.Textbox(label="🤖 ExperimentAgent Response", lines=30, interactive=False)
|
| 288 |
+
feedback = gr.Textbox(placeholder="N/A", label="🧑🔬 User Feedback", lines=3, interactive=True)
|
| 289 |
submit_button = gr.Button("Submit", elem_classes=["Submit-btn"])
|
| 290 |
|
| 291 |
hypothesis_state.change(
|
|
|
|
| 298 |
start_exp_agnet.click(
|
| 299 |
fn=start_experiment_agent,
|
| 300 |
inputs=[hypothesis_state, experiment_plan_state],
|
| 301 |
+
outputs=[code_display, log, response]
|
| 302 |
)
|
| 303 |
|
| 304 |
submit_button.click(
|
|
|
|
| 309 |
|
| 310 |
if __name__ == "__main__":
|
| 311 |
step_index = 0
|
| 312 |
+
app.launch(share=True)
|