Sujatha commited on
Commit
06451a2
·
verified ·
1 Parent(s): f9800c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -4,24 +4,24 @@ from pytorch_tabular import TabularModel
4
  from pytorch_tabular.config import DataConfig, TrainerConfig
5
  from pytorch_tabular.models import CategoryEmbeddingModelConfig
6
 
7
- # Sample Data
8
  data = {
9
- 'feature1': [0.5, 0.3, 0.7, 0.2],
10
- 'feature2': [1, 0, 1, 1],
11
- 'feature3': [0.6, 0.1, 0.8, 0.4],
12
- 'target': [0, 1, 0, 1] # Binary classification target
13
  }
14
  df = pd.DataFrame(data)
15
 
16
  # Configure pytorch_tabular
17
  data_config = DataConfig(
18
- target=["target"],
19
- continuous_cols=["feature1", "feature2", "feature3"]
20
  )
21
 
22
  model_config = CategoryEmbeddingModelConfig(
23
  task="classification",
24
- layers="64-64",
25
  learning_rate=1e-3
26
  )
27
 
@@ -41,29 +41,28 @@ except ValueError as e:
41
  print(f"Error initializing TabularModel: {e}")
42
 
43
  # Define Inference Function
44
- def classify(feature1, feature2, feature3):
45
  input_data = pd.DataFrame({
46
- "feature1": [feature1],
47
- "feature2": [feature2],
48
- "feature3": [feature3]
49
  })
50
  prediction = tabular_model.predict(input_data)["prediction"].iloc[0]
51
- return "Class 1" if prediction == 1 else "Class 0"
52
 
53
  # Gradio Interface
54
  iface = gr.Interface(
55
  fn=classify,
56
  inputs=[
57
- gr.Slider(0, 1, step=0.1, label="Feature 1"),
58
- gr.Slider(0, 1, step=0.1, label="Feature 2"),
59
- gr.Slider(0, 1, step=0.1, label="Feature 3")
60
  ],
61
  outputs="text",
62
- title="Tabular Classification with PyTorch Tabular",
63
- description="Classify entries based on tabular data"
64
  )
65
 
66
  # Launch the Gradio app with necessary server settings
67
  print("Launching Gradio Interface...")
68
  iface.launch(server_name="0.0.0.0", server_port=7860, share=True)
69
-
 
4
  from pytorch_tabular.config import DataConfig, TrainerConfig
5
  from pytorch_tabular.models import CategoryEmbeddingModelConfig
6
 
7
+ # Sample meaningful data for loan approval prediction
8
  data = {
9
+ 'age': [25, 45, 35, 50, 23, 63, 41, 34],
10
+ 'income': [50000, 120000, 75000, 60000, 48000, 150000, 90000, 80000],
11
+ 'credit_score': [650, 720, 680, 690, 640, 780, 710, 660],
12
+ 'approved': [1, 1, 0, 0, 0, 1, 1, 0] # Binary target: 1 for approved, 0 for rejected
13
  }
14
  df = pd.DataFrame(data)
15
 
16
  # Configure pytorch_tabular
17
  data_config = DataConfig(
18
+ target=["approved"],
19
+ continuous_cols=["age", "income", "credit_score"]
20
  )
21
 
22
  model_config = CategoryEmbeddingModelConfig(
23
  task="classification",
24
+ layers="64-64", # Hidden layer sizes for the model
25
  learning_rate=1e-3
26
  )
27
 
 
41
  print(f"Error initializing TabularModel: {e}")
42
 
43
  # Define Inference Function
44
+ def classify(age, income, credit_score):
45
  input_data = pd.DataFrame({
46
+ "age": [age],
47
+ "income": [income],
48
+ "credit_score": [credit_score]
49
  })
50
  prediction = tabular_model.predict(input_data)["prediction"].iloc[0]
51
+ return "Loan Approved" if prediction == 1 else "Loan Rejected"
52
 
53
  # Gradio Interface
54
  iface = gr.Interface(
55
  fn=classify,
56
  inputs=[
57
+ gr.Slider(18, 70, step=1, label="Age"),
58
+ gr.Slider(30000, 200000, step=5000, label="Annual Income"),
59
+ gr.Slider(300, 850, step=10, label="Credit Score")
60
  ],
61
  outputs="text",
62
+ title="Loan Approval Prediction",
63
+ description="Predicts loan approval based on age, income, and credit score."
64
  )
65
 
66
  # Launch the Gradio app with necessary server settings
67
  print("Launching Gradio Interface...")
68
  iface.launch(server_name="0.0.0.0", server_port=7860, share=True)