Akshayram1 commited on
Commit
18eaccd
·
verified ·
1 Parent(s): c0df516

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -80
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
- def process_data(df):
6
- # Clean and prepare data
7
- df = df[['Project Category', 'Logged']].copy()
8
- df = df.dropna(subset=['Project Category'])
9
-
10
- # Consolidate categories
11
- df['Main Category'] = df['Project Category'].apply(
12
- lambda x: 'Billable' if 'Billable' in str(x) else str(x).strip()
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
- def create_pie_chart(data):
27
- fig, ax = plt.subplots(figsize=(6, 6))
28
- wedges, texts, autotexts = ax.pie(
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
- def create_bar_chart(data):
40
- fig, ax = plt.subplots(figsize=(10, 5))
41
- data.plot(kind='bar', x='Project Category', y='Logged', ax=ax, legend=False)
42
- ax.set_title('Non-Billable Time Breakdown')
43
- ax.set_ylabel('Hours')
44
- ax.set_xlabel('')
45
- plt.xticks(rotation=45, ha='right')
46
- plt.tight_layout()
47
- return fig
48
 
49
- def main():
50
- st.title('QA Team Utilization Dashboard')
51
-
52
- uploaded_file = st.file_uploader("Upload Timesheet Excel File", type=['xls', 'xlsx'])
53
-
54
- if uploaded_file:
55
- try:
56
- df = pd.read_excel(uploaded_file, sheet_name='Report')
57
- main_cats, non_billable = process_data(df)
58
-
59
- # Main utilization section
60
- st.header("Overall Utilization")
61
- col1, col2 = st.columns([2, 1])
62
-
63
- with col1:
64
- st.pyplot(create_pie_chart(main_cats))
65
-
66
- with col2:
67
- st.dataframe(
68
- main_cats[['Main Category', 'Logged', 'Percentage']],
69
- hide_index=True,
70
- column_config={
71
- 'Main Category': 'Category',
72
- 'Logged': st.column_config.NumberColumn('Hours', format="%.2f"),
73
- 'Percentage': st.column_config.NumberColumn(format="%.1f%%")
74
- }
75
- )
76
-
77
- # Non-billable breakdown
78
- st.header("Non-Billable Detailed Breakdown")
79
- st.pyplot(create_bar_chart(non_billable))
80
-
81
- # Raw data preview
82
- st.subheader("Raw Data Preview")
83
- st.dataframe(df.head(10), hide_index=True)
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()