Upload AppANN.py
Browse files
AppANN.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Charger le modèle sauvegardé
|
| 9 |
+
MODEL_PATH = "modeleANN.pth" # Remplace par le vrai chemin
|
| 10 |
+
|
| 11 |
+
class diamonds_model(nn.Module):
|
| 12 |
+
def __init__(self):
|
| 13 |
+
super().__init__()
|
| 14 |
+
|
| 15 |
+
self.layer_1 = nn.Linear(in_features=9, out_features=15)
|
| 16 |
+
self.layer_2 = nn.Linear(in_features=15, out_features=12)
|
| 17 |
+
self.layer_3 = nn.Linear(in_features=12, out_features=8)
|
| 18 |
+
self.layer_4 = nn.Linear(in_features=8, out_features=5)
|
| 19 |
+
self.relu = nn.ReLU()
|
| 20 |
+
def forward(self, x):
|
| 21 |
+
x = self.relu(self.layer_1(x))
|
| 22 |
+
x = self.relu(self.layer_2(x))
|
| 23 |
+
x = self.relu(self.layer_3(x))
|
| 24 |
+
x = self.layer_4(x)
|
| 25 |
+
return x
|
| 26 |
+
|
| 27 |
+
model = diamonds_model()
|
| 28 |
+
|
| 29 |
+
# Définition du modèle
|
| 30 |
+
class LinearRegression(nn.Module):
|
| 31 |
+
def __init__(self, input_size):
|
| 32 |
+
super(LinearRegression, self).__init__()
|
| 33 |
+
self.linear = nn.Linear(input_size, 1)
|
| 34 |
+
|
| 35 |
+
def forward(self, x):
|
| 36 |
+
return self.linear(x)
|
| 37 |
+
|
| 38 |
+
# Charger le modèle complet
|
| 39 |
+
model = torch.load(MODEL_PATH, map_location=torch.device('cpu'))
|
| 40 |
+
model.eval()
|
| 41 |
+
|
| 42 |
+
# Définir les noms des colonnes d'entrée
|
| 43 |
+
feature_columns = ["carat", "depth", "table", "price", "x", "y", "z", "Color", "Clarity"]
|
| 44 |
+
|
| 45 |
+
# Définir les classes de sortie
|
| 46 |
+
class_labels = ['Fair', 'Good', 'Ideal', 'Premium', 'Very Good']
|
| 47 |
+
|
| 48 |
+
st.title("Prédiction de la qualité du diamant")
|
| 49 |
+
st.write("Entrez les caractéristiques du diamant pour prédire sa qualité.")
|
| 50 |
+
|
| 51 |
+
# Interface utilisateur pour entrer les valeurs des features
|
| 52 |
+
st.sidebar.header("Entrée des caractéristiques")
|
| 53 |
+
features = {}
|
| 54 |
+
|
| 55 |
+
col1, col2 = st.sidebar.columns(2)
|
| 56 |
+
|
| 57 |
+
for i, col in enumerate(feature_columns):
|
| 58 |
+
if i % 2 == 0:
|
| 59 |
+
features[col] = col1.number_input(f"{col}", value=0.0)
|
| 60 |
+
else:
|
| 61 |
+
features[col] = col2.number_input(f"{col}", value=0.0)
|
| 62 |
+
|
| 63 |
+
if st.sidebar.button("Prédire"):
|
| 64 |
+
input_tensor = torch.tensor([list(features.values())], dtype=torch.float32)
|
| 65 |
+
prediction_index = torch.argmax(model(input_tensor), dim=1).item()
|
| 66 |
+
predicted_class = class_labels[prediction_index]
|
| 67 |
+
st.subheader(f"Ce diamant entre dans la categorie: {predicted_class}")
|
| 68 |
+
st.balloons()
|