Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -149,17 +149,73 @@ if st.session_state.df is not None:
|
|
149 |
verbose=True,
|
150 |
)
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
temp_dir.cleanup()
|
161 |
else:
|
162 |
-
st.info("
|
163 |
|
164 |
with st.sidebar:
|
165 |
# st.markdown("---")
|
|
|
149 |
verbose=True,
|
150 |
)
|
151 |
|
152 |
+
# SQL-RAG Analysis
|
153 |
+
if st.session_state.df is not None:
|
154 |
+
temp_dir = tempfile.TemporaryDirectory()
|
155 |
+
db_path = os.path.join(temp_dir.name, "data.db")
|
156 |
+
connection = sqlite3.connect(db_path)
|
157 |
+
st.session_state.df.to_sql("salaries", connection, if_exists="replace", index=False)
|
158 |
+
db = SQLDatabase.from_uri(f"sqlite:///{db_path}")
|
159 |
+
|
160 |
+
@tool("execute_sql")
|
161 |
+
def execute_sql(sql_query: str) -> str:
|
162 |
+
return QuerySQLDataBaseTool(db=db).invoke(sql_query)
|
163 |
+
|
164 |
+
sql_dev = Agent("SQL Developer", "Optimize SQL queries for data extraction.", llm, tools=[execute_sql])
|
165 |
+
data_analyst = Agent("Data Analyst", "Analyze data and generate insights.", llm)
|
166 |
+
report_writer = Agent("Report Writer", "Summarize analysis into reports.", llm)
|
167 |
+
|
168 |
+
extract_data = Task("Extract data for: {query}.", "Query results.", sql_dev)
|
169 |
+
analyze_data = Task("Analyze extracted data for: {query}.", "Insights.", data_analyst, context=[extract_data])
|
170 |
+
write_report = Task("Generate a report for: {query}.", "Summary report.", report_writer, context=[analyze_data])
|
171 |
+
|
172 |
+
crew = Crew([sql_dev, data_analyst, report_writer], [extract_data, analyze_data, write_report], Process.sequential)
|
173 |
+
|
174 |
+
# Tabs for UI
|
175 |
+
tab1, tab2 = st.tabs(["π Query Specific Insights + Viz", "π Full Data Insights"])
|
176 |
+
|
177 |
+
# Tab 1: Query-Specific Analysis + viz
|
178 |
+
with tab1:
|
179 |
+
query = st.text_area("Enter Query:", placeholder="e.g., 'What is the average salary for senior employees?'")
|
180 |
+
if st.button("Submit Query"):
|
181 |
+
with st.spinner("Processing query..."):
|
182 |
+
result = crew.kickoff(inputs={"query": query})
|
183 |
+
st.subheader("π Analysis Report")
|
184 |
+
st.markdown(result)
|
185 |
+
|
186 |
+
# Query-Specific Visualization
|
187 |
+
if "salary" in query.lower():
|
188 |
+
fig = px.box(st.session_state.df, x="job_title", y="salary_in_usd", title="Salary by Job Title")
|
189 |
+
st.plotly_chart(fig)
|
190 |
+
|
191 |
+
# Tab 2: Full Insights
|
192 |
+
with tab2:
|
193 |
+
st.subheader("π Comprehensive Data Visualizations")
|
194 |
+
|
195 |
+
# Salary by Job Title
|
196 |
+
fig1 = px.box(st.session_state.df, x="job_title", y="salary_in_usd", title="Salary Distribution by Job Title")
|
197 |
+
st.plotly_chart(fig1)
|
198 |
+
|
199 |
+
# Average Salary by Experience Level
|
200 |
+
fig2 = px.bar(st.session_state.df.groupby("experience_level")["salary_in_usd"].mean().reset_index(),
|
201 |
+
x="experience_level", y="salary_in_usd", title="Average Salary by Experience Level")
|
202 |
+
st.plotly_chart(fig2)
|
203 |
+
|
204 |
+
# Average Salary by Company Size
|
205 |
+
fig3 = px.bar(st.session_state.df.groupby("company_size")["salary_in_usd"].mean().reset_index(),
|
206 |
+
x="company_size", y="salary_in_usd", title="Average Salary by Company Size")
|
207 |
+
st.plotly_chart(fig3)
|
208 |
+
|
209 |
+
# Download CSV Report
|
210 |
+
if st.button("π₯ Download CSV Summary"):
|
211 |
+
summary = st.session_state.df.groupby("job_title")["salary_in_usd"].mean().reset_index()
|
212 |
+
summary.to_csv("summary.csv", index=False)
|
213 |
+
with open("summary.csv", "rb") as file:
|
214 |
+
st.download_button("Download CSV", file, "summary.csv", "text/csv")
|
215 |
|
216 |
temp_dir.cleanup()
|
217 |
else:
|
218 |
+
st.info("Upload a dataset to continue.")
|
219 |
|
220 |
with st.sidebar:
|
221 |
# st.markdown("---")
|