-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
153 lines (121 loc) · 6.27 KB
/
Copy pathmain.py
File metadata and controls
153 lines (121 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Autonomous ML Agent - Main Application
A Streamlit application that automates the entire ML pipeline from data cleaning to model deployment.
"""
import streamlit as st
import pandas as pd
import json
from dotenv import load_dotenv
# Import our custom modules
from prompts import (
build_cleaning_prompt,
build_model_training_prompt,
build_model_analysis_prompt
)
from e2b_executor import (
execute_in_e2b,
execute_model_training_in_e2b
)
from ui_components import (
create_leaderboard_ui,
display_model_analysis,
display_cleaned_data
)
from llm_client import (
get_llm_response,
get_llm_analysis_response
)
# Load environment variables from .env file
load_dotenv()
###################### USER INTERFACE ##########################################################################################
# USES STREAMLIT TO BUILD THE UI AND ADD A TITLE
st.title("Autonomous ML Agent 🕵🏻")
# DESCRIPTION TEXT TO GUIDE THE USER ON HOW TO START
st.markdown("""
Welcome to the Autonomous ML Agent!
This autonomous machine learning agent ingests tabular datasets, automatically cleans and preprocesses the data,
trains models, and optimizes them for target metrics such as accuracy, precision, or recall.
The entire pipeline is orchestrated by LLMs, where they generate and modify code, select appropriate algorithms,
and iteratively refine the pipeline until the best-performing model is achieved.
""")
# USES STREAMLIT TO ALLOW USER TO UPLOAD A CSV FILE
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
# ONCE USER HAS UPLOADED A CSV FILE, IT IS READ INTO A PANDAS DATAFRAME
# THE DATAFRAME IS DISPLAYED TO THE USER ON THE APP
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
st.write(df)
selected_column = st.selectbox("Select a column to predict",
df.columns.tolist(),
help = "The column to predict")
# Button to run the Autonomous ML Agent
button = st.button("Run Autonomous ML Agent")
# ONCE THE BUTTON IS CLICKED, THE AUTONOMOUS ML AGENT STARTS RUNNING
if button:
# SPINNER TO SHOW THAT THE AUTONOMOUS ML AGENT IS RUNNING
with st.spinner("Running Autonomous ML Agent..."):
# STEP 1: DATA CLEANING
st.markdown("### Step 1: Data Cleaning")
# BUILD THE CLEANING PROMPT
cleaning_prompt = build_cleaning_prompt(df, selected_column)
# DISPLAY THE CLEANING PROMPT IN AN ACCORDION
with st.expander("Cleaning Prompt"):
st.write(cleaning_prompt)
# GET THE CLEANING SCRIPT FROM LLM
script = get_llm_response(cleaning_prompt)
# DISPLAY THE SCRIPT IN AN ACCORDION
with st.expander("Cleaning Script"):
st.code(script)
# SPINNER TO SHOW THAT THE SCRIPT IS BEING EXECUTED IN THE E2B SANDBOX
with st.spinner("Executing cleaning script in E2B sandbox..."):
# CONVERT THE DATAFRAME TO A CSV FILE AND ENCODE IT TO BYTES
input_csv_bytes = df.to_csv(index=False).encode('utf-8')
cleaned_bytes, exec_info = execute_in_e2b(script, input_csv_bytes)
# DISPLAY THE EXECUTION INFO IN AN ACCORDION
with st.expander("E2B Execution Info"):
st.write(exec_info)
# DISPLAY THE CLEANED DATA IN AN ACCORDION
with st.expander("Cleaned Data"):
if cleaned_bytes is not None:
cleaned_df = display_cleaned_data(cleaned_bytes)
if cleaned_df is not None:
# STEP 2: MODEL TRAINING
st.markdown("---")
st.markdown("### Step 2: Model Training")
# Build the model training prompt
model_training_prompt = build_model_training_prompt(cleaned_df, selected_column)
# Display the model training prompt
with st.expander("Model Training Prompt"):
st.write(model_training_prompt)
# Get the model training script from LLM
model_script = get_llm_response(model_training_prompt)
# Display the model training script
with st.expander("Model Training Script"):
st.code(model_script)
# Initialize variables
model_results_df = None
model_exec_info = {}
# Execute model training in E2B sandbox
with st.spinner("Training models in E2B sandbox..."):
model_results_df, model_exec_info = execute_model_training_in_e2b(
model_script, cleaned_bytes
)
# Display model training execution info
with st.expander("Model Training Execution Info"):
st.write(model_exec_info)
# Display model results if available
if model_results_df is not None:
# Display the model results dataframe
st.write(model_results_df)
# STEP 3: MODEL ANALYSIS AND LEADERBOARD
st.markdown("---")
st.markdown("### Step 3: Model Analysis & Leaderboard")
# Generate model analysis using LLM
with st.spinner("Analyzing model results..."):
analysis_prompt = build_model_analysis_prompt(model_results_df, selected_column)
analysis_results = get_llm_analysis_response(analysis_prompt)
# Display leaderboard and analysis
create_leaderboard_ui(model_results_df, analysis_results)
display_model_analysis(analysis_results)
else:
st.error("Model training failed. Check the execution info for details.")