Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,152 +1,171 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
strutture_target = {
|
| 9 |
-
"Dock-Rigenerato": {
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"max_lunghezza": 160,
|
| 13 |
-
"size": (200,80,60) # L x W x H in mm
|
| 14 |
-
},
|
| 15 |
-
"Oggetto-Vetrina": {
|
| 16 |
-
"max_volume": 200,
|
| 17 |
-
"max_area": 80,
|
| 18 |
-
"max_lunghezza": 130,
|
| 19 |
-
"size": (150,100,80)
|
| 20 |
-
},
|
| 21 |
-
"Tool educativo": {
|
| 22 |
-
"max_volume": 300,
|
| 23 |
-
"max_area": 100,
|
| 24 |
-
"max_lunghezza": 200,
|
| 25 |
-
"size": (250,120,100)
|
| 26 |
-
},
|
| 27 |
}
|
| 28 |
|
| 29 |
-
def
|
| 30 |
score = 0
|
| 31 |
-
if row[
|
| 32 |
-
if row[
|
| 33 |
-
if row[
|
| 34 |
return score
|
| 35 |
|
| 36 |
-
def assegna_struttura(
|
| 37 |
-
|
|
|
|
| 38 |
best = max(punteggi, key=punteggi.get)
|
| 39 |
return best
|
| 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 |
-
def
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
st.
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
st.subheader("
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
from torch.utils.data import DataLoader, TensorDataset
|
| 8 |
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
# =========================
|
| 12 |
+
# STRUTTURE TARGET (compatibilità)
|
| 13 |
+
# =========================
|
| 14 |
strutture_target = {
|
| 15 |
+
"Dock-Rigenerato": {"max_volume": 250, "max_area": 60, "max_lunghezza": 160},
|
| 16 |
+
"Oggetto-Vetrina": {"max_volume": 200, "max_area": 80, "max_lunghezza": 130},
|
| 17 |
+
"Tool educativo": {"max_volume": 300, "max_area": 100, "max_lunghezza": 200},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
+
def valuta_compatibilità(row, target):
|
| 21 |
score = 0
|
| 22 |
+
if row[0] <= target["max_volume"]: score += 1
|
| 23 |
+
if row[1] <= target["max_area"]: score += 1
|
| 24 |
+
if row[2] <= target["max_lunghezza"]: score += 1
|
| 25 |
return score
|
| 26 |
|
| 27 |
+
def assegna_struttura(x):
|
| 28 |
+
# x = [volume, area, lunghezza, materiale, rigidita, usura]
|
| 29 |
+
punteggi = {k: valuta_compatibilità(x, v) for k,v in strutture_target.items()}
|
| 30 |
best = max(punteggi, key=punteggi.get)
|
| 31 |
return best
|
| 32 |
|
| 33 |
+
# =========================
|
| 34 |
+
# VAE DEFINIZIONE
|
| 35 |
+
# =========================
|
| 36 |
+
class VAE(nn.Module):
|
| 37 |
+
def __init__(self, input_dim=6, latent_dim=2):
|
| 38 |
+
super(VAE, self).__init__()
|
| 39 |
+
self.fc1 = nn.Linear(input_dim, 12)
|
| 40 |
+
self.fc21 = nn.Linear(12, latent_dim)
|
| 41 |
+
self.fc22 = nn.Linear(12, latent_dim)
|
| 42 |
+
self.fc3 = nn.Linear(latent_dim, 12)
|
| 43 |
+
self.fc4 = nn.Linear(12, input_dim)
|
| 44 |
+
|
| 45 |
+
def encode(self, x):
|
| 46 |
+
h1 = F.relu(self.fc1(x))
|
| 47 |
+
return self.fc21(h1), self.fc22(h1)
|
| 48 |
+
|
| 49 |
+
def reparameterize(self, mu, logvar):
|
| 50 |
+
std = torch.exp(0.5 * logvar)
|
| 51 |
+
eps = torch.randn_like(std)
|
| 52 |
+
return mu + eps * std
|
| 53 |
+
|
| 54 |
+
def decode(self, z):
|
| 55 |
+
h3 = F.relu(self.fc3(z))
|
| 56 |
+
return self.fc4(h3)
|
| 57 |
+
|
| 58 |
+
def forward(self, x):
|
| 59 |
+
mu, logvar = self.encode(x)
|
| 60 |
+
z = self.reparameterize(mu, logvar)
|
| 61 |
+
return self.decode(z), mu, logvar
|
| 62 |
+
|
| 63 |
+
def vae_loss(recon_x, x, mu, logvar):
|
| 64 |
+
mse = F.mse_loss(recon_x, x, reduction='sum')
|
| 65 |
+
kld = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
|
| 66 |
+
return mse + kld
|
| 67 |
+
|
| 68 |
+
# ============== STREAMLIT SETUP ==============
|
| 69 |
+
st.set_page_config(page_title="Weeko AI + Overlay", layout="wide")
|
| 70 |
+
st.title("✨ WEEKO – OR6.3 Demo: AI dimensionale + Overlay Estetico")
|
| 71 |
+
|
| 72 |
+
tabs = st.tabs(["🧠 AI Dimensionale", "🎨 Overlay Estetico"])
|
| 73 |
+
|
| 74 |
+
# --------------------------------------------
|
| 75 |
+
# TAB 1: AI Dimensionale (VAE)
|
| 76 |
+
# --------------------------------------------
|
| 77 |
+
with tabs[0]:
|
| 78 |
+
st.subheader("AI Dimensionale con VAE (6 feature)")
|
| 79 |
+
|
| 80 |
+
st.markdown("""
|
| 81 |
+
**Scenario**: Abbiamo 6 feature: [volume, area, lunghezza, materiale, rigidità, usura].
|
| 82 |
+
- Addestriamo al volo un piccolo VAE su dati simulati
|
| 83 |
+
- Visualizziamo la ricostruzione e assegniamo a ciascun sample la struttura più compatibile
|
| 84 |
+
""")
|
| 85 |
+
|
| 86 |
+
if st.button("🏋️ Train VAE su dati dummy"):
|
| 87 |
+
# 1) Creiamo dataset fittizio
|
| 88 |
+
torch.manual_seed(42)
|
| 89 |
+
n = 200
|
| 90 |
+
volume = torch.randn(n,1)*10 + 50
|
| 91 |
+
area = torch.randn(n,1)*5 + 20
|
| 92 |
+
lung = torch.randn(n,1)*30 + 100
|
| 93 |
+
materiale = torch.randint(0,3,(n,1)).float()
|
| 94 |
+
rigidita = torch.rand(n,1)
|
| 95 |
+
usura = torch.rand(n,1)
|
| 96 |
+
data = torch.cat([volume, area, lung, materiale, rigidita, usura], dim=1)
|
| 97 |
+
|
| 98 |
+
dataset = TensorDataset(data)
|
| 99 |
+
loader = DataLoader(dataset, batch_size=32, shuffle=True)
|
| 100 |
+
|
| 101 |
+
# 2) Inizializziamo e train
|
| 102 |
+
vae = VAE()
|
| 103 |
+
optimizer = torch.optim.Adam(vae.parameters(), lr=1e-3)
|
| 104 |
+
n_epochs = 30
|
| 105 |
+
losses = []
|
| 106 |
+
for epoch in range(n_epochs):
|
| 107 |
+
total_loss = 0
|
| 108 |
+
for (batch,) in loader:
|
| 109 |
+
optimizer.zero_grad()
|
| 110 |
+
recon, mu, logvar = vae(batch)
|
| 111 |
+
loss = vae_loss(recon, batch, mu, logvar)
|
| 112 |
+
loss.backward()
|
| 113 |
+
optimizer.step()
|
| 114 |
+
total_loss += loss.item()
|
| 115 |
+
losses.append(total_loss)
|
| 116 |
+
|
| 117 |
+
st.success("Addestramento completato. Ultimo epoch loss: {:.2f}".format(losses[-1]))
|
| 118 |
+
|
| 119 |
+
# 3) Generiamo riconstruction
|
| 120 |
+
data_np = data.numpy()
|
| 121 |
+
with torch.no_grad():
|
| 122 |
+
recon, _, _ = vae(data)
|
| 123 |
+
recon_np = recon.numpy()
|
| 124 |
+
|
| 125 |
+
# 4) Assegniamo struttura a original e ricostruito
|
| 126 |
+
df = pd.DataFrame(data_np, columns=["volume","area","lunghezza","materiale","rigidita","usura"])
|
| 127 |
+
df["struttura_compat"] = df.apply(lambda row: assegna_struttura(row.values), axis=1)
|
| 128 |
+
|
| 129 |
+
df_rec = pd.DataFrame(recon_np, columns=["volume","area","lunghezza","materiale","rigidita","usura"])
|
| 130 |
+
df_rec["struttura_compat"] = df_rec.apply(lambda row: assegna_struttura(row.values), axis=1)
|
| 131 |
+
|
| 132 |
+
st.write("### Esempio di 10 sample originali (Sx) e ricostruiti (Dx)")
|
| 133 |
+
joined = pd.concat([
|
| 134 |
+
df.head(10).round(2),
|
| 135 |
+
df_rec.head(10).round(2)
|
| 136 |
+
], axis=1, keys=["Original","Ricostruito"])
|
| 137 |
+
st.dataframe(joined)
|
| 138 |
+
|
| 139 |
+
st.write("Struttura compatibile (originale) vs (ricostruito):")
|
| 140 |
+
st.dataframe(pd.DataFrame({
|
| 141 |
+
"Original": df["struttura_compat"].head(10).values,
|
| 142 |
+
"Ricostruito": df_rec["struttura_compat"].head(10).values
|
| 143 |
+
}))
|
| 144 |
+
|
| 145 |
+
# --------------------------------------------
|
| 146 |
+
# TAB 2: Overlay Estetico
|
| 147 |
+
# --------------------------------------------
|
| 148 |
+
with tabs[1]:
|
| 149 |
+
st.subheader("Overlay Estetico: Componente EoL su Oggetto Finale")
|
| 150 |
+
|
| 151 |
+
st.write("Carica due foto: (1) EoL (es. lamiera), (2) oggetto finale. Regola la trasparenza per un 'effetto wow'.")
|
| 152 |
+
|
| 153 |
+
col1, col2 = st.columns(2)
|
| 154 |
+
with col1:
|
| 155 |
+
eol_file = st.file_uploader("Foto EoL (jpg/png)", type=["jpg","png"], key="eol_upload")
|
| 156 |
+
with col2:
|
| 157 |
+
final_file = st.file_uploader("Oggetto finale (jpg/png)", type=["jpg","png"], key="final_upload")
|
| 158 |
+
|
| 159 |
+
if eol_file and final_file:
|
| 160 |
+
from PIL import Image
|
| 161 |
+
lamiera_img = Image.open(eol_file).convert("RGBA")
|
| 162 |
+
oggetto_img = Image.open(final_file).convert("RGBA")
|
| 163 |
+
|
| 164 |
+
# ridimensiona la lamiera alle dimensioni dell'oggetto
|
| 165 |
+
lamiera_img = lamiera_img.resize(oggetto_img.size)
|
| 166 |
+
|
| 167 |
+
alpha = st.slider("Trasparenza componente EoL", 0.0, 1.0, 0.5)
|
| 168 |
+
blended = Image.blend(oggetto_img, lamiera_img, alpha)
|
| 169 |
+
st.image(blended, caption="Overlay lamiera + oggetto finale", use_column_width=True)
|
| 170 |
+
else:
|
| 171 |
+
st.info("Attendi di caricare entrambe le immagini")
|