Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,53 @@
|
|
|
|
|
| 1 |
import pandas as pd
|
|
|
|
| 2 |
import openai
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
|
| 6 |
-
# Analyze using OpenAI
|
| 7 |
-
def get_openai_insights(api_key, prompt):
|
| 8 |
-
openai.api_key = api_key
|
| 9 |
-
response = openai.Completion.create(
|
| 10 |
-
engine="text-davinci-003",
|
| 11 |
-
prompt=prompt,
|
| 12 |
-
max_tokens=500,
|
| 13 |
-
temperature=0.5
|
| 14 |
-
)
|
| 15 |
-
return response["choices"][0]["text"].strip()
|
| 16 |
-
|
| 17 |
-
# Streamlit app
|
| 18 |
-
def main():
|
| 19 |
-
st.title("Excel Data Visualization with OpenAI Insights")
|
| 20 |
-
|
| 21 |
-
# Input OpenAI API Key
|
| 22 |
-
api_key = st.text_input("Enter your OpenAI API Key", type="password")
|
| 23 |
-
if not api_key:
|
| 24 |
-
st.warning("Please enter your OpenAI API key to proceed.")
|
| 25 |
-
return
|
| 26 |
-
|
| 27 |
-
# File upload
|
| 28 |
-
excel_file = st.file_uploader("Upload the Excel File", type=["xls", "xlsx"])
|
| 29 |
-
|
| 30 |
-
if excel_file:
|
| 31 |
-
# Load Excel data
|
| 32 |
-
excel_data = pd.ExcelFile(excel_file)
|
| 33 |
-
st.sidebar.header("Select a Sheet to Visualize")
|
| 34 |
-
sheet_name = st.sidebar.selectbox("Sheet Name", excel_data.sheet_names)
|
| 35 |
-
|
| 36 |
-
if sheet_name:
|
| 37 |
-
data = pd.read_excel(excel_data, sheet_name=sheet_name)
|
| 38 |
-
st.subheader(f"Data from Sheet: {sheet_name}")
|
| 39 |
-
st.dataframe(data)
|
| 40 |
-
|
| 41 |
-
# Option to generate insights using OpenAI
|
| 42 |
-
st.header("Generate AI Insights")
|
| 43 |
-
if st.button("Get Insights from OpenAI"):
|
| 44 |
-
with st.spinner("Generating insights..."):
|
| 45 |
-
try:
|
| 46 |
-
data_sample = data.head(5).to_csv(index=False)
|
| 47 |
-
prompt = f"Analyze the following data and provide key insights:\n\n{data_sample}"
|
| 48 |
-
insights = get_openai_insights(api_key, prompt)
|
| 49 |
-
st.success("AI Insights Generated!")
|
| 50 |
-
st.text_area("AI Insights:", insights, height=200)
|
| 51 |
-
except openai.error.OpenAIError as e:
|
| 52 |
-
st.error(f"Error with OpenAI API: {e}")
|
| 53 |
-
|
| 54 |
-
# Visualize numeric data
|
| 55 |
-
st.header("Visualize Data")
|
| 56 |
-
numeric_cols = data.select_dtypes(include="number").columns
|
| 57 |
-
if numeric_cols.any():
|
| 58 |
-
col_to_plot = st.selectbox("Select a Column to Plot", numeric_cols)
|
| 59 |
-
if col_to_plot:
|
| 60 |
-
fig, ax = plt.subplots()
|
| 61 |
-
data[col_to_plot].plot(kind="bar", ax=ax, title=f"{col_to_plot} Analysis")
|
| 62 |
-
st.pyplot(fig)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
import openai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Setup OpenAI API
|
| 7 |
+
openai.api_key = st.text_input("Enter your OpenAI API key", type="password")
|
| 8 |
+
|
| 9 |
+
# App Title
|
| 10 |
+
st.title("Bi-Weekly Report Visualizer")
|
| 11 |
+
|
| 12 |
+
# Upload Excel File
|
| 13 |
+
uploaded_file = st.file_uploader("Upload your Excel file", type=["xls", "xlsx"])
|
| 14 |
+
|
| 15 |
+
if uploaded_file is not None:
|
| 16 |
+
# Load data
|
| 17 |
+
df = pd.read_excel(uploaded_file)
|
| 18 |
+
|
| 19 |
+
# Display raw data
|
| 20 |
+
st.write("### Raw Data")
|
| 21 |
+
st.dataframe(df)
|
| 22 |
+
|
| 23 |
+
# Extract relevant columns (assume "Week", "Category", and "Hours" columns exist in the sheet)
|
| 24 |
+
if {"Week", "Category", "Hours"}.issubset(df.columns):
|
| 25 |
+
# Summarize data for visualization
|
| 26 |
+
summary = df.groupby(["Week", "Category"]).sum().reset_index()
|
| 27 |
+
|
| 28 |
+
# Visualize data as a pie chart for each week
|
| 29 |
+
st.write("### Weekly Time Allocation")
|
| 30 |
+
weeks = summary["Week"].unique()
|
| 31 |
+
for week in weeks:
|
| 32 |
+
week_data = summary[summary["Week"] == week]
|
| 33 |
+
fig = px.pie(week_data, values="Hours", names="Category", title=f"Week: {week}")
|
| 34 |
+
st.plotly_chart(fig)
|
| 35 |
+
|
| 36 |
+
# Generate descriptive insights using OpenAI
|
| 37 |
+
if st.button("Generate Insights"):
|
| 38 |
+
# Get summary in text form
|
| 39 |
+
insights_data = summary.to_string(index=False)
|
| 40 |
+
# OpenAI prompt
|
| 41 |
+
prompt = f"Provide insights about time allocation trends in the following data:\n\n{insights_data}"
|
| 42 |
+
try:
|
| 43 |
+
response = openai.Completion.create(
|
| 44 |
+
engine="text-davinci-003",
|
| 45 |
+
prompt=prompt,
|
| 46 |
+
max_tokens=150
|
| 47 |
+
)
|
| 48 |
+
st.write("### Insights")
|
| 49 |
+
st.text(response.choices[0].text.strip())
|
| 50 |
+
except Exception as e:
|
| 51 |
+
st.error(f"Failed to generate insights: {e}")
|
| 52 |
+
else:
|
| 53 |
+
st.error("The Excel file doesn't have the required columns: 'Week', 'Category', and 'Hours'.")
|