Snigs98 commited on
Commit
126f3be
·
verified ·
1 Parent(s): 38ab4dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -71
app.py CHANGED
@@ -1,14 +1,17 @@
1
- import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
  import zipfile
5
  import os
6
- from sklearn.preprocessing import StandardScaler
 
 
7
  from sklearn.ensemble import RandomForestClassifier
 
 
8
 
9
  # Extract ZIP file
10
- zip_file_path = "LUNG_CANCER.zip"
11
- extract_folder = "LUNG_CANCER_DATA"
12
  with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
13
  zip_ref.extractall(extract_folder)
14
 
@@ -23,79 +26,52 @@ df['LUNG_CANCER'] = df['LUNG_CANCER'].map({'YES': 1, 'NO': 0})
23
  # Splitting dataset
24
  X = df.drop(columns=['LUNG_CANCER'])
25
  y = df['LUNG_CANCER']
 
 
 
 
 
26
 
27
  # Scaling features
28
  scaler = StandardScaler()
29
- X_scaled = scaler.fit_transform(X)
 
30
 
31
- # Train model
32
- model = RandomForestClassifier(n_estimators=100, random_state=42)
33
- model.fit(X_scaled, y)
34
 
35
- # Prediction function
36
- def predict_lung_cancer(GENDER, AGE, SMOKING, YELLOW_FINGERS, ANXIETY, PEER_PRESSURE, CHRONIC_DISEASE,
37
- FATIGUE, ALLERGY, WHEEZING, ALCOHOL_CONSUMING, COUGHING, SHORTNESS_OF_BREATH,
38
- SWALLOWING_DIFFICULTY, CHEST_PAIN):
39
-
40
- # Gender Encoding: Male -> 0, Female -> 1
41
- gender_value = 0 if GENDER.lower() == "male" else 1
42
-
43
- # Convert "Yes" -> 1, "No" -> 0
44
- feature_values = [gender_value, AGE] + [
45
- 1 if x.lower() == "yes" else 0 for x in [
46
- SMOKING, YELLOW_FINGERS, ANXIETY, PEER_PRESSURE, CHRONIC_DISEASE,
47
- FATIGUE, ALLERGY, WHEEZING, ALCOHOL_CONSUMING, COUGHING, SHORTNESS_OF_BREATH,
48
- SWALLOWING_DIFFICULTY, CHEST_PAIN
49
- ]
50
- ]
51
-
52
- # Convert to NumPy array and Reshape
53
- features = np.array([feature_values])
54
-
55
- # **Apply the same scaling transformation used during training**
56
- features_scaled = scaler.transform(features)
57
-
58
- # **Predict using the trained model**
59
- prediction = model.predict(features_scaled)
60
-
61
- return "Lung Cancer Detected" if prediction[0] == 1 else "No Lung Cancer"
62
- # Create GUI with Gradio
63
- with gr.Blocks() as demo:
64
- gr.Markdown("# 🩺 Lung Cancer Detection AI")
65
- gr.Markdown("Provide patient details and check if lung cancer is detected.")
66
-
67
- with gr.Row():
68
- gender = gr.Radio(["Male", "Female"], label="Gender")
69
- age = gr.Slider(20, 90, step=1, label="Age")
70
-
71
- with gr.Row():
72
- smoking = gr.Radio(["No", "Yes"], label="Smoking")
73
- yellow_fingers = gr.Radio(["No", "Yes"], label="Yellow Fingers")
74
- anxiety = gr.Radio(["No", "Yes"], label="Anxiety")
75
- peer_pressure = gr.Radio(["No", "Yes"], label="Peer Pressure")
76
- chronic_disease = gr.Radio(["No", "Yes"], label="Chronic Disease")
77
-
78
- with gr.Row():
79
- fatigue = gr.Radio(["No", "Yes"], label="Fatigue")
80
- allergy = gr.Radio(["No", "Yes"], label="Allergy")
81
- wheezing = gr.Radio(["No", "Yes"], label="Wheezing")
82
- alcohol_consuming = gr.Radio(["No", "Yes"], label="Alcohol Consuming")
83
- coughing = gr.Radio(["No", "Yes"], label="Coughing")
84
 
85
- with gr.Row():
86
- shortness_of_breath = gr.Radio(["No", "Yes"], label="Shortness of Breath")
87
- swallowing_difficulty = gr.Radio(["No", "Yes"], label="Swallowing Difficulty")
88
- chest_pain = gr.Radio(["No", "Yes"], label="Chest Pain")
89
-
90
- predict_button = gr.Button("Predict")
91
- output_text = gr.Textbox(label="Prediction Result")
92
 
93
- predict_button.click(
94
- predict_lung_cancer,
95
- inputs=[gender, age, smoking, yellow_fingers, anxiety, peer_pressure, chronic_disease,
96
- fatigue, allergy, wheezing, alcohol_consuming, coughing, shortness_of_breath,
97
- swallowing_difficulty, chest_pain],
98
- outputs=output_text
99
- )
 
 
 
 
 
 
 
 
 
 
 
100
 
 
101
  demo.launch()
 
 
1
  import pandas as pd
2
  import numpy as np
3
  import zipfile
4
  import os
5
+ import gradio as gr
6
+ from sklearn.model_selection import train_test_split
7
+ from sklearn.preprocessing import LabelEncoder, StandardScaler
8
  from sklearn.ensemble import RandomForestClassifier
9
+ from sklearn.metrics import accuracy_score, classification_report
10
+ from imblearn.over_sampling import SMOTE
11
 
12
  # Extract ZIP file
13
+ zip_file_path = "LUNG CANCER.zip"
14
+ extract_folder = "./LUNG_CANCER_DATA"
15
  with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
16
  zip_ref.extractall(extract_folder)
17
 
 
26
  # Splitting dataset
27
  X = df.drop(columns=['LUNG_CANCER'])
28
  y = df['LUNG_CANCER']
29
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
30
+
31
+ # Handling class imbalance
32
+ smote = SMOTE(random_state=42)
33
+ X_train_resampled, y_train_resampled = smote.fit_resample(X_train, y_train)
34
 
35
  # Scaling features
36
  scaler = StandardScaler()
37
+ X_train_resampled = scaler.fit_transform(X_train_resampled)
38
+ X_test = scaler.transform(X_test)
39
 
40
+ # Model training
41
+ model = RandomForestClassifier(n_estimators=200, random_state=42)
42
+ model.fit(X_train_resampled, y_train_resampled)
43
 
44
+ # Model evaluation
45
+ y_pred = model.predict(X_test)
46
+ accuracy = accuracy_score(y_test, y_pred)
47
+ print(f"Model Accuracy: {accuracy:.2f}")
48
+ print("Classification Report:\n", classification_report(y_test, y_pred))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ # Gradio Prediction Function
51
+ def predict_lung_cancer(*features):
52
+ features = np.array(features).reshape(1, -1)
53
+ features = scaler.transform(features)
54
+ prediction = model.predict(features)
55
+ return "Lung Cancer Detected" if prediction[0] == 1 else "No Lung Cancer"
 
56
 
57
+ # Gradio Interface
58
+ inputs = [
59
+ gr.Number(label="Gender (0: Male, 1: Female)"),
60
+ gr.Number(label="Age"),
61
+ gr.Number(label="Smoking"),
62
+ gr.Number(label="Yellow Fingers"),
63
+ gr.Number(label="Anxiety"),
64
+ gr.Number(label="Peer Pressure"),
65
+ gr.Number(label="Chronic Disease"),
66
+ gr.Number(label="Fatigue"),
67
+ gr.Number(label="Allergy"),
68
+ gr.Number(label="Wheezing"),
69
+ gr.Number(label="Alcohol Consuming"),
70
+ gr.Number(label="Coughing"),
71
+ gr.Number(label="Shortness of Breath"),
72
+ gr.Number(label="Swallowing Difficulty"),
73
+ gr.Number(label="Chest Pain")
74
+ ]
75
 
76
+ demo = gr.Interface(fn=predict_lung_cancer, inputs=inputs, outputs="text", title="Lung Cancer Prediction")
77
  demo.launch()