Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,55 @@
|
|
| 1 |
-
import
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
df = pd.read_csv(file.name) if file.name.endswith('.csv') else pd.read_excel(file.name)
|
| 19 |
-
agent = smolagent.SmolAgent()
|
| 20 |
-
insights = agent.run("Generate insights and report on this dataset", df)
|
| 21 |
-
return insights
|
| 22 |
-
except Exception as e:
|
| 23 |
-
return f"Error in generating insights: {str(e)}"
|
| 24 |
-
|
| 25 |
-
def visualize_data(file):
|
| 26 |
-
try:
|
| 27 |
-
df = pd.read_csv(file.name) if file.name.endswith('.csv') else pd.read_excel(file.name)
|
| 28 |
-
agent = smolagent.SmolAgent()
|
| 29 |
-
important_features = agent.run("Identify important features in this dataset", df)
|
| 30 |
-
|
| 31 |
-
if not isinstance(important_features, dict):
|
| 32 |
-
return "Error: Expected a dictionary of feature importance values."
|
| 33 |
-
|
| 34 |
-
fig, ax = plt.subplots(figsize=(8, 6))
|
| 35 |
-
sns.barplot(x=list(important_features.keys()), y=list(important_features.values()), ax=ax)
|
| 36 |
-
ax.set_title("Feature Importance")
|
| 37 |
-
plt.xticks(rotation=45)
|
| 38 |
-
return fig
|
| 39 |
-
except Exception as e:
|
| 40 |
-
return f"Error in visualization: {str(e)}"
|
| 41 |
-
|
| 42 |
-
with gr.Blocks() as demo:
|
| 43 |
-
gr.Markdown("## AI-Powered Data Analysis with SmolAgent")
|
| 44 |
-
file_input = gr.File(label="Upload CSV or Excel File")
|
| 45 |
-
|
| 46 |
-
preprocess_btn = gr.Button("Preprocess Data")
|
| 47 |
-
preprocess_output = gr.Textbox()
|
| 48 |
-
preprocess_btn.click(preprocess_data, inputs=file_input, outputs=preprocess_output)
|
| 49 |
-
|
| 50 |
-
insights_btn = gr.Button("Generate Insights")
|
| 51 |
-
insights_output = gr.Textbox()
|
| 52 |
-
insights_btn.click(generate_insights, inputs=file_input, outputs=insights_output)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from smolagents import DataCleanser, InsightGenerator, DataVisualizer
|
| 5 |
+
|
| 6 |
+
# Title of the app
|
| 7 |
+
st.title("Data Analysis with Hugging Face SmolAgents")
|
| 8 |
+
|
| 9 |
+
# Step 1: Create a user interface to receive data file
|
| 10 |
+
uploaded_file = st.file_uploader("Upload your data file (CSV or Excel)", type=["csv", "xlsx"])
|
| 11 |
+
|
| 12 |
+
if uploaded_file is not None:
|
| 13 |
+
# Read the file
|
| 14 |
+
if uploaded_file.name.endswith('.csv'):
|
| 15 |
+
df = pd.read_csv(uploaded_file)
|
| 16 |
+
elif uploaded_file.name.endswith('.xlsx'):
|
| 17 |
+
df = pd.read_excel(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
st.write("### Raw Data")
|
| 20 |
+
st.write(df)
|
| 21 |
+
|
| 22 |
+
# Step 2: Use Hugging Face SmolAgents for data cleansing and preprocessing
|
| 23 |
+
st.write("### Data Cleansing and Preprocessing")
|
| 24 |
+
cleanser = DataCleanser()
|
| 25 |
+
df_cleaned = cleanser.clean_data(df)
|
| 26 |
+
st.write("Cleaned Data:")
|
| 27 |
+
st.write(df_cleaned)
|
| 28 |
+
|
| 29 |
+
# Step 3: Use Hugging Face SmolAgents to generate insights
|
| 30 |
+
st.write("### Key Insights from Data")
|
| 31 |
+
insight_generator = InsightGenerator()
|
| 32 |
+
insights = insight_generator.generate_insights(df_cleaned)
|
| 33 |
+
st.write(insights)
|
| 34 |
+
|
| 35 |
+
# Step 4: Create data visualizations
|
| 36 |
+
st.write("### Data Visualizations")
|
| 37 |
+
visualizer = DataVisualizer()
|
| 38 |
|
| 39 |
+
# Example visualizations
|
| 40 |
+
st.write("#### Histogram of Numerical Columns")
|
| 41 |
+
numerical_columns = df_cleaned.select_dtypes(include=[np.number]).columns
|
| 42 |
+
for col in numerical_columns:
|
| 43 |
+
fig = visualizer.plot_histogram(df_cleaned, col)
|
| 44 |
+
st.pyplot(fig)
|
| 45 |
+
|
| 46 |
+
st.write("#### Correlation Heatmap")
|
| 47 |
+
fig = visualizer.plot_correlation_heatmap(df_cleaned)
|
| 48 |
+
st.pyplot(fig)
|
| 49 |
+
|
| 50 |
+
# Step 5: Display the output
|
| 51 |
+
st.write("### Final Output")
|
| 52 |
+
st.write("Data analysis completed. Check the insights and visualizations above.")
|
| 53 |
+
|
| 54 |
+
else:
|
| 55 |
+
st.write("Please upload a file to get started.")
|