Sujatha commited on
Commit
7b36568
·
verified ·
1 Parent(s): 3527269

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -38
app.py CHANGED
@@ -1,56 +1,42 @@
1
  import gradio as gr
2
  import pandas as pd
3
- from transformers import TabularTransformerForSequenceClassification, TabularTransformerConfig
4
- from transformers import Trainer, TrainingArguments
5
- from datasets import Dataset
6
- import torch
7
 
8
  # Sample Data
9
  data = {
10
  'feature1': [0.5, 0.3, 0.7, 0.2],
11
  'feature2': [1, 0, 1, 1],
12
  'feature3': [0.6, 0.1, 0.8, 0.4],
13
- 'label': [0, 1, 0, 1] # Binary classification
14
  }
15
  df = pd.DataFrame(data)
16
- dataset = Dataset.from_pandas(df)
17
 
18
- # Configure the Model
19
- config = TabularTransformerConfig(
20
- num_labels=2, # Binary classification
21
- numerical_features=['feature1', 'feature2', 'feature3']
22
  )
23
- model = TabularTransformerForSequenceClassification(config)
24
-
25
- # Define Training Arguments
26
- training_args = TrainingArguments(
27
- output_dir="./results",
28
- evaluation_strategy="epoch",
29
- learning_rate=2e-5,
30
- per_device_train_batch_size=4,
31
- num_train_epochs=3
32
- )
33
-
34
- # Define Trainer
35
- trainer = Trainer(
36
- model=model,
37
- args=training_args,
38
- train_dataset=dataset,
39
- eval_dataset=dataset
40
  )
41
-
42
- # Train the model
43
- trainer.train()
44
 
45
  # Define Inference Function
46
  def classify(feature1, feature2, feature3):
47
- input_data = {'feature1': feature1, 'feature2': feature2, 'feature3': feature3}
48
- input_df = pd.DataFrame([input_data])
49
- test_dataset = Dataset.from_pandas(input_df)
50
- with torch.no_grad():
51
- logits = model(**test_dataset[:][0]).logits
52
- prediction = torch.argmax(logits, dim=1).item()
53
- return "Class 1" if prediction == 1 else "Class 0"
54
 
55
  # Gradio Interface
56
  iface = gr.Interface(
@@ -61,8 +47,9 @@ iface = gr.Interface(
61
  gr.inputs.Slider(0, 1, step=0.1, label="Feature 3")
62
  ],
63
  outputs="text",
64
- title="Tabular Classification with Hugging Face",
65
  description="Classify entries based on tabular data"
66
  )
67
 
68
  iface.launch()
 
 
1
  import gradio as gr
2
  import pandas as pd
3
+ from pytorch_tabular import TabularModel
4
+ from pytorch_tabular.config import DataConfig, ModelConfig, TrainerConfig
 
 
5
 
6
  # Sample Data
7
  data = {
8
  'feature1': [0.5, 0.3, 0.7, 0.2],
9
  'feature2': [1, 0, 1, 1],
10
  'feature3': [0.6, 0.1, 0.8, 0.4],
11
+ 'target': [0, 1, 0, 1] # Binary classification target
12
  }
13
  df = pd.DataFrame(data)
 
14
 
15
+ # Configure pytorch_tabular
16
+ data_config = DataConfig(
17
+ target=["target"],
18
+ continuous_cols=["feature1", "feature2", "feature3"]
19
  )
20
+ model_config = ModelConfig(task="classification", num_classes=2)
21
+ trainer_config = TrainerConfig(max_epochs=10)
22
+
23
+ # Initialize and train model
24
+ tabular_model = TabularModel(
25
+ data_config=data_config,
26
+ model_config=model_config,
27
+ trainer_config=trainer_config
 
 
 
 
 
 
 
 
 
28
  )
29
+ tabular_model.fit(df)
 
 
30
 
31
  # Define Inference Function
32
  def classify(feature1, feature2, feature3):
33
+ input_data = pd.DataFrame({
34
+ "feature1": [feature1],
35
+ "feature2": [feature2],
36
+ "feature3": [feature3]
37
+ })
38
+ prediction = tabular_model.predict(input_data)["prediction"].iloc[0]
39
+ return "Class 1" if prediction == 1 else "Class 0"
40
 
41
  # Gradio Interface
42
  iface = gr.Interface(
 
47
  gr.inputs.Slider(0, 1, step=0.1, label="Feature 3")
48
  ],
49
  outputs="text",
50
+ title="Tabular Classification with PyTorch Tabular",
51
  description="Classify entries based on tabular data"
52
  )
53
 
54
  iface.launch()
55
+