Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import smolagent
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
|
| 7 |
+
def preprocess_data(file):
|
| 8 |
+
try:
|
| 9 |
+
df = pd.read_csv(file.name) if file.name.endswith('.csv') else pd.read_excel(file.name)
|
| 10 |
+
agent = smolagent.SmolAgent()
|
| 11 |
+
cleaned_df = agent.run("Clean and preprocess this dataset", df)
|
| 12 |
+
return cleaned_df.describe().to_string()
|
| 13 |
+
except Exception as e:
|
| 14 |
+
return f"Error in preprocessing: {str(e)}"
|
| 15 |
+
|
| 16 |
+
def generate_insights(file):
|
| 17 |
+
try:
|
| 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 |
+
visualize_btn = gr.Button("Visualize Data")
|
| 55 |
+
visualize_output = gr.Plot()
|
| 56 |
+
visualize_btn.click(visualize_data, inputs=file_input, outputs=visualize_output)
|
| 57 |
+
|
| 58 |
+
demo.launch()
|