File size: 9,548 Bytes
13fbfb8 65aa4e2 13fbfb8 99241c8 13fbfb8 715848f |
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
import streamlit as st
import pandas as pd
import os
from crewai import Crew
from langchain_groq import ChatGroq
import streamlit_ace as st_ace
import traceback
import contextlib
import io
from crewai_tools import FileReadTool
import matplotlib.pyplot as plt
import glob
from dotenv import load_dotenv
from autotabml_agents import initialize_agents
from autotabml_tasks import create_tasks
TEMP_DIR = "temp_dir"
OUTPUT_DIR = "Output_dir"
# Ensure the temporary directory exists
if not os.path.exists(TEMP_DIR):
os.makedirs(TEMP_DIR)
# Ensure the Output directory exits
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
# Function to save uploaded file
def save_uploaded_file(uploaded_file):
file_path = os.path.join(TEMP_DIR, uploaded_file.name)
with open(file_path, 'wb') as f:
f.write(uploaded_file.getbuffer())
return file_path
# load the .env file
load_dotenv()
# Set up Groq API key
groq_api_key = os.environ.get("GROQ_API_KEY") # os.environ["GROQ_API_KEY"] =
def main():
# Set custom CSS for UI
set_custom_css()
# Initialize session state for edited code
if 'edited_code' not in st.session_state:
st.session_state['edited_code'] = ""
# Initialize session state for whether the initial code is generated
if 'code_generated' not in st.session_state:
st.session_state['code_generated'] = False
# Header with futuristic design
st.markdown("""
<div class="header">
<h1>AutoTabML</h1>
<p>Automated Machine Learning Code Generation for Tabluar Data</p>
</div>
""", unsafe_allow_html=True)
# Sidebar for customization options
st.sidebar.title('LLM Model')
model = st.sidebar.selectbox(
'Model',
["llama3-70b-8192"]
)
# Initialize LLM
llm = initialize_llm(model)
# User inputs
user_question = st.text_area("Describe your ML problem:", key="user_question")
uploaded_file = st.file_uploader("Upload a sample .csv of your data", key="uploaded_file")
try:
file_name = uploaded_file.name
except:
file_name = "dataset.csv"
# Initialize agents
agents = initialize_agents(llm,file_name,TEMP_DIR)
# Process uploaded file
if uploaded_file:
try:
file_path = save_uploaded_file(uploaded_file)
df = pd.read_csv(uploaded_file)
st.write("Data successfully uploaded:")
st.dataframe(df.head())
data_upload = True
except Exception as e:
st.error(f"Error reading the file: {e}")
data_upload = False
else:
df = None
data_upload = False
# Process button
if st.button('Process'):
tasks = create_tasks("Process",user_question,file_name, data_upload, df, None, st.session_state['edited_code'], None, agents)
with st.spinner('Processing...'):
crew = Crew(
agents=list(agents.values()),
tasks=tasks,
verbose=2
)
result = crew.kickoff()
if result: # Only call st_ace if code has a valid value
code = result.strip("```")
try:
filt_idx = code.index("```")
code = code[:filt_idx]
except:
pass
st.session_state['edited_code'] = code
st.session_state['code_generated'] = True
st.session_state['edited_code'] = st_ace.st_ace(
value=st.session_state['edited_code'],
language='python',
theme='monokai',
keybinding='vscode',
min_lines=20,
max_lines=50
)
if st.session_state['code_generated']:
# Show options for modification, debugging, and running the code
suggestion = st.text_area("Suggest modifications to the generated code (optional):", key="suggestion")
if st.button('Modify'):
if st.session_state['edited_code'] and suggestion:
tasks = create_tasks("Modify",user_question,file_name, data_upload, df, suggestion, st.session_state['edited_code'], None, agents)
with st.spinner('Modifying code...'):
crew = Crew(
agents=list(agents.values()),
tasks=tasks,
verbose=2
)
result = crew.kickoff()
if result: # Only call st_ace if code has a valid value
code = result.strip("```")
try:
filter_idx = code.index("```")
code = code[:filter_idx]
except:
pass
st.session_state['edited_code'] = code
st.write("Modified code:")
st.session_state['edited_code']= st_ace.st_ace(
value=st.session_state['edited_code'],
language='python',
theme='monokai',
keybinding='vscode',
min_lines=20,
max_lines=50
)
debugger = st.text_area("Paste error message here for debugging (optional):", key="debugger")
if st.button('Debug'):
if st.session_state['edited_code'] and debugger:
tasks = create_tasks("Debug",user_question,file_name, data_upload, df, None, st.session_state['edited_code'], debugger, agents)
with st.spinner('Debugging code...'):
crew = Crew(
agents=list(agents.values()),
tasks=tasks,
verbose=2
)
result = crew.kickoff()
if result: # Only call st_ace if code has a valid value
code = result.strip("```")
try:
filter_idx = code.index("```")
code = code[:filter_idx]
except:
pass
st.session_state['edited_code'] = code
st.write("Debugged code:")
st.session_state['edited_code'] = st_ace.st_ace(
value=st.session_state['edited_code'],
language='python',
theme='monokai',
keybinding='vscode',
min_lines=20,
max_lines=50
)
if st.button('Run'):
output = io.StringIO()
with contextlib.redirect_stdout(output):
try:
globals().update({'dataset': df})
final_code = st.session_state["edited_code"]
with st.expander("Final Code"):
st.code(final_code, language='python')
exec(final_code, globals())
result = output.getvalue()
success = True
except Exception as e:
result = str(e)
success = False
st.subheader('Output:')
st.text(result)
figs = [manager.canvas.figure for manager in plt._pylab_helpers.Gcf.get_all_fig_managers()]
if figs:
st.subheader('Generated Plots:')
for fig in figs:
st.pyplot(fig)
if success:
st.success("Code executed successfully!")
else:
st.error("Code execution failed! Waiting for debugging input...")
# Move the generated files section to the sidebar
with st.sidebar:
st.header('Output_dir :')
files = glob.glob(os.path.join(OUTPUT_DIR, '*'))
for file in files:
if os.path.isfile(file):
with open(file, 'rb') as f:
st.download_button(label=f'Download {os.path.basename(file)}', data=f, file_name=os.path.basename(file))
# Function to set custom CSS for futuristic UI
def set_custom_css():
st.markdown("""
<style>
body {
background: #0e0e0e;
color: #e0e0e0;
font-family: 'Roboto', sans-serif;
}
.header {
background: linear-gradient(135deg, #6e3aff, #b839ff);
padding: 10px;
border-radius: 10px;
}
.header h1, .header p {
color: white;
text-align: center;
}
.stButton button {
background-color: #b839ff;
color: white;
border-radius: 10px;
font-size: 16px;
padding: 10px 20px;
}
.stButton button:hover {
background-color: #6e3aff;
color: #e0e0e0;
}
.spinner {
display: flex;
justify-content: center;
align-items: center;
}
</style>
""", unsafe_allow_html=True)
# Function to initialize LLM
def initialize_llm(model):
return ChatGroq(
temperature=0,
groq_api_key=groq_api_key,
model_name=model
)
#main function
if __name__ == "__main__":
main() |