Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
from
|
4 |
-
from
|
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 |
-
'
|
14 |
}
|
15 |
df = pd.DataFrame(data)
|
16 |
-
dataset = Dataset.from_pandas(df)
|
17 |
|
18 |
-
# Configure
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
)
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
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 = {
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
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
|
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 |
+
|