hackerbyhobby commited on
Commit
6dfb9dc
·
unverified ·
1 Parent(s): 58cf43a

corrected app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -47
app.py CHANGED
@@ -1,49 +1,53 @@
1
-
2
  import gradio as gr
3
- import joblib
4
  import json
5
- import numpy as np
6
-
7
- # Load the trained model
8
- model = joblib.load('trained_model.pkl')
9
-
10
- # Load feature names
11
- with open('selected_features.json', 'r') as f:
12
- feature_names = json.load(f)
13
-
14
- # Define prediction function
15
- def predict_heart_failure(*args):
16
- """
17
- Predict heart failure likelihood based on input features.
18
-
19
- Args:
20
- *args: Input values for each feature.
21
-
22
- Returns:
23
- str: Prediction result.
24
- str: Prediction probability as a percentage.
25
- """
26
- input_data = np.array(args).reshape(1, -1)
27
- prediction = model.predict(input_data)
28
- probability = model.predict_proba(input_data)
29
- result = "Likely" if prediction[0] == 1 else "Unlikely"
30
- return result, f"{probability[0][1] * 100:.2f}%"
31
-
32
- # Create Gradio interface
33
- inputs = [gr.inputs.Number(label=feature) for feature in feature_names]
34
- outputs = [
35
- gr.outputs.Textbox(label="Prediction"),
36
- gr.outputs.Textbox(label="Probability (%)")
37
- ]
38
-
39
- app = gr.Interface(
40
- fn=predict_heart_failure,
41
- inputs=inputs,
42
- outputs=outputs,
43
- title="Heart Failure Prediction",
44
- description="Enter the values for the features to predict the likelihood of heart failure."
45
- )
46
-
47
- # Launch the app
48
- if __name__ == "__main__":
49
- app.launch()
 
 
 
 
 
 
 
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