valegro commited on
Commit
f054d36
·
verified ·
1 Parent(s): a9a378e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import streamlit as st
4
+ import torch
5
+ import torch.nn as nn
6
+ import torch.nn.functional as F
7
+ import pandas as pd
8
+ import numpy as np
9
+
10
+ st.title("🧠 Weeko Configurator - AI per dispositivi ICT rigenerati")
11
+
12
+ # 📦 Step 1 – Simulazione dataset componenti
13
+ def genera_dataset(n=100):
14
+ torch.manual_seed(42)
15
+ volume = torch.randn(n, 1) * 10 + 50
16
+ area = torch.randn(n, 1) * 5 + 20
17
+ lunghezza = torch.randn(n, 1) * 30 + 100
18
+ materiale = torch.randint(0, 3, (n, 1)).float()
19
+ rigidita = torch.rand(n, 1)
20
+ usura = torch.rand(n, 1)
21
+ return torch.cat([volume, area, lunghezza, materiale, rigidita, usura], dim=1)
22
+
23
+ # 🧠 Step 2 – VAE molto semplice
24
+ class VAE(nn.Module):
25
+ def __init__(self, input_dim=6, latent_dim=2):
26
+ super(VAE, self).__init__()
27
+ self.fc1 = nn.Linear(input_dim, 12)
28
+ self.fc21 = nn.Linear(12, latent_dim)
29
+ self.fc22 = nn.Linear(12, latent_dim)
30
+ self.fc3 = nn.Linear(latent_dim, 12)
31
+ self.fc4 = nn.Linear(12, input_dim)
32
+
33
+ def encode(self, x):
34
+ h1 = F.relu(self.fc1(x))
35
+ return self.fc21(h1), self.fc22(h1)
36
+
37
+ def reparameterize(self, mu, logvar):
38
+ std = torch.exp(0.5 * logvar)
39
+ eps = torch.randn_like(std)
40
+ return mu + eps * std
41
+
42
+ def decode(self, z):
43
+ h3 = F.relu(self.fc3(z))
44
+ return self.fc4(h3)
45
+
46
+ def forward(self, x):
47
+ mu, logvar = self.encode(x)
48
+ z = self.reparameterize(mu, logvar)
49
+ return self.decode(z)
50
+
51
+ # 🧪 Step 3 – Compatibilità con strutture target
52
+ strutture_target = {
53
+ "Dock-Rigenerato": {"max_volume": 250, "max_area": 60, "max_lunghezza": 160},
54
+ "Oggetto-Vetrina": {"max_volume": 200, "max_area": 80, "max_lunghezza": 130},
55
+ "Tool educativo": {"max_volume": 300, "max_area": 100, "max_lunghezza": 200},
56
+ }
57
+
58
+ def valuta_compatibilità(x, target):
59
+ score = 0
60
+ if x[0] <= target["max_volume"]: score += 1
61
+ if x[1] <= target["max_area"]: score += 1
62
+ if x[2] <= target["max_lunghezza"]: score += 1
63
+ return score
64
+
65
+ def assegna_struttura(x):
66
+ punteggi = {k: valuta_compatibilità(x, v) for k, v in strutture_target.items()}
67
+ return max(punteggi, key=punteggi.get)
68
+
69
+ # 🎛️ Step 4 – Interfaccia
70
+ if st.button("🎲 Genera Configurazioni"):
71
+ vae = VAE()
72
+ dati_input = genera_dataset()
73
+ with torch.no_grad():
74
+ decoded = vae(dati_input).numpy()
75
+
76
+ df = pd.DataFrame(decoded, columns=["Volume", "Area", "Lunghezza", "Materiale", "Rigidità", "Usura"])
77
+ df["Struttura Compatibile"] = df.apply(lambda row: assegna_struttura(row.values), axis=1)
78
+
79
+ st.success("✅ Configurazioni generate!")
80
+ st.dataframe(df.round(2))
81
+ csv = df.to_csv(index=False).encode('utf-8')
82
+ st.download_button("📥 Scarica CSV", csv, "weeko_config.csv", "text/csv")