Spaces:
Runtime error
Runtime error
scontess
commited on
Commit
Β·
1ae86c6
1
Parent(s):
9a948a7
ss
Browse files- requirements.txt +9 -2
- src/streamlit_app.py +112 -38
requirements.txt
CHANGED
|
@@ -1,3 +1,10 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
pandas
|
| 3 |
-
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
tensorflow
|
| 3 |
+
tensorflow-datasets
|
| 4 |
+
matplotlib
|
| 5 |
+
numpy
|
| 6 |
+
scikit-learn
|
| 7 |
+
seaborn
|
| 8 |
+
huggingface_hub
|
| 9 |
pandas
|
| 10 |
+
python-dotenv
|
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,114 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import tensorflow_datasets as tfds
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import numpy as np
|
| 5 |
+
import time
|
| 6 |
+
import tensorflow.keras as keras
|
| 7 |
+
from tensorflow.keras.applications import VGG16
|
| 8 |
+
from tensorflow.keras.layers import Dense, Flatten
|
| 9 |
+
from tensorflow.keras.models import Model, load_model
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
from sklearn.metrics import confusion_matrix
|
| 12 |
+
import seaborn as sns
|
| 13 |
+
from huggingface_hub import HfApi
|
| 14 |
+
import os
|
| 15 |
+
|
| 16 |
+
# π Percorso della cartella dove Γ¨ salvato il dataset
|
| 17 |
+
DATA_DIR = "/app/src"
|
| 18 |
+
|
| 19 |
+
# π Autenticazione Hugging Face dal Secret nello Space
|
| 20 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 21 |
+
|
| 22 |
+
if HF_TOKEN:
|
| 23 |
+
api = HfApi()
|
| 24 |
+
user_info = api.whoami(HF_TOKEN)
|
| 25 |
+
|
| 26 |
+
if "name" in user_info:
|
| 27 |
+
st.write(f"β
Autenticato come {user_info['name']}")
|
| 28 |
+
else:
|
| 29 |
+
st.warning("β οΈ Token API non valido! Controlla il Secret nello Space.")
|
| 30 |
+
else:
|
| 31 |
+
st.warning("β οΈ Nessun token API trovato! Verifica il Secret nello Space.")
|
| 32 |
+
|
| 33 |
+
# π Carica solo 300 immagini da ImageNet
|
| 34 |
+
st.write("π Caricamento di 300 immagini da ImageNet...")
|
| 35 |
+
imagenet = tfds.load("imagenet2012", split="train", as_supervised=True, download=False, data_dir=DATA_DIR)
|
| 36 |
+
|
| 37 |
+
image_list = []
|
| 38 |
+
label_list = []
|
| 39 |
+
|
| 40 |
+
for i, (image, label) in enumerate(imagenet.take(300)): # Prende solo 300 immagini
|
| 41 |
+
image = tf.image.resize(image, (224, 224)) / 255.0 # Normalizzazione
|
| 42 |
+
image_list.append(image.numpy())
|
| 43 |
+
label_list.append(label.numpy())
|
| 44 |
+
|
| 45 |
+
X_train = np.array(image_list)
|
| 46 |
+
y_train = np.array(label_list)
|
| 47 |
+
|
| 48 |
+
st.write(f"β
Scaricate e preprocessate {len(X_train)} immagini da ImageNet!")
|
| 49 |
+
|
| 50 |
+
# π Caricamento del modello
|
| 51 |
+
if os.path.exists("Silva.h5"):
|
| 52 |
+
model = load_model("Silva.h5")
|
| 53 |
+
st.write("β
Modello `Silva.h5` caricato, nessun nuovo training necessario!")
|
| 54 |
+
else:
|
| 55 |
+
st.write("π Training in corso perchΓ© `Silva.h5` non esiste...")
|
| 56 |
+
|
| 57 |
+
# Caricare il modello VGG16 pre-addestrato
|
| 58 |
+
base_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3))
|
| 59 |
+
|
| 60 |
+
# Congelare i livelli convoluzionali
|
| 61 |
+
for layer in base_model.layers:
|
| 62 |
+
layer.trainable = False
|
| 63 |
+
|
| 64 |
+
# Aggiungere nuovi livelli Dense
|
| 65 |
+
x = Flatten()(base_model.output)
|
| 66 |
+
x = Dense(256, activation="relu")(x)
|
| 67 |
+
x = Dense(128, activation="relu")(x)
|
| 68 |
+
output = Dense(len(set(y_train)), activation="softmax")(x)
|
| 69 |
+
|
| 70 |
+
# Creare il modello finale
|
| 71 |
+
model = Model(inputs=base_model.input, outputs=output)
|
| 72 |
+
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
|
| 73 |
+
|
| 74 |
+
# π Barra di progresso del training
|
| 75 |
+
progress_bar = st.progress(0)
|
| 76 |
+
status_text = st.empty()
|
| 77 |
+
start_time = time.time()
|
| 78 |
+
|
| 79 |
+
# π Addestramento con progress bar
|
| 80 |
+
for epoch in range(10):
|
| 81 |
+
history = model.fit(X_train, y_train, epochs=1)
|
| 82 |
+
progress_bar.progress((epoch + 1) / 10)
|
| 83 |
+
elapsed_time = time.time() - start_time
|
| 84 |
+
status_text.text(f"β³ Tempo rimanente stimato: {int(elapsed_time / (epoch + 1) * (10 - (epoch + 1)))} secondi")
|
| 85 |
+
|
| 86 |
+
st.write("β
Addestramento completato!")
|
| 87 |
+
|
| 88 |
+
# π Salvare il modello
|
| 89 |
+
model.save("Silva.h5")
|
| 90 |
+
st.write("β
Modello salvato come `Silva.h5`!")
|
| 91 |
+
|
| 92 |
+
# π Bottone per scaricare il modello
|
| 93 |
+
if os.path.exists("Silva.h5"):
|
| 94 |
+
with open("Silva.h5", "rb") as f:
|
| 95 |
+
st.download_button(
|
| 96 |
+
label="π₯ Scarica il modello Silva.h5",
|
| 97 |
+
data=f,
|
| 98 |
+
file_name="Silva.h5",
|
| 99 |
+
mime="application/octet-stream"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Bottone per caricare il modello su Hugging Face
|
| 103 |
+
def upload_model():
|
| 104 |
+
api.upload_file(
|
| 105 |
+
path_or_fileobj="Silva.h5",
|
| 106 |
+
path_in_repo="Silva.h5",
|
| 107 |
+
repo_id="scontess/Silva",
|
| 108 |
+
repo_type="model"
|
| 109 |
+
)
|
| 110 |
+
st.success("β
Modello 'Silva' caricato su Hugging Face!")
|
| 111 |
|
| 112 |
+
st.write("π₯ Carica il modello Silva su Hugging Face")
|
| 113 |
+
if st.button("π Carica Silva su Model Store"):
|
| 114 |
+
upload_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|