Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,65 @@
|
|
1 |
-
import streamlit as st
|
2 |
import pandas as pd
|
|
|
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
)
|
14 |
-
|
15 |
-
# Group by main categories
|
16 |
-
main_categories = df.groupby('Main Category')['Logged'].sum().reset_index()
|
17 |
-
total_hours = main_categories['Logged'].sum()
|
18 |
-
main_categories['Percentage'] = (main_categories['Logged'] / total_hours * 100).round(1)
|
19 |
-
|
20 |
-
# Prepare non-billable breakdown
|
21 |
-
non_billable = df[df['Main Category'] == 'Non-Billable']
|
22 |
-
non_billable_breakdown = non_billable.groupby('Project Category')['Logged'].sum().reset_index()
|
23 |
-
|
24 |
-
return main_categories, non_billable_breakdown
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
data['Logged'],
|
30 |
-
labels=data['Main Category'],
|
31 |
-
autopct='%1.1f%%',
|
32 |
-
colors=['#4CAF50', '#FFC107', '#9E9E9E'],
|
33 |
-
startangle=90
|
34 |
-
)
|
35 |
-
plt.setp(autotexts, size=10, weight="bold", color='white')
|
36 |
-
ax.set_title('Overall Utilization Distribution', pad=20)
|
37 |
-
return fig
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
ax.set_xlabel('')
|
45 |
-
plt.xticks(rotation=45, ha='right')
|
46 |
-
plt.tight_layout()
|
47 |
-
return fig
|
48 |
|
49 |
-
|
50 |
-
st.
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
st.
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
except Exception as e:
|
86 |
-
st.error(f"Error processing file: {str(e)}")
|
87 |
|
88 |
if __name__ == "__main__":
|
89 |
-
main()
|
|
|
|
|
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 |
if __name__ == "__main__":
|
65 |
+
main()
|