DrishtiSharma commited on
Commit
ac04073
Β·
verified Β·
1 Parent(s): 04aac68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -36
app.py CHANGED
@@ -111,7 +111,7 @@ if st.session_state.df is not None:
111
  """Validate the SQL query syntax and structure before execution."""
112
  return QuerySQLCheckerTool(db=db, llm=llm).invoke({"query": sql_query})
113
 
114
- # Agent for SQL data extraction
115
  sql_dev = Agent(
116
  role="Senior Database Developer",
117
  goal="Extract data using optimized SQL queries.",
@@ -120,7 +120,6 @@ if st.session_state.df is not None:
120
  tools=[list_tables, tables_schema, execute_sql, check_sql],
121
  )
122
 
123
- # Agent for data analysis
124
  data_analyst = Agent(
125
  role="Senior Data Analyst",
126
  goal="Analyze the data and produce insights.",
@@ -128,23 +127,21 @@ if st.session_state.df is not None:
128
  llm=llm,
129
  )
130
 
131
- # Agent for generating the main report (without Conclusion)
132
  report_writer = Agent(
133
  role="Technical Report Writer",
134
- goal="Summarize the analysis into a structured report with Introduction, Key Insights, and Analysis. DO NOT include any conclusion or summary.",
135
- backstory="Markdown report excluding Conclusion and Summary.",
136
  llm=llm,
137
  )
138
 
139
- # New Agent for generating ONLY the Conclusion
140
  conclusion_writer = Agent(
141
  role="Conclusion Specialist",
142
- goal="Summarize findings into a clear and concise Conclusion/Summary section.",
143
- backstory="An expert in crafting well-structured and insightful conclusions.",
144
  llm=llm,
145
  )
146
 
147
- # Tasks for each agent
148
  extract_data = Task(
149
  description="Extract data based on the query: {query}.",
150
  expected_output="Database results matching the query.",
@@ -153,57 +150,61 @@ if st.session_state.df is not None:
153
 
154
  analyze_data = Task(
155
  description="Analyze the extracted data for query: {query}.",
156
- expected_output="Provide ONLY Key Insights and Analysis. Exclude Introduction and Conclusion.",
157
  agent=data_analyst,
158
  context=[extract_data],
159
  )
160
 
161
  write_report = Task(
162
- description="Write the report with ONLY Key Insights and Analysis. DO NOT include Introduction or Conclusion.",
163
- expected_output="Markdown report excluding Introduction and Conclusion.",
164
  agent=report_writer,
165
  context=[analyze_data],
166
  )
167
 
168
  write_conclusion = Task(
169
- description="Summarize the findings into a concise Conclusion.",
170
- expected_output="Markdown-formatted Conclusion section.",
171
  agent=conclusion_writer,
172
  context=[analyze_data],
173
  )
174
 
175
- # Crew setup
176
- crew = Crew(
177
- agents=[sql_dev, data_analyst, report_writer, conclusion_writer],
178
- tasks=[extract_data, analyze_data, write_report, write_conclusion],
179
  process=Process.sequential,
180
  verbose=True,
181
  )
182
 
183
- # Tabs for Query Results and General Insights
 
 
 
 
 
 
 
184
  tab1, tab2 = st.tabs(["πŸ” Query Insights + Viz", "πŸ“Š Full Data Viz"])
185
 
186
- # Tab 1: Query-Insights + Visualization
187
  with tab1:
188
  query = st.text_area("Enter Query:", value="Provide insights into the salary of a Principal Data Scientist.")
189
  if st.button("Submit Query"):
190
  with st.spinner("Processing query..."):
191
- # Step 1: Generate Report without Conclusion
192
- report_inputs = {"query": query + " Provide a detailed analysis but DO NOT include a Conclusion."}
193
- report_result = crew.kickoff(inputs=report_inputs)
194
-
195
- # Step 2: Generate only the Conclusion
196
- conclusion_inputs = {"query": query + " Now, provide only the Conclusion for this analysis."}
197
- conclusion_result = crew.kickoff(inputs=conclusion_inputs)
198
 
199
- # Directly use the outputs
200
- main_report = report_result if report_result else "⚠️ No Report Generated."
201
- conclusion = conclusion_result if conclusion_result else "⚠️ No Conclusion Generated."
202
 
 
203
  st.markdown("### Analysis Report:")
204
- st.markdown(main_report)
205
 
206
- # Step 3: Generate relevant visualizations
207
  visualizations = []
208
 
209
  fig_salary = px.box(st.session_state.df, x="job_title", y="salary_in_usd",
@@ -221,16 +222,16 @@ if st.session_state.df is not None:
221
  title="Salary Distribution by Employment Type")
222
  visualizations.append(fig_employment)
223
 
224
- # Step 4: Insert Visual Insights
225
  st.markdown("## πŸ“Š Visual Insights")
226
  for fig in visualizations:
227
  st.plotly_chart(fig, use_container_width=True)
228
 
229
- # Step 5: Append the Conclusion
230
  st.markdown("## Conclusion")
231
- st.markdown(conclusion)
232
 
233
- # Tab 2: Full Data Visualization
234
  with tab2:
235
  st.subheader("πŸ“Š Comprehensive Data Visualizations")
236
 
 
111
  """Validate the SQL query syntax and structure before execution."""
112
  return QuerySQLCheckerTool(db=db, llm=llm).invoke({"query": sql_query})
113
 
114
+ # Agents for SQL data extraction and analysis
115
  sql_dev = Agent(
116
  role="Senior Database Developer",
117
  goal="Extract data using optimized SQL queries.",
 
120
  tools=[list_tables, tables_schema, execute_sql, check_sql],
121
  )
122
 
 
123
  data_analyst = Agent(
124
  role="Senior Data Analyst",
125
  goal="Analyze the data and produce insights.",
 
127
  llm=llm,
128
  )
129
 
 
130
  report_writer = Agent(
131
  role="Technical Report Writer",
132
+ goal="Write a structured report with Key Insights and Analysis. DO NOT include Introduction or Conclusion.",
133
+ backstory="Specializes in detailed analytical reports without conclusions.",
134
  llm=llm,
135
  )
136
 
 
137
  conclusion_writer = Agent(
138
  role="Conclusion Specialist",
139
+ goal="Summarize findings into a clear and concise 3-5 line Conclusion highlighting only the most important insights.",
140
+ backstory="An expert in crafting impactful and clear conclusions.",
141
  llm=llm,
142
  )
143
 
144
+ # Define tasks for report and conclusion
145
  extract_data = Task(
146
  description="Extract data based on the query: {query}.",
147
  expected_output="Database results matching the query.",
 
150
 
151
  analyze_data = Task(
152
  description="Analyze the extracted data for query: {query}.",
153
+ expected_output="Key Insights and Analysis without any Introduction or Conclusion.",
154
  agent=data_analyst,
155
  context=[extract_data],
156
  )
157
 
158
  write_report = Task(
159
+ description="Write the analysis report with Key Insights. DO NOT include a Conclusion.",
160
+ expected_output="Markdown-formatted report excluding Conclusion.",
161
  agent=report_writer,
162
  context=[analyze_data],
163
  )
164
 
165
  write_conclusion = Task(
166
+ description="Write a brief and impactful 3-5 line Conclusion summarizing only the most important insights.",
167
+ expected_output="Markdown-formatted concise Conclusion section.",
168
  agent=conclusion_writer,
169
  context=[analyze_data],
170
  )
171
 
172
+ # Separate Crews for report and conclusion
173
+ crew_report = Crew(
174
+ agents=[sql_dev, data_analyst, report_writer],
175
+ tasks=[extract_data, analyze_data, write_report],
176
  process=Process.sequential,
177
  verbose=True,
178
  )
179
 
180
+ crew_conclusion = Crew(
181
+ agents=[data_analyst, conclusion_writer],
182
+ tasks=[write_conclusion],
183
+ process=Process.sequential,
184
+ verbose=True,
185
+ )
186
+
187
+ # Tabs for Query Results and Visualizations
188
  tab1, tab2 = st.tabs(["πŸ” Query Insights + Viz", "πŸ“Š Full Data Viz"])
189
 
190
+ # Query Insights + Visualization
191
  with tab1:
192
  query = st.text_area("Enter Query:", value="Provide insights into the salary of a Principal Data Scientist.")
193
  if st.button("Submit Query"):
194
  with st.spinner("Processing query..."):
195
+ # Step 1: Generate the analysis report
196
+ report_inputs = {"query": query + " Provide detailed analysis but DO NOT include Conclusion."}
197
+ report_result = crew_report.kickoff(inputs=report_inputs)
 
 
 
 
198
 
199
+ # Step 2: Generate only the concise conclusion
200
+ conclusion_inputs = {"query": query + " Provide ONLY the most important insights in 3-5 concise lines."}
201
+ conclusion_result = crew_conclusion.kickoff(inputs=conclusion_inputs)
202
 
203
+ # Step 3: Display the report
204
  st.markdown("### Analysis Report:")
205
+ st.markdown(report_result if report_result else "⚠️ No Report Generated.")
206
 
207
+ # Step 4: Generate Visualizations
208
  visualizations = []
209
 
210
  fig_salary = px.box(st.session_state.df, x="job_title", y="salary_in_usd",
 
222
  title="Salary Distribution by Employment Type")
223
  visualizations.append(fig_employment)
224
 
225
+ # Step 5: Insert Visual Insights
226
  st.markdown("## πŸ“Š Visual Insights")
227
  for fig in visualizations:
228
  st.plotly_chart(fig, use_container_width=True)
229
 
230
+ # Step 6: Display Concise Conclusion
231
  st.markdown("## Conclusion")
232
+ st.markdown(conclusion_result if conclusion_result else "⚠️ No Conclusion Generated.")
233
 
234
+ # Full Data Visualization Tab
235
  with tab2:
236
  st.subheader("πŸ“Š Comprehensive Data Visualizations")
237