File size: 1,993 Bytes
4b771a1
 
7b36568
192e408
 
4b771a1
 
 
 
 
 
7b36568
4b771a1
 
 
0901b66
7b36568
 
0901b66
 
4b771a1
0901b66
 
 
 
 
 
 
 
 
4b771a1
0901b66
 
 
 
 
 
 
 
 
 
 
4b771a1
 
 
7b36568
 
 
 
 
 
 
4b771a1
 
 
 
 
 
 
 
 
 
7b36568
4b771a1
 
 
b47b2b3
3dea44a
b47b2b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import pandas as pd
from pytorch_tabular import TabularModel
from pytorch_tabular.config import DataConfig, TrainerConfig
from pytorch_tabular.models import CategoryEmbeddingModelConfig

# Sample Data
data = {
    'feature1': [0.5, 0.3, 0.7, 0.2],
    'feature2': [1, 0, 1, 1],
    'feature3': [0.6, 0.1, 0.8, 0.4],
    'target': [0, 1, 0, 1]  # Binary classification target
}
df = pd.DataFrame(data)

# Ensure all configurations are set correctly
data_config = DataConfig(
    target=["target"],
    continuous_cols=["feature1", "feature2", "feature3"],
    task="classification"
)

model_config = CategoryEmbeddingModelConfig(
    task="classification",
    layers="64-64",  # Example hidden layer sizes
    learning_rate=1e-3
)

trainer_config = TrainerConfig(
    max_epochs=10
)

# Initialize and train the model
try:
    tabular_model = TabularModel(
        data_config=data_config,
        model_config=model_config,
        trainer_config=trainer_config
    )
    tabular_model.fit(df)
except ValueError as e:
    print(f"Error initializing TabularModel: {e}")

# Define Inference Function
def classify(feature1, feature2, feature3):
    input_data = pd.DataFrame({
        "feature1": [feature1],
        "feature2": [feature2],
        "feature3": [feature3]
    })
    prediction = tabular_model.predict(input_data)["prediction"].iloc[0]
    return "Class 1" if prediction == 1 else "Class 0"

# Gradio Interface
iface = gr.Interface(
    fn=classify,
    inputs=[
        gr.inputs.Slider(0, 1, step=0.1, label="Feature 1"),
        gr.inputs.Slider(0, 1, step=0.1, label="Feature 2"),
        gr.inputs.Slider(0, 1, step=0.1, label="Feature 3")
    ],
    outputs="text",
    title="Tabular Classification with PyTorch Tabular",
    description="Classify entries based on tabular data"
)

# Launch with additional server settings for Hugging Face Spaces
print("Launching Gradio Interface...")
iface.launch(server_name="0.0.0.0", server_port=7860, share=True)