Spaces:
Build error
Build error
Update app.py
#1
by
alexacido
- opened
app.py
CHANGED
|
@@ -1,89 +1,89 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import google.generativeai as genai
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
import seaborn as sns
|
| 6 |
-
import plotly.express as px
|
| 7 |
-
|
| 8 |
-
# Set up page layout
|
| 9 |
-
st.set_page_config(page_title="AI CSV Data Analyst", layout="wide")
|
| 10 |
-
|
| 11 |
-
# Initialize Gemini API (Replace with your API Key)
|
| 12 |
-
GEMINI_API_KEY = "
|
| 13 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
-
|
| 15 |
-
# Create two columns (70:30 split)
|
| 16 |
-
left_col, right_col = st.columns([7, 3])
|
| 17 |
-
|
| 18 |
-
with left_col:
|
| 19 |
-
st.title("π AI-Powered CSV Data Analyst")
|
| 20 |
-
|
| 21 |
-
# File Upload
|
| 22 |
-
uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
|
| 23 |
-
|
| 24 |
-
if uploaded_file is not None:
|
| 25 |
-
# Read File
|
| 26 |
-
file_ext = uploaded_file.name.split(".")[-1]
|
| 27 |
-
if file_ext == "csv":
|
| 28 |
-
df = pd.read_csv(uploaded_file)
|
| 29 |
-
elif file_ext == "xlsx":
|
| 30 |
-
df = pd.read_excel(uploaded_file, engine="openpyxl")
|
| 31 |
-
|
| 32 |
-
# Display the DataFrame
|
| 33 |
-
st.subheader("π Uploaded Data")
|
| 34 |
-
st.dataframe(df)
|
| 35 |
-
|
| 36 |
-
# Data Insights
|
| 37 |
-
st.subheader("π Data Insights")
|
| 38 |
-
|
| 39 |
-
# Dataset Summary
|
| 40 |
-
st.write(f"**Rows:** {df.shape[0]}, **Columns:** {df.shape[1]}")
|
| 41 |
-
st.write(f"**Missing Values:**\n{df.isnull().sum()}")
|
| 42 |
-
|
| 43 |
-
# Basic Statistics
|
| 44 |
-
st.subheader("π Statistical Summary")
|
| 45 |
-
st.write(df.describe())
|
| 46 |
-
|
| 47 |
-
# Visualizations
|
| 48 |
-
st.subheader("π Data Visualizations")
|
| 49 |
-
|
| 50 |
-
# Select Column for Histogram
|
| 51 |
-
numeric_columns = df.select_dtypes(include=["number"]).columns
|
| 52 |
-
if len(numeric_columns) > 0:
|
| 53 |
-
col = st.selectbox("Select a column for histogram:", numeric_columns)
|
| 54 |
-
fig = px.histogram(df, x=col, title=f"Histogram of {col}")
|
| 55 |
-
st.plotly_chart(fig)
|
| 56 |
-
|
| 57 |
-
# Correlation Heatmap
|
| 58 |
-
if len(numeric_columns) > 1:
|
| 59 |
-
st.subheader("π Correlation Heatmap")
|
| 60 |
-
fig, ax = plt.subplots(figsize=(6, 4))
|
| 61 |
-
sns.heatmap(df[numeric_columns].corr(), annot=True, cmap="coolwarm", ax=ax)
|
| 62 |
-
st.pyplot(fig)
|
| 63 |
-
|
| 64 |
-
with right_col:
|
| 65 |
-
st.subheader("π¬ Chat with Your Data")
|
| 66 |
-
|
| 67 |
-
user_query = st.text_input("Ask a question about the data...")
|
| 68 |
-
|
| 69 |
-
if user_query and uploaded_file is not None:
|
| 70 |
-
# Prepare prompt for AI
|
| 71 |
-
prompt = f"""
|
| 72 |
-
You are a data analyst. The user has uploaded a dataset.
|
| 73 |
-
Answer the query based on the dataset provided.
|
| 74 |
-
|
| 75 |
-
Dataset Overview:
|
| 76 |
-
{df.describe().to_string()}
|
| 77 |
-
|
| 78 |
-
User Question:
|
| 79 |
-
{user_query}
|
| 80 |
-
"""
|
| 81 |
-
|
| 82 |
-
# Get AI response
|
| 83 |
-
model = genai.GenerativeModel("gemini-pro")
|
| 84 |
-
response = model.generate_content(prompt)
|
| 85 |
-
|
| 86 |
-
# Display AI Response
|
| 87 |
-
if response.text:
|
| 88 |
-
st.write("π€ AI Response:")
|
| 89 |
-
st.write(response.text)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
|
| 8 |
+
# Set up page layout
|
| 9 |
+
st.set_page_config(page_title="AI CSV Data Analyst", layout="wide")
|
| 10 |
+
|
| 11 |
+
# Initialize Gemini API (Replace with your API Key)
|
| 12 |
+
GEMINI_API_KEY = "AIzaSyDt0TM6beHrE-f5bvfYXQa6iDACSCfU7go"
|
| 13 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
+
|
| 15 |
+
# Create two columns (70:30 split)
|
| 16 |
+
left_col, right_col = st.columns([7, 3])
|
| 17 |
+
|
| 18 |
+
with left_col:
|
| 19 |
+
st.title("π AI-Powered CSV Data Analyst")
|
| 20 |
+
|
| 21 |
+
# File Upload
|
| 22 |
+
uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
|
| 23 |
+
|
| 24 |
+
if uploaded_file is not None:
|
| 25 |
+
# Read File
|
| 26 |
+
file_ext = uploaded_file.name.split(".")[-1]
|
| 27 |
+
if file_ext == "csv":
|
| 28 |
+
df = pd.read_csv(uploaded_file)
|
| 29 |
+
elif file_ext == "xlsx":
|
| 30 |
+
df = pd.read_excel(uploaded_file, engine="openpyxl")
|
| 31 |
+
|
| 32 |
+
# Display the DataFrame
|
| 33 |
+
st.subheader("π Uploaded Data")
|
| 34 |
+
st.dataframe(df)
|
| 35 |
+
|
| 36 |
+
# Data Insights
|
| 37 |
+
st.subheader("π Data Insights")
|
| 38 |
+
|
| 39 |
+
# Dataset Summary
|
| 40 |
+
st.write(f"**Rows:** {df.shape[0]}, **Columns:** {df.shape[1]}")
|
| 41 |
+
st.write(f"**Missing Values:**\n{df.isnull().sum()}")
|
| 42 |
+
|
| 43 |
+
# Basic Statistics
|
| 44 |
+
st.subheader("π Statistical Summary")
|
| 45 |
+
st.write(df.describe())
|
| 46 |
+
|
| 47 |
+
# Visualizations
|
| 48 |
+
st.subheader("π Data Visualizations")
|
| 49 |
+
|
| 50 |
+
# Select Column for Histogram
|
| 51 |
+
numeric_columns = df.select_dtypes(include=["number"]).columns
|
| 52 |
+
if len(numeric_columns) > 0:
|
| 53 |
+
col = st.selectbox("Select a column for histogram:", numeric_columns)
|
| 54 |
+
fig = px.histogram(df, x=col, title=f"Histogram of {col}")
|
| 55 |
+
st.plotly_chart(fig)
|
| 56 |
+
|
| 57 |
+
# Correlation Heatmap
|
| 58 |
+
if len(numeric_columns) > 1:
|
| 59 |
+
st.subheader("π Correlation Heatmap")
|
| 60 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
| 61 |
+
sns.heatmap(df[numeric_columns].corr(), annot=True, cmap="coolwarm", ax=ax)
|
| 62 |
+
st.pyplot(fig)
|
| 63 |
+
|
| 64 |
+
with right_col:
|
| 65 |
+
st.subheader("π¬ Chat with Your Data")
|
| 66 |
+
|
| 67 |
+
user_query = st.text_input("Ask a question about the data...")
|
| 68 |
+
|
| 69 |
+
if user_query and uploaded_file is not None:
|
| 70 |
+
# Prepare prompt for AI
|
| 71 |
+
prompt = f"""
|
| 72 |
+
You are a data analyst. The user has uploaded a dataset.
|
| 73 |
+
Answer the query based on the dataset provided.
|
| 74 |
+
|
| 75 |
+
Dataset Overview:
|
| 76 |
+
{df.describe().to_string()}
|
| 77 |
+
|
| 78 |
+
User Question:
|
| 79 |
+
{user_query}
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
# Get AI response
|
| 83 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 84 |
+
response = model.generate_content(prompt)
|
| 85 |
+
|
| 86 |
+
# Display AI Response
|
| 87 |
+
if response.text:
|
| 88 |
+
st.write("π€ AI Response:")
|
| 89 |
+
st.write(response.text)
|