Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
class diamonds_model(nn.Module):
|
| 8 |
+
def __init__(self, input_size, output_size):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.layer_1 = nn.Linear(in_features=input_size, out_features=15)
|
| 11 |
+
self.layer_2 = nn.Linear(in_features=15, out_features=12)
|
| 12 |
+
self.layer_3 = nn.Linear(in_features=12, out_features=8)
|
| 13 |
+
self.layer_4 = nn.Linear(in_features=8, out_features=output_size)
|
| 14 |
+
self.relu = nn.ReLU()
|
| 15 |
+
self.softmax = nn.Softmax(dim=1)
|
| 16 |
+
|
| 17 |
+
def forward(self, x):
|
| 18 |
+
x = self.relu(self.layer_1(x))
|
| 19 |
+
x = self.relu(self.layer_2(x))
|
| 20 |
+
x = self.relu(self.layer_3(x))
|
| 21 |
+
x = self.layer_4(x)
|
| 22 |
+
x = self.softmax(x)
|
| 23 |
+
return x
|
| 24 |
+
|
| 25 |
+
# Chargement du modèle
|
| 26 |
+
try:
|
| 27 |
+
model = torch.load("modeleANN.pth", map_location=torch.device('cpu'))
|
| 28 |
+
model.eval()
|
| 29 |
+
input_size = model.layer_1.in_features
|
| 30 |
+
output_size = model.layer_4.out_features
|
| 31 |
+
st.write("Modèle chargé avec succès.") #confirmation du chargement.
|
| 32 |
+
except FileNotFoundError:
|
| 33 |
+
st.error("Le fichier du modèle (modeleANN.pth) n'a pas été trouvé.")
|
| 34 |
+
st.stop()
|
| 35 |
+
except Exception as e:
|
| 36 |
+
st.error(f"Une erreur s'est produite lors du chargement du modèle : {e}")
|
| 37 |
+
st.stop()
|
| 38 |
+
|
| 39 |
+
# Interface Streamlit
|
| 40 |
+
st.title("Prédiction de la qualité de la coupe du diamant")
|
| 41 |
+
st.write("Entrez les caractéristiques du diamant:")
|
| 42 |
+
|
| 43 |
+
# Création des entrées utilisateur
|
| 44 |
+
inputs = [st.number_input(f"Feature {i+1}", value=0.0) for i in range(input_size)]
|
| 45 |
+
|
| 46 |
+
# Prédiction
|
| 47 |
+
if st.button("Prédire"):
|
| 48 |
+
inputs_tensor = torch.tensor([inputs], dtype=torch.float32)
|
| 49 |
+
with torch.no_grad():
|
| 50 |
+
prediction = model(inputs_tensor.unsqueeze(0)) #Ajout de la dimension batch
|
| 51 |
+
probabilities = prediction.numpy()[0]
|
| 52 |
+
predicted_class = np.argmax(probabilities)
|
| 53 |
+
|
| 54 |
+
class_labels = ["Classe 1", "Classe 2", "Classe 3", "Classe 4", "Classe 5"] # Remplacez par vos labels réels
|
| 55 |
+
|
| 56 |
+
st.write("Probabilités de classe :")
|
| 57 |
+
for i, prob in enumerate(probabilities):
|
| 58 |
+
st.write(f"{class_labels[i]}: {prob:.4f}")
|
| 59 |
+
|
| 60 |
+
st.write(f"Classe prédite: {class_labels[predicted_class]}")
|