samridh12 commited on
Commit
89a60a0
·
verified ·
1 Parent(s): 706fbda

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +174 -0
app.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import pickle
4
+ import os
5
+ from database import create_connection, initialize_database, insert_data, fetch_data
6
+
7
+ # Database setup
8
+ conn = create_connection('example.db')
9
+ initialize_database(conn)
10
+
11
+ # Load the trained models
12
+ current_dir = os.path.dirname(os.path.abspath(__file__))
13
+
14
+ with open(os.path.join(current_dir, 'best_model.pkl'), 'rb') as model_file:
15
+ input_weight_model = pickle.load(model_file)
16
+ with open(os.path.join(current_dir, 'machining_model.pkl'), 'rb') as model_file:
17
+ machining_model = pickle.load(model_file)
18
+ with open(os.path.join(current_dir, 'inspection_model.pkl'), 'rb') as model_file:
19
+ inspection_model = pickle.load(model_file)
20
+
21
+ # Final landed cost based on grade type
22
+ final_landed_cost = {
23
+ '1 MT XX (25-95 dia)': 103,
24
+ '1 MT XX (100-210 dia)': 113,
25
+ '1 MT YY (25-95 dia)': 160,
26
+ '1 MT YY (100-125 dia)': 173,
27
+ '1 MT XY (25-95 dia))': 106,
28
+ '1 MT 8319 (100-210 dia)':116,
29
+ '1 MT 8319':104
30
+ }
31
+
32
+ # Function to calculate raw material cost
33
+ def calculate_raw_material_cost(process_type, input_weight, grade_type):
34
+ if process_type == 0: # 0 represents casting
35
+ return 0
36
+ elif process_type == 1: # 1 represents forging
37
+ return input_weight * final_landed_cost[grade_type]
38
+
39
+ # Function to calculate process cost
40
+ def calculate_process_cost(process_type, input_weight):
41
+ if process_type == 0: # 0 represents casting
42
+ return (input_weight * (120.57788 / 1000) * 1000)
43
+ elif process_type == 1: # 1 represents forging
44
+ return input_weight * 30
45
+
46
+ # Streamlit interface
47
+ st.title("EX-Works Calculator")
48
+
49
+ # Page navigation
50
+ pages = ["Home", "Vendor Data", "Material Data", "RM Cost Data", "Supplier Data"]
51
+ page = st.sidebar.selectbox("Select Page", pages)
52
+
53
+ if page == "Home":
54
+ st.write("Welcome to the EX-Works Calculator application.")
55
+
56
+ elif page == "Vendor Data":
57
+ st.header("Vendor Data")
58
+ vendor_name = st.text_input("Vendor Name")
59
+ vendor_type = st.text_input("Vendor Type")
60
+ gst_no =st.number_input("GST NO")
61
+ contact_person_name=st.text_input("CONTACT PERSON/NAME")
62
+ address=st.text_input("ADDRESS")
63
+ city=st.text_input("CITY")
64
+ panno=st.text_input("PAN NO")
65
+
66
+ if st.button("Add Vendor"):
67
+ vendor_data = pd.DataFrame({'vendor_name': [vendor_name], 'vendor_type': [vendor_type], 'GST_NO': [gst_no], 'Contact_person_name': [contact_person_name], 'address': [address], 'city': [city], 'pan_no':[panno]})
68
+ insert_data(conn, 'vendor_data', vendor_data)
69
+ st.success("Vendor data added successfully")
70
+
71
+ elif page == "Material Data":
72
+ st.header("Material Data")
73
+ part_id = st.number_input("Part ID")
74
+ part_no = st.number_input("Part Number")
75
+ scf = st.selectbox("SCF", options=[0, 1])
76
+ process_type = st.selectbox("Process Type", options=[0, 1])
77
+ part_od = st.number_input("Part Outer Dimension")
78
+ part_width = st.number_input("Part Width")
79
+ part_inner_dimension = st.number_input("Part Inner Dimension")
80
+ material_spec = st.selectbox("Material Specification", options=[0, 1])
81
+ finish_wt = st.number_input("Finish Weight")
82
+ green_drg_no = st.selectbox("Green DRG Number", options=[0, 1])
83
+
84
+ if st.button("Add Material"):
85
+ material_data = pd.DataFrame({'Part_id': [part_id],'part_no': [part_no], 'scf': [scf],'process_type': [process_type],'part_od': [part_od],'part_width': [part_width],'part_inner_dimension': [part_inner_dimension],'material_specification': [material_spec],'finish_wt': [finish_wt],'green_drg_no': [green_drg_no]})
86
+ insert_data(conn, 'material_data', material_data)
87
+ st.success("Material data added successfully")
88
+
89
+
90
+ elif page == "RM Cost Data":
91
+ st.header("RM Cost Data")
92
+ rm_type = st.text_input("RM Type")
93
+ rm_cost = st.number_input("RM Cost", min_value=0.0, step=0.01)
94
+ vendor_id = st.number_input("Vendor ID", min_value=1, step=1)
95
+
96
+ if st.button("Add RM Cost Data"):
97
+ rm_cost_data = pd.DataFrame({'rm_type': [rm_type], 'rm_cost': [rm_cost], 'vendor_id': [vendor_id]})
98
+ insert_data(conn, 'rm_cost_data', rm_cost_data)
99
+ st.success("RM cost data added successfully")
100
+
101
+ elif page == "Supplier Data":
102
+ st.header("Supplier Data")
103
+ part_no = st.number_input("Part No", min_value=1, step=1)
104
+ process_type = st.selectbox("Process Type", options=[0, 1])
105
+ part_od = st.number_input("Part OD", min_value=0.0, step=0.1)
106
+ part_id = st.number_input("Part ID", min_value=0.0, step=0.1)
107
+ part_width = st.number_input("Part Width", min_value=0, step=1)
108
+ finish_wt = st.number_input("Finish Wt", min_value=0.0, step=0.1)
109
+ grade_type = st.selectbox("Grade Type", options=list(final_landed_cost.keys()))
110
+ material_id = st.number_input("Material ID", min_value=1, step=1)
111
+
112
+ if st.button("Calculate and Add Supplier Data"):
113
+ # Prepare the input data for prediction
114
+ input_data = pd.DataFrame({
115
+ 'Process type': [process_type],
116
+ 'Part Od': [part_od],
117
+ 'Part ID': [part_id],
118
+ 'Part Width': [part_width],
119
+ 'Finish Wt': [finish_wt]
120
+ })
121
+
122
+ # Predict the input weight
123
+ predicted_input_weight = input_weight_model.predict(input_data)[0]
124
+
125
+ # Calculate raw material cost
126
+ raw_material_cost = calculate_raw_material_cost(process_type, predicted_input_weight, grade_type)
127
+
128
+ # Calculate process cost
129
+ process_cost = calculate_process_cost(process_type, predicted_input_weight)
130
+
131
+ # Prepare the data for machining time prediction
132
+ machining_data = pd.DataFrame({
133
+ 'Process type': [process_type],
134
+ 'Part Od': [part_od],
135
+ 'Part ID': [part_id],
136
+ 'Part Width': [part_width],
137
+ 'Finish Wt': [finish_wt],
138
+ 'Input Weight': [predicted_input_weight],
139
+ 'Raw material cost': [raw_material_cost],
140
+ 'Process cost': [process_cost]
141
+ })
142
+
143
+ # Predict the machining time
144
+ predicted_machining_time = machining_model.predict(machining_data)[0]
145
+
146
+ # Calculate machining cost
147
+ machining_cost = predicted_machining_time * 375.71
148
+
149
+ # Calculate scrap recovery
150
+ scrap_recovery = (predicted_input_weight - finish_wt) * 11.5
151
+
152
+ # Prepare the data for inspection time prediction
153
+ inspection_data = pd.DataFrame({
154
+ 'Process type': [process_type],
155
+ 'Part Od': [part_od],
156
+ 'Part ID': [part_id],
157
+ 'Part Width': [part_width],
158
+ 'Finish Wt': [finish_wt],
159
+ 'Input Weight': [predicted_input_weight],
160
+ 'Raw material cost': [raw_material_cost],
161
+ 'Process cost': [process_cost],
162
+ 'Machining Time': [predicted_machining_time],
163
+ 'Machining cost': [machining_cost],
164
+ 'Scrap recovery': [scrap_recovery]
165
+ })
166
+
167
+ # Predict the inspection time
168
+ predicted_inspection_time = inspection_model.predict(inspection_data)[0]
169
+
170
+ # Calculate inspection cost
171
+ inspection_cost = predicted_inspection_time * 375.71
172
+
173
+ # Calculate total mg cost
174
+ total_mg_cost = raw_material_cost + process_cost + machining_cost + scrap_recovery + inspection