Spaces:
Sleeping
Sleeping
hackerbyhobby
commited on
corrected app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,53 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
import json
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
)
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
import json
|
4 |
+
from sklearn.compose import ColumnTransformer
|
5 |
+
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
6 |
+
from io import StringIO
|
7 |
+
|
8 |
+
# Load selected features from JSON file
|
9 |
+
with open("selected_features.json", "r") as file:
|
10 |
+
selected_features = json.load(file)
|
11 |
+
|
12 |
+
def preprocess_data(dataframe):
|
13 |
+
# Identify numerical and categorical columns
|
14 |
+
numerical_cols = dataframe.select_dtypes(include=["number"]).columns
|
15 |
+
categorical_cols = [col for col in dataframe.columns if col not in numerical_cols]
|
16 |
+
|
17 |
+
# Preprocessing pipeline
|
18 |
+
preprocessor = ColumnTransformer(
|
19 |
+
transformers=[
|
20 |
+
('num', StandardScaler(), numerical_cols),
|
21 |
+
('cat', OneHotEncoder(sparse_output=False, drop='first'), categorical_cols)
|
22 |
+
]
|
23 |
+
)
|
24 |
+
|
25 |
+
# Apply preprocessing
|
26 |
+
processed_data = preprocessor.fit_transform(dataframe)
|
27 |
+
feature_names = numerical_cols.tolist() + list(preprocessor.named_transformers_['cat'].get_feature_names_out(categorical_cols))
|
28 |
+
return pd.DataFrame(processed_data, columns=feature_names)
|
29 |
+
|
30 |
+
def process_uploaded_data(file):
|
31 |
+
# Load dataset from uploaded file
|
32 |
+
data = pd.read_csv(file.name)
|
33 |
+
|
34 |
+
# Check for missing selected features
|
35 |
+
missing_features = [feature for feature in selected_features if feature not in data.columns]
|
36 |
+
if missing_features:
|
37 |
+
return f"Missing features: {', '.join(missing_features)}. Please upload a valid dataset."
|
38 |
+
|
39 |
+
# Preprocess data
|
40 |
+
data = data[selected_features]
|
41 |
+
processed_data = preprocess_data(data)
|
42 |
+
return processed_data.head(10).to_csv(index=False)
|
43 |
+
|
44 |
+
def process_manual_data(**inputs):
|
45 |
+
# Construct dataframe from manual inputs
|
46 |
+
input_data = pd.DataFrame([inputs])
|
47 |
+
|
48 |
+
# Preprocess data
|
49 |
+
processed_data = preprocess_data(input_data)
|
50 |
+
return processed_data.head(10).to_csv(index=False)
|
51 |
+
|
52 |
+
# GUI for manual input
|
53 |
+
manual
|