Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import openai
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
openai.api_key = os.getenv("openapikey")
|
| 10 |
+
|
| 11 |
+
def generate_insight(df, question):
|
| 12 |
+
"""Generates insights using an LLM."""
|
| 13 |
+
prompt = f"Given the following dataset (first 5 rows):\n{df.head().to_string()}\n\nQuestion: {question}\n\nAnswer:"
|
| 14 |
+
try:
|
| 15 |
+
response = openai.chat.completions.create(
|
| 16 |
+
model="gpt-3.5-turbo",
|
| 17 |
+
messages=[{"role": "user", "content": prompt}],
|
| 18 |
+
max_tokens=300,
|
| 19 |
+
)
|
| 20 |
+
return response.choices[0].message.content.strip()
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"Error: {e}"
|
| 23 |
+
def generate_visualization(df, column_x, column_y=None, vis_type="hist"):
|
| 24 |
+
"""Generates a visualization."""
|
| 25 |
+
try:
|
| 26 |
+
plt.figure(figsize=(10, 6))
|
| 27 |
+
if vis_type == "hist":
|
| 28 |
+
sns.histplot(df[column_x])
|
| 29 |
+
elif vis_type == "scatter" and column_y:
|
| 30 |
+
sns.scatterplot(x=column_x, y=column_y, data=df)
|
| 31 |
+
else:
|
| 32 |
+
return "Invalid visualization request."
|
| 33 |
+
st.pyplot(plt)
|
| 34 |
+
return "Visualization generated."
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Error generating visualization: {e}"
|
| 37 |
+
|
| 38 |
+
st.title("Data Exploration and Insight Generation")
|
| 39 |
+
|
| 40 |
+
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
| 41 |
+
|
| 42 |
+
if uploaded_file:
|
| 43 |
+
df = pd.read_csv(uploaded_file)
|
| 44 |
+
st.write("### Dataset Preview")
|
| 45 |
+
st.dataframe(df.head())
|
| 46 |
+
|
| 47 |
+
st.write("### Dataset Information")
|
| 48 |
+
st.write(f"Number of rows: {df.shape[0]}")
|
| 49 |
+
st.write(f"Number of columns: {df.shape[1]}")
|
| 50 |
+
st.write(f"Column names: {', '.join(df.columns)}")
|
| 51 |
+
|
| 52 |
+
question = st.text_input("Ask a question about the data:")
|
| 53 |
+
if question:
|
| 54 |
+
insight = generate_insight(df, question)
|
| 55 |
+
st.write("### Generated Insight")
|
| 56 |
+
st.write(insight)
|
| 57 |
+
|
| 58 |
+
st.write("### Data Visualization")
|
| 59 |
+
col_x = st.selectbox("Select X-axis column", df.columns)
|
| 60 |
+
col_y = st.selectbox("Select Y-axis column (for scatter plot)", [None] + list(df.columns))
|
| 61 |
+
vis_type = st.selectbox("Select visualization type", ["hist", "scatter"])
|
| 62 |
+
if st.button("Generate Visualization"):
|
| 63 |
+
vis_result = generate_visualization(df, col_x, col_y if vis_type == "scatter" else None, vis_type)
|
| 64 |
+
st.write(vis_result)
|