abdurr29 commited on
Commit
61a60e1
1 Parent(s): 8f1f0a2
Files changed (1) hide show
  1. app.py +143 -0
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ from pathlib import Path
6
+
7
+ def search_data(sku, group_name, date):
8
+
9
+
10
+ if(sku == '' and group_name == '' and date != ''):
11
+ filtered_df = initial_df[initial_df['Date'] == date]
12
+
13
+ elif(sku == '' and group_name != '' and date == ''):
14
+ filtered_df = initial_df[initial_df['Group Name'] == group_name]
15
+
16
+ elif(sku != '' and group_name == '' and date == ''):
17
+ filtered_df = initial_df[initial_df['Current Job'] == sku]
18
+
19
+ elif(sku == '' and group_name != '' and date != ''):
20
+ filtered_df = initial_df[(initial_df['Group Name'] == group_name) & (initial_df['Date'] == date)]
21
+
22
+ elif(sku != '' and group_name == '' and date != ''):
23
+ filtered_df = initial_df[(initial_df['Current Job'] == sku) & (initial_df['Date'] == date)]
24
+
25
+ elif(sku != '' and group_name != '' and date == ''):
26
+ filtered_df = initial_df[(initial_df['Current Job'] == sku) & (initial_df['Group Name'] == group_name)]
27
+
28
+ else:
29
+ filtered_df = initial_df[(initial_df['Current Job'] == sku) & (initial_df['Group Name'] == group_name) & (initial_df['Date'] == date)]
30
+
31
+
32
+ records = len(filtered_df)
33
+
34
+ compliant_records = len(filtered_df[(filtered_df['Units Per Hour'] >= filtered_df['Lower Target']) & (filtered_df['Units Per Hour'] <= filtered_df['Upper Target'])])
35
+
36
+ compliance_percentage = round((compliant_records / records),2) * 100 if records != 0 else 0
37
+
38
+ avg_units_per_hour = round(filtered_df['Units Per Hour'].mean(),2)
39
+
40
+ stdev_units_per_hour = round(filtered_df['Units Per Hour'].std(),2)
41
+
42
+ most_common_value = filtered_df['Description'].value_counts().idxmax()
43
+
44
+ fig1 = plt.figure()
45
+ plt.hist(filtered_df['Units Per Hour'], bins=20, color='skyblue', edgecolor='black')
46
+ plt.xlabel('Units Per Hour')
47
+ plt.ylabel('Frequency')
48
+ plt.grid(True)
49
+
50
+ return most_common_value, records, compliance_percentage, avg_units_per_hour, stdev_units_per_hour, fig1
51
+
52
+
53
+
54
+
55
+ def upload_file(filepath):
56
+ global initial_df
57
+ name = Path(filepath).name
58
+ initial_df = prepare_df(filepath)
59
+ initial_analysis_output = initial_analysis(initial_df)
60
+ return initial_analysis_output
61
+
62
+ def initial_analysis(df):
63
+
64
+ compliant_df = df[(df['Units Per Hour'] >= df['Lower Target']) & (df['Units Per Hour'] <= df['Upper Target'])]
65
+ non_compliant_df = df[(df['Units Per Hour'] < df['Lower Target']) | (df['Units Per Hour'] > df['Upper Target'])]
66
+
67
+ total_observations = len(df)
68
+ pct_compliant = 100 * round(len(compliant_df)/total_observations,2)
69
+ pct_non_compliant = 100 * round(len(non_compliant_df)/total_observations,2)
70
+
71
+ return total_observations, pct_compliant, pct_non_compliant
72
+
73
+ def prepare_df(file_path):
74
+
75
+ df = pd.read_excel(file_path)
76
+
77
+ df['Current Job'] = df['Current Job'].astype(str)
78
+
79
+ df['Start of Batch Date/Time'] = pd.to_datetime(df['Start of Batch Date/Time'])
80
+ df['End of Batch Date/Time'] = pd.to_datetime(df['End of Batch Date/Time'])
81
+
82
+ df['Batch Length (Hours)'] = (df['End of Batch Date/Time'] - df['Start of Batch Date/Time']).dt.total_seconds() / 3600
83
+
84
+ df['Units Per Hour'] = df['Batch Count'] / df['Batch Length (Hours)']
85
+
86
+ df.dropna(subset=['Optimal Cases Per Hour'], inplace=True)
87
+
88
+ return df
89
+
90
+
91
+ with gr.Blocks() as tab_search_by_sku:
92
+ with gr.Row():
93
+ sku = gr.Textbox(label="SKU")
94
+ group_name = gr.Textbox(label="Group Name")
95
+ date = gr.Textbox(label="Date(yyyy-mm-dd)")
96
+
97
+ with gr.Row():
98
+ description = gr.Textbox(label="Description")
99
+
100
+ with gr.Row():
101
+ records = gr.Textbox(label="Records")
102
+ compliance = gr.Textbox(label="Compliance %")
103
+ avgunits = gr.Textbox(label="Avg Units/Hr")
104
+ stddev = gr.Textbox(label="Std Dev Units/Hr")
105
+
106
+ with gr.Row():
107
+ graph = gr.Plot(label="Units Per Hour Distribution")
108
+
109
+ with gr.Row():
110
+ submit_btn = gr.Button('Submit')
111
+ submit_btn.click(
112
+ fn=search_data,
113
+ inputs=[sku, group_name, date],
114
+ outputs=[description, records, compliance, avgunits, stddev, graph]
115
+ )
116
+
117
+ # clear_btn = gr.Button('Clear')
118
+ # clear_btn.click(
119
+ # fn=lambda: ("", "", "", "", "", plt.figure()),
120
+ # inputs=[],
121
+ # outputs=[description, records, compliance, avgunits, stddev, graph]
122
+ # )
123
+
124
+
125
+
126
+
127
+ tab_files = gr.Interface(
128
+ fn = upload_file,
129
+ inputs = [
130
+ gr.File(label="Upload Template File", file_types=[".xlsx"], file_count="single")
131
+ ],
132
+ outputs = [
133
+ gr.Textbox(label="Total Observations", interactive=False),
134
+ gr.Textbox(label="Compliant Observations %", interactive=False),
135
+ gr.Textbox(label="Non-Compliant Observations %", interactive=False)
136
+ ],
137
+ allow_flagging = "never"
138
+
139
+ )
140
+
141
+ demo = gr.TabbedInterface([tab_files, tab_search_by_sku], ["Upload Sheet", "Search"])
142
+
143
+ demo.launch(share=True, auth=("BrianA", "Brian@Muffintown1")