Shalu Singh
commited on
Commit
·
dcf932e
1
Parent(s):
b0f3209
Application file Initialization
Browse files- .gitignore +1 -0
- requirements.txt +13 -0
- working.py +415 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|
3 |
+
os
|
4 |
+
crewai
|
5 |
+
langchain_groq
|
6 |
+
streamlit_ace
|
7 |
+
traceback
|
8 |
+
contextlib
|
9 |
+
io
|
10 |
+
crewai_tools
|
11 |
+
matplotlib
|
12 |
+
glob
|
13 |
+
dotenv
|
working.py
ADDED
@@ -0,0 +1,415 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import os
|
4 |
+
from crewai import Agent, Task, Crew
|
5 |
+
from langchain_groq import ChatGroq
|
6 |
+
import streamlit_ace as st_ace
|
7 |
+
import traceback
|
8 |
+
import contextlib
|
9 |
+
import io
|
10 |
+
from crewai_tools import FileReadTool
|
11 |
+
import matplotlib.pyplot as plt
|
12 |
+
import glob
|
13 |
+
from dotenv import load_dotenv
|
14 |
+
|
15 |
+
# load the .env file
|
16 |
+
load_dotenv()
|
17 |
+
# Set up Groq API key
|
18 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
19 |
+
|
20 |
+
|
21 |
+
def main():
|
22 |
+
# Set custom CSS for UI
|
23 |
+
set_custom_css()
|
24 |
+
|
25 |
+
# Initialize session state for edited code
|
26 |
+
if 'edited_code' not in st.session_state:
|
27 |
+
st.session_state['edited_code'] = ""
|
28 |
+
|
29 |
+
# Initialize session state for whether the initial code is generated
|
30 |
+
if 'code_generated' not in st.session_state:
|
31 |
+
st.session_state['code_generated'] = False
|
32 |
+
|
33 |
+
# Header with futuristic design
|
34 |
+
st.markdown("""
|
35 |
+
<div class="header">
|
36 |
+
<h1>CrewAI Machine Learning Assistant</h1>
|
37 |
+
<p>Your AI-powered partner for machine learning projects.</p>
|
38 |
+
</div>
|
39 |
+
""", unsafe_allow_html=True)
|
40 |
+
|
41 |
+
# Sidebar for customization options
|
42 |
+
st.sidebar.title('Customization')
|
43 |
+
model = st.sidebar.selectbox(
|
44 |
+
'Choose a model',
|
45 |
+
['llama3-8b-8192', "llama3-70b-8192"]
|
46 |
+
)
|
47 |
+
|
48 |
+
# Initialize LLM
|
49 |
+
llm = initialize_llm(model)
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
# User inputs
|
54 |
+
user_question = st.text_area("Describe your ML problem:", key="user_question")
|
55 |
+
uploaded_file = st.file_uploader("Upload a sample .csv of your data (optional)", key="uploaded_file")
|
56 |
+
try:
|
57 |
+
file_name = uploaded_file.name
|
58 |
+
except:
|
59 |
+
file_name = "dataset.csv"
|
60 |
+
|
61 |
+
# Initialize agents
|
62 |
+
agents = initialize_agents(llm,file_name)
|
63 |
+
# Process uploaded file
|
64 |
+
if uploaded_file:
|
65 |
+
try:
|
66 |
+
df = pd.read_csv(uploaded_file)
|
67 |
+
st.write("Data successfully uploaded:")
|
68 |
+
st.dataframe(df.head())
|
69 |
+
data_upload = True
|
70 |
+
except Exception as e:
|
71 |
+
st.error(f"Error reading the file: {e}")
|
72 |
+
data_upload = False
|
73 |
+
else:
|
74 |
+
df = None
|
75 |
+
data_upload = False
|
76 |
+
|
77 |
+
# Process button
|
78 |
+
if st.button('Process'):
|
79 |
+
tasks = create_tasks("Process",user_question,file_name, data_upload, df, None, st.session_state['edited_code'], None, agents)
|
80 |
+
with st.spinner('Processing...'):
|
81 |
+
crew = Crew(
|
82 |
+
agents=list(agents.values()),
|
83 |
+
tasks=tasks,
|
84 |
+
verbose=2
|
85 |
+
)
|
86 |
+
|
87 |
+
result = crew.kickoff()
|
88 |
+
|
89 |
+
if result: # Only call st_ace if code has a valid value
|
90 |
+
code = result.strip("```")
|
91 |
+
try:
|
92 |
+
filt_idx = code.index("```")
|
93 |
+
code = code[:filt_idx]
|
94 |
+
except:
|
95 |
+
pass
|
96 |
+
st.session_state['edited_code'] = code
|
97 |
+
st.session_state['code_generated'] = True
|
98 |
+
|
99 |
+
st.session_state['edited_code'] = st_ace.st_ace(
|
100 |
+
value=st.session_state['edited_code'],
|
101 |
+
language='python',
|
102 |
+
theme='monokai',
|
103 |
+
keybinding='vscode',
|
104 |
+
min_lines=20,
|
105 |
+
max_lines=50
|
106 |
+
)
|
107 |
+
|
108 |
+
if st.session_state['code_generated']:
|
109 |
+
# Show options for modification, debugging, and running the code
|
110 |
+
suggestion = st.text_area("Suggest modifications to the generated code (optional):", key="suggestion")
|
111 |
+
if st.button('Modify'):
|
112 |
+
if st.session_state['edited_code'] and suggestion:
|
113 |
+
tasks = create_tasks("Modify",user_question,file_name, data_upload, df, suggestion, st.session_state['edited_code'], None, agents)
|
114 |
+
with st.spinner('Modifying code...'):
|
115 |
+
crew = Crew(
|
116 |
+
agents=list(agents.values()),
|
117 |
+
tasks=tasks,
|
118 |
+
verbose=2
|
119 |
+
)
|
120 |
+
|
121 |
+
result = crew.kickoff()
|
122 |
+
|
123 |
+
if result: # Only call st_ace if code has a valid value
|
124 |
+
code = result.strip("```")
|
125 |
+
try:
|
126 |
+
filter_idx = code.index("```")
|
127 |
+
code = code[:filter_idx]
|
128 |
+
except:
|
129 |
+
pass
|
130 |
+
st.session_state['edited_code'] = code
|
131 |
+
|
132 |
+
st.write("Modified code:")
|
133 |
+
st.session_state['edited_code']= st_ace.st_ace(
|
134 |
+
value=st.session_state['edited_code'],
|
135 |
+
language='python',
|
136 |
+
theme='monokai',
|
137 |
+
keybinding='vscode',
|
138 |
+
min_lines=20,
|
139 |
+
max_lines=50
|
140 |
+
)
|
141 |
+
|
142 |
+
debugger = st.text_area("Paste error message here for debugging (optional):", key="debugger")
|
143 |
+
if st.button('Debug'):
|
144 |
+
if st.session_state['edited_code'] and debugger:
|
145 |
+
tasks = create_tasks("Debug",user_question,file_name, data_upload, df, None, st.session_state['edited_code'], debugger, agents)
|
146 |
+
with st.spinner('Debugging code...'):
|
147 |
+
crew = Crew(
|
148 |
+
agents=list(agents.values()),
|
149 |
+
tasks=tasks,
|
150 |
+
verbose=2
|
151 |
+
)
|
152 |
+
|
153 |
+
result = crew.kickoff()
|
154 |
+
|
155 |
+
if result: # Only call st_ace if code has a valid value
|
156 |
+
code = result.strip("```")
|
157 |
+
try:
|
158 |
+
filter_idx = code.index("```")
|
159 |
+
code = code[:filter_idx]
|
160 |
+
except:
|
161 |
+
pass
|
162 |
+
st.session_state['edited_code'] = code
|
163 |
+
|
164 |
+
st.write("Debugged code:")
|
165 |
+
st.session_state['edited_code'] = st_ace.st_ace(
|
166 |
+
value=st.session_state['edited_code'],
|
167 |
+
language='python',
|
168 |
+
theme='monokai',
|
169 |
+
keybinding='vscode',
|
170 |
+
min_lines=20,
|
171 |
+
max_lines=50
|
172 |
+
)
|
173 |
+
|
174 |
+
if st.button('Run'):
|
175 |
+
output = io.StringIO()
|
176 |
+
with contextlib.redirect_stdout(output):
|
177 |
+
try:
|
178 |
+
globals().update({'dataset': df})
|
179 |
+
final_code = st.session_state["edited_code"]
|
180 |
+
|
181 |
+
with st.expander("Final Code"):
|
182 |
+
st.code(final_code, language='python')
|
183 |
+
|
184 |
+
exec(final_code, globals())
|
185 |
+
result = output.getvalue()
|
186 |
+
success = True
|
187 |
+
except Exception as e:
|
188 |
+
result = str(e)
|
189 |
+
success = False
|
190 |
+
|
191 |
+
st.subheader('Output:')
|
192 |
+
st.text(result)
|
193 |
+
|
194 |
+
figs = [manager.canvas.figure for manager in plt._pylab_helpers.Gcf.get_all_fig_managers()]
|
195 |
+
if figs:
|
196 |
+
st.subheader('Generated Plots:')
|
197 |
+
for fig in figs:
|
198 |
+
st.pyplot(fig)
|
199 |
+
|
200 |
+
if success:
|
201 |
+
st.success("Code executed successfully!")
|
202 |
+
else:
|
203 |
+
st.error("Code execution failed! Waiting for debugging input...")
|
204 |
+
|
205 |
+
# Move the generated files section to the sidebar
|
206 |
+
with st.sidebar:
|
207 |
+
st.header('Output Files:')
|
208 |
+
files = glob.glob(os.path.join("Output/", '*'))
|
209 |
+
for file in files:
|
210 |
+
if os.path.isfile(file):
|
211 |
+
with open(file, 'rb') as f:
|
212 |
+
st.download_button(label=f'Download {os.path.basename(file)}', data=f, file_name=os.path.basename(file))
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
# Function to set custom CSS for futuristic UI
|
217 |
+
def set_custom_css():
|
218 |
+
st.markdown("""
|
219 |
+
<style>
|
220 |
+
body {
|
221 |
+
background: #0e0e0e;
|
222 |
+
color: #e0e0e0;
|
223 |
+
font-family: 'Roboto', sans-serif;
|
224 |
+
}
|
225 |
+
.header {
|
226 |
+
background: linear-gradient(135deg, #6e3aff, #b839ff);
|
227 |
+
padding: 10px;
|
228 |
+
border-radius: 10px;
|
229 |
+
}
|
230 |
+
.header h1, .header p {
|
231 |
+
color: white;
|
232 |
+
text-align: center;
|
233 |
+
}
|
234 |
+
.stButton button {
|
235 |
+
background-color: #b839ff;
|
236 |
+
color: white;
|
237 |
+
border-radius: 10px;
|
238 |
+
font-size: 16px;
|
239 |
+
padding: 10px 20px;
|
240 |
+
}
|
241 |
+
.stButton button:hover {
|
242 |
+
background-color: #6e3aff;
|
243 |
+
color: #e0e0e0;
|
244 |
+
}
|
245 |
+
.spinner {
|
246 |
+
display: flex;
|
247 |
+
justify-content: center;
|
248 |
+
align-items: center;
|
249 |
+
}
|
250 |
+
</style>
|
251 |
+
""", unsafe_allow_html=True)
|
252 |
+
|
253 |
+
# Function to initialize LLM
|
254 |
+
def initialize_llm(model):
|
255 |
+
return ChatGroq(
|
256 |
+
temperature=0,
|
257 |
+
groq_api_key=groq_api_key,
|
258 |
+
model_name=model
|
259 |
+
)
|
260 |
+
|
261 |
+
# Function to initialize agents
|
262 |
+
def initialize_agents(llm,file_name):
|
263 |
+
file_read_tool = FileReadTool()
|
264 |
+
return {
|
265 |
+
"Data_Reader_Agent": Agent(
|
266 |
+
role='Data_Reader_Agent',
|
267 |
+
goal="Read the uploaded dataset and provide it to other agents.",
|
268 |
+
backstory="Responsible for reading the uploaded dataset.",
|
269 |
+
verbose=True,
|
270 |
+
allow_delegation=False,
|
271 |
+
llm=llm,
|
272 |
+
tools=[file_read_tool]
|
273 |
+
),
|
274 |
+
"Problem_Definition_Agent": Agent(
|
275 |
+
role='Problem_Definition_Agent',
|
276 |
+
goal="Clarify the machine learning problem the user wants to solve.",
|
277 |
+
backstory="Expert in defining machine learning problems.",
|
278 |
+
verbose=True,
|
279 |
+
allow_delegation=False,
|
280 |
+
llm=llm,
|
281 |
+
),
|
282 |
+
"EDA_Agent": Agent(
|
283 |
+
role='EDA_Agent',
|
284 |
+
goal="Perform all possible Exploratory Data Analysis (EDA) on the data provided by the user.",
|
285 |
+
backstory="Specializes in conducting comprehensive EDA to understand the data characteristics, distributions, and relationships.",
|
286 |
+
verbose=True,
|
287 |
+
allow_delegation=False,
|
288 |
+
llm=llm,
|
289 |
+
),
|
290 |
+
"Feature_Engineering_Agent": Agent(
|
291 |
+
role='Feature_Engineering_Agent',
|
292 |
+
goal="Perform feature engineering on the data based on the EDA results provided by the EDA agent.",
|
293 |
+
backstory="Expert in deriving new features, transforming existing features, and preprocessing data to prepare it for modeling.",
|
294 |
+
verbose=True,
|
295 |
+
allow_delegation=False,
|
296 |
+
llm=llm,
|
297 |
+
),
|
298 |
+
"Model_Recommendation_Agent": Agent(
|
299 |
+
role='Model_Recommendation_Agent',
|
300 |
+
goal="Suggest the most suitable machine learning models.",
|
301 |
+
backstory="Expert in recommending machine learning algorithms.",
|
302 |
+
verbose=True,
|
303 |
+
allow_delegation=False,
|
304 |
+
llm=llm,
|
305 |
+
),
|
306 |
+
"Starter_Code_Generator_Agent": Agent(
|
307 |
+
role='Starter_Code_Generator_Agent',
|
308 |
+
goal=f"Generate starter Python code for the project. Always give dataset name as {file_name}",
|
309 |
+
backstory="Code wizard for generating starter code templates.",
|
310 |
+
verbose=True,
|
311 |
+
allow_delegation=False,
|
312 |
+
llm=llm,
|
313 |
+
),
|
314 |
+
"Code_Modification_Agent": Agent(
|
315 |
+
role='Code_Modification_Agent',
|
316 |
+
goal="Modify the generated Python code based on user suggestions.",
|
317 |
+
backstory="Expert in adapting code according to user feedback.",
|
318 |
+
verbose=True,
|
319 |
+
allow_delegation=False,
|
320 |
+
llm=llm,
|
321 |
+
),
|
322 |
+
# "Code_Runner_Agent": Agent(
|
323 |
+
# role='Code_Runner_Agent',
|
324 |
+
# goal="Run the generated Python code and catch any errors.",
|
325 |
+
# backstory="Debugging expert.",
|
326 |
+
# verbose=True,
|
327 |
+
# allow_delegation=True,
|
328 |
+
# llm=llm,
|
329 |
+
# ),
|
330 |
+
"Code_Debugger_Agent": Agent(
|
331 |
+
role='Code_Debugger_Agent',
|
332 |
+
goal="Debug the generated Python code.",
|
333 |
+
backstory="Seasoned code debugger.",
|
334 |
+
verbose=True,
|
335 |
+
allow_delegation=False,
|
336 |
+
llm=llm,
|
337 |
+
),
|
338 |
+
"Compiler_Agent":Agent(
|
339 |
+
role = "Code_compiler",
|
340 |
+
goal = "Extract only the python code.",
|
341 |
+
backstory = "You are the compiler which extract only the python code.",
|
342 |
+
verbose = True,
|
343 |
+
allow_delegation = False,
|
344 |
+
llm = llm
|
345 |
+
)
|
346 |
+
}
|
347 |
+
|
348 |
+
# Function to create tasks based on user inputs
|
349 |
+
def create_tasks(func_call,user_question,file_name, data_upload, df, suggestion, edited_code, debugger, agents):
|
350 |
+
info = df.info()
|
351 |
+
tasks = []
|
352 |
+
if(func_call == "Process"):
|
353 |
+
tasks.append(Task(
|
354 |
+
description=f"Clarify the ML problem: {user_question}",
|
355 |
+
agent=agents["Problem_Definition_Agent"],
|
356 |
+
expected_output="A clear and concise definition of the ML problem."
|
357 |
+
)
|
358 |
+
)
|
359 |
+
|
360 |
+
if data_upload:
|
361 |
+
tasks.extend([
|
362 |
+
Task(
|
363 |
+
description=f"Evaluate the data provided by the file name . This is the data: {df}",
|
364 |
+
agent=agents["EDA_Agent"],
|
365 |
+
expected_output="An assessment of the EDA and preprocessing like dataset info, missing value, duplication, outliers etc. on the data provided"
|
366 |
+
),
|
367 |
+
Task(
|
368 |
+
description=f"Feature Engineering on data {df} based on EDA output: {info}",
|
369 |
+
agent=agents["Feature_Engineering_Agent"],
|
370 |
+
expected_output="An assessment of the Featuring Engineering and preprocessing like handling missing values, handling duplication, handling outliers, feature encoding, feature scaling etc. on the data provided"
|
371 |
+
)
|
372 |
+
])
|
373 |
+
|
374 |
+
tasks.extend([
|
375 |
+
Task(
|
376 |
+
description="Suggest suitable ML models.",
|
377 |
+
agent=agents["Model_Recommendation_Agent"],
|
378 |
+
expected_output="A list of suitable ML models."
|
379 |
+
),
|
380 |
+
Task(
|
381 |
+
description=f"Generate starter Python code based on feature engineering, where column names are {df.columns.tolist()}. Generate only the code without any extra text",
|
382 |
+
agent=agents["Starter_Code_Generator_Agent"],
|
383 |
+
expected_output="Starter Python code."
|
384 |
+
),
|
385 |
+
])
|
386 |
+
if(func_call == "Modify"):
|
387 |
+
if suggestion:
|
388 |
+
tasks.append(
|
389 |
+
Task(
|
390 |
+
description=f"Modify the already generated code {edited_code} according to the suggestion: {suggestion} \n\n Do not generate entire new code.",
|
391 |
+
agent=agents["Code_Modification_Agent"],
|
392 |
+
expected_output="Modified code."
|
393 |
+
)
|
394 |
+
)
|
395 |
+
if(func_call == "Debug"):
|
396 |
+
if debugger:
|
397 |
+
tasks.append(
|
398 |
+
Task(
|
399 |
+
description=f"Debug and fix any errors for data with column names {df.columns.tolist()} with data as {df} in the generated code: {edited_code} \n\n According to the debugging: {debugger}. \n\n Do not generate entire new code. Just remove the error in the code by modifying only necessary parts of the code.",
|
400 |
+
agent=agents["Code_Debugger_Agent"],
|
401 |
+
expected_output="Debugged and successfully executed code."
|
402 |
+
)
|
403 |
+
)
|
404 |
+
tasks.append(
|
405 |
+
Task(
|
406 |
+
description = "Your job is to only extract python code from string",
|
407 |
+
agent = agents["Compiler_Agent"],
|
408 |
+
expected_output = "Running python code."
|
409 |
+
)
|
410 |
+
)
|
411 |
+
|
412 |
+
return tasks
|
413 |
+
|
414 |
+
if __name__ == "__main__":
|
415 |
+
main()
|