Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import pickle | |
| import os | |
| # Get the absolute path of the directory containing the current script | |
| current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| # Load the trained models using absolute paths | |
| with open(os.path.join(current_dir, 'best_model.pkl'), 'rb') as model_file: | |
| input_weight_model = pickle.load(model_file) | |
| with open(os.path.join(current_dir, 'machining_model.pkl'), 'rb') as model_file: | |
| machining_model = pickle.load(model_file) | |
| with open(os.path.join(current_dir, 'inspection_model.pkl'), 'rb') as model_file: | |
| inspection_model = pickle.load(model_file) | |
| # Load the trained models | |
| with open('best_model.pkl', 'rb') as model_file: | |
| input_weight_model = pickle.load(model_file) | |
| with open('machining_model.pkl', 'rb') as model_file: | |
| machining_model = pickle.load(model_file) | |
| with open('inspection_model.pkl', 'rb') as model_file: | |
| inspection_model = pickle.load(model_file) | |
| # Final landed cost based on grade type | |
| final_landed_cost = { | |
| '1 MT XX (25-95 dia)': 103, | |
| '1 MT XX (100-210 dia)': 113, | |
| '1 MT YY (25-95 dia)': 160, | |
| '1 MT YY (100-125 dia)': 173, | |
| '1 MT XY (25-95 dia))': 106, | |
| '1 MT 8319 (100-210 dia)':116, | |
| '1 MT 8319':104 | |
| } | |
| # Function to calculate raw material cost | |
| def calculate_raw_material_cost(process_type, input_weight, grade_type): | |
| if process_type == 0: # 0 represents casting | |
| return 0 | |
| elif process_type == 1: # 1 represents forging | |
| return input_weight * final_landed_cost[grade_type] | |
| # Function to calculate process cost | |
| def calculate_process_cost(process_type, input_weight): | |
| if process_type == 0: # 0 represents casting | |
| return (input_weight * (120.57788 / 1000)*1000) | |
| elif process_type == 1: # 1 represents forging | |
| return input_weight * 30 | |
| # Streamlit interface | |
| st.title("EX-Works Calculator") | |
| # User input form | |
| with st.form("input_form"): | |
| part_no = st.number_input("Part No", min_value=1, step=1) | |
| process_type = st.selectbox("Process Type", options=[0,1]) | |
| part_od = st.number_input("Part Od", min_value=0.0, step=0.1) | |
| part_id = st.number_input("Part ID", min_value=0.0, step=0.1) | |
| part_width = st.number_input("Part Width", min_value=0, step=1) | |
| finish_wt = st.number_input("Finish Wt", min_value=0.0, step=0.1) | |
| grade_type = st.selectbox("Grade Type", options=list(final_landed_cost.keys())) | |
| submitted = st.form_submit_button("Calculate") | |
| if submitted: | |
| # Prepare the input data for prediction | |
| input_data = pd.DataFrame({ | |
| 'Process type': [process_type], | |
| 'Part Od': [part_od], | |
| 'Part ID': [part_id], | |
| 'Part Width': [part_width], | |
| 'Finish Wt': [finish_wt] | |
| }) | |
| # Predict the input weight | |
| predicted_input_weight = input_weight_model.predict(input_data)[0] | |
| # Calculate raw material cost | |
| raw_material_cost = calculate_raw_material_cost(process_type, predicted_input_weight, grade_type) | |
| # Calculate process cost | |
| process_cost = calculate_process_cost(process_type, predicted_input_weight) | |
| # Prepare the data for machining time prediction | |
| machining_data = pd.DataFrame({ | |
| 'Process type': [process_type], | |
| 'Part Od': [part_od], | |
| 'Part ID': [part_id], | |
| 'Part Width': [part_width], | |
| 'Finish Wt': [finish_wt], | |
| 'Input Weight': [predicted_input_weight], | |
| 'Raw material cost': [raw_material_cost], | |
| 'Process cost': [process_cost] | |
| }) | |
| # Predict the machining time | |
| predicted_machining_time = machining_model.predict(machining_data)[0] | |
| # Calculate machining cost | |
| machining_cost = predicted_machining_time * 375.71 | |
| # Calculate scrap recovery | |
| scrap_recovery = (predicted_input_weight - finish_wt) * 11.5 | |
| # Prepare the data for inspection time prediction | |
| inspection_data = pd.DataFrame({ | |
| 'Process type': [process_type], | |
| 'Part Od': [part_od], | |
| 'Part ID': [part_id], | |
| 'Part Width': [part_width], | |
| 'Finish Wt': [finish_wt], | |
| 'Input Weight': [predicted_input_weight], | |
| 'Raw material cost': [raw_material_cost], | |
| 'Process cost': [process_cost], | |
| 'Machining Time': [predicted_machining_time], | |
| 'Machining cost ': [machining_cost], | |
| }) | |
| # Predict the inspection time | |
| predicted_inspection_time = inspection_model.predict(inspection_data)[0] | |
| # Calculate inspection cost | |
| inspection_cost = predicted_inspection_time * 435.43 | |
| # Calculate total Mg cost | |
| total_mg_cost = raw_material_cost + process_cost + machining_cost + inspection_cost - scrap_recovery | |
| # Calculate rejection on manufacturing cost | |
| rejection_on_manufacturing_cost = total_mg_cost * 0.003 | |
| # Calculate 'Oiling, Inspection' cost | |
| oiling_inspection_cost = total_mg_cost * 0.005 | |
| # Calculate 'Transport and packing (BIN + 80 micron bag)' cost | |
| transport_packing_cost = total_mg_cost * 0.01 | |
| # Calculate 'Overheads & Profit on Material' | |
| overheads_profit_material = raw_material_cost * 0.003 | |
| # Calculate 'Overheads & Profit on Conversion' | |
| overheads_profit_conversion = (total_mg_cost - raw_material_cost) * 0.07 | |
| # Calculate 'ICC' | |
| icc = total_mg_cost * 0.01 | |
| # Calculate 'Ex-works' | |
| ex_works = total_mg_cost + rejection_on_manufacturing_cost + oiling_inspection_cost + \ | |
| transport_packing_cost + overheads_profit_material + overheads_profit_conversion | |
| # Create DataFrame to display the results | |
| data = { | |
| 'Part No': [part_no], | |
| 'Process type': ['casting' if process_type == 0 else 'forging'], | |
| 'Part Od': [part_od], | |
| 'Part ID': [part_id], | |
| 'Part Width': [part_width], | |
| 'Finish Wt': [finish_wt], | |
| 'Predicted Input Weight': [predicted_input_weight], | |
| 'Grade type': [grade_type], | |
| 'Raw material cost': [raw_material_cost], | |
| 'Process cost': [process_cost], | |
| 'Predicted Machining Time': [predicted_machining_time], | |
| 'Machining Cost': [machining_cost], | |
| 'Scrap Recovery': [scrap_recovery], | |
| 'Predicted Inspection Time': [predicted_inspection_time], | |
| 'Inspection Cost': [inspection_cost], | |
| 'Total Mg Cost': [total_mg_cost], | |
| 'Rejection on Manufacturing cost': [rejection_on_manufacturing_cost], | |
| 'Oiling, Inspectio': [oiling_inspection_cost], # Ensure the column name remains the same | |
| 'Transport and packing( BIN + 80 micron bag)': [transport_packing_cost], | |
| 'Overheads & Profit on Material': [overheads_profit_material], | |
| 'Overheads & Profit on Conversion': [overheads_profit_conversion], | |
| 'ICC': [icc], | |
| 'Ex-works': [ex_works] | |
| } | |
| df = pd.DataFrame(data) | |
| st.write("Input Data, Predicted Input Weight, and Calculated Costs:") | |
| st.dataframe(df) | |