HarshilRamiAISV commited on
Commit
0836746
·
verified ·
1 Parent(s): b89a4d9

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +122 -0
  2. label_encoders.pkl +3 -0
  3. random_forest_model.pkl +3 -0
  4. scaler.pkl +3 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import pickle
4
+ from sklearn.impute import SimpleImputer
5
+ from sklearn.utils.validation import check_is_fitted
6
+
7
+ # Load the trained model and preprocessing objects using pickle
8
+ with open('random_forest_model.pkl', 'rb') as f:
9
+ random_forest_model = pickle.load(f)
10
+
11
+ with open('scaler.pkl', 'rb') as f:
12
+ scaler = pickle.load(f)
13
+
14
+ with open('label_encoders.pkl', 'rb') as f:
15
+ label_encoders = pickle.load(f)
16
+
17
+ # State corrections and valid states/UTs
18
+ state_corrections = {
19
+ 'uttaranchal': 'uttarakhand',
20
+ 'orissa (odisha)': 'odisha',
21
+ 'kashmir': 'jammu and kashmir',
22
+ 'multi state': 'other',
23
+ 'not classified': 'other'
24
+ }
25
+
26
+ valid_states_uts = [
27
+ 'andhra pradesh', 'arunachal pradesh', 'assam', 'bihar', 'chhattisgarh', 'goa',
28
+ 'gujarat', 'haryana', 'himachal pradesh', 'jharkhand', 'karnataka', 'kerala',
29
+ 'madhya pradesh', 'maharashtra', 'manipur', 'meghalaya', 'mizoram', 'nagaland',
30
+ 'odisha', 'punjab', 'rajasthan', 'sikkim', 'tamil nadu', 'telangana', 'tripura',
31
+ 'uttar pradesh', 'uttarakhand', 'west bengal', 'andaman and nicobar islands',
32
+ 'chandigarh', 'dadra and nagar haveli and daman and diu', 'lakshadweep', 'delhi',
33
+ 'puducherry', 'jammu and kashmir', 'ladakh'
34
+ ]
35
+
36
+ # Extract city, state, and country
37
+ def extract_city(x):
38
+ if isinstance(x, str):
39
+ splitted_string = x.split("-")
40
+ if len(splitted_string) == 4:
41
+ return f"{splitted_string[0].strip().lower()} {splitted_string[1].strip().lower()}"
42
+ else:
43
+ return splitted_string[0].strip().lower()
44
+ else:
45
+ return "other"
46
+
47
+ def extract_state(x):
48
+ if isinstance(x, str):
49
+ state = x.split("-")[-2].strip().lower()
50
+ return state_corrections.get(state, state if state in valid_states_uts else 'other')
51
+ else:
52
+ return "other"
53
+
54
+ def extract_country(x):
55
+ if isinstance(x, str):
56
+ return x.split("-")[-1].strip().lower()
57
+ else:
58
+ return "other"
59
+
60
+ def preprocess_new_data(df):
61
+ df['Ownership'] = df['Ownership'].str.lower().str.strip()
62
+ df[' Type of Tender '] = df[' Type of Tender '].str.lower().str.strip()
63
+
64
+ def parse_closing_date(date_str):
65
+ try:
66
+ return pd.to_datetime(date_str)
67
+ except Exception:
68
+ if " to " in date_str:
69
+ date_str = date_str.split(" to ")[-1]
70
+ return pd.to_datetime(date_str, errors='coerce')
71
+ return pd.NaT
72
+
73
+ df['Closing Date'] = df['Closing Date'].apply(parse_closing_date)
74
+ df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
75
+ df['days_left'] = (df['Closing Date'] - df['Date']).dt.days
76
+
77
+ df['city'] = df['Location'].apply(lambda x: extract_city(x))
78
+ df['state'] = df['Location'].apply(lambda x: extract_state(x))
79
+ df['country'] = df['Location'].apply(lambda x: extract_country(x))
80
+
81
+ df['city'].fillna("other", inplace=True)
82
+ df['state'].fillna("other", inplace=True)
83
+ df['country'].fillna("other", inplace=True)
84
+
85
+ df = df[['Ref No', 'Earnest Money', 'Estimated Cost', 'DocFees', 'Ownership', ' Type of Tender ', 'days_left', 'city', 'state', 'country']]
86
+
87
+ imputer = SimpleImputer(strategy='median')
88
+ df['days_left'] = imputer.fit_transform(df[['days_left']])
89
+
90
+ for column in ['Ownership', ' Type of Tender ', 'city', 'state', 'country']:
91
+ le = label_encoders[column]
92
+ df[column] = df[column].apply(lambda x: x if x in le.classes_ else 'other')
93
+ df[column] = le.transform(df[column])
94
+
95
+ numerical_features = ['Earnest Money', 'Estimated Cost', 'DocFees', 'days_left']
96
+ df[numerical_features] = scaler.transform(df[numerical_features])
97
+
98
+ return df
99
+
100
+ def predict_new_data(new_data):
101
+ preprocessed_data = preprocess_new_data(new_data)
102
+ X_new = preprocessed_data.drop(columns=['Ref No'])
103
+ tender_ref_numbers_new = preprocessed_data['Ref No']
104
+ predictions = random_forest_model.predict(X_new)
105
+ results = pd.DataFrame({
106
+ 'Ref No': tender_ref_numbers_new,
107
+ 'Selected': predictions
108
+ })
109
+
110
+ return results
111
+
112
+ st.title("Tender Selection Prediction")
113
+ uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
114
+
115
+ if uploaded_file is not None:
116
+ new_data = pd.read_csv(uploaded_file)
117
+ prediction_results = predict_new_data(new_data)
118
+ selected_tenders = prediction_results[prediction_results['Selected'] == "yes"]['Ref No'].astype(str).to_list()
119
+ new_data['Ref No'] = new_data['Ref No'].astype(str)
120
+
121
+ st.write("Selected Tenders:")
122
+ st.write(new_data[new_data['Ref No'].isin(selected_tenders)].drop(columns=['Unnamed: 0']).reset_index().drop(columns=['index']))
label_encoders.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:61fdd47acff081b0938b383ed2a372cbf1ca7eb42fa58717f9e1d966cbbea3c6
3
+ size 19704
random_forest_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bc78b9a55f882d8580dcc8971148ab856ba55dfd76d39660d5291dcff10cc3ac
3
+ size 153909782
scaler.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:baba225548cb3c0765e59d932acb0b4e035bb5c759e3473a524674af63931235
3
+ size 688