Spaces:
Build error
Build error
Update Home.py
Browse files
Home.py
CHANGED
|
@@ -4,102 +4,102 @@ from keras.models import load_model
|
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
st.set_page_config(page_title="
|
| 9 |
|
| 10 |
-
#
|
| 11 |
st.markdown("""
|
| 12 |
<style>
|
| 13 |
-
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@
|
| 14 |
|
| 15 |
html, body, [class*="css"] {
|
| 16 |
-
font-family: '
|
| 17 |
-
background: linear-gradient(to right, #
|
| 18 |
color: white;
|
| 19 |
}
|
| 20 |
|
| 21 |
h1 {
|
| 22 |
font-family: 'Orbitron', sans-serif;
|
| 23 |
-
color: #
|
| 24 |
-
text-align: center;
|
| 25 |
font-size: 3em;
|
| 26 |
-
text-
|
| 27 |
-
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
| 31 |
text-align: center;
|
| 32 |
-
font-size:
|
| 33 |
-
color: #
|
|
|
|
| 34 |
margin-bottom: 30px;
|
| 35 |
}
|
| 36 |
|
| 37 |
-
.canvas-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
.prediction-box {
|
| 48 |
-
margin-top: 25px;
|
| 49 |
-
padding: 15px;
|
| 50 |
font-size: 2.2em;
|
| 51 |
-
font-weight:
|
| 52 |
-
color: #00ffff;
|
| 53 |
-
background: rgba(255, 255, 255, 0.07);
|
| 54 |
-
border-radius: 12px;
|
| 55 |
text-align: center;
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
.emoji {
|
| 60 |
text-align: center;
|
| 61 |
font-size: 3em;
|
| 62 |
-
margin-top:
|
| 63 |
}
|
| 64 |
</style>
|
| 65 |
""", unsafe_allow_html=True)
|
| 66 |
|
| 67 |
-
# Title
|
| 68 |
st.markdown("<h1>Digit Recognizer</h1>", unsafe_allow_html=True)
|
| 69 |
-
st.markdown("<
|
| 70 |
|
| 71 |
-
# Sidebar
|
| 72 |
-
st.sidebar.
|
| 73 |
-
drawing_mode = st.sidebar.selectbox("
|
| 74 |
stroke_width = st.sidebar.slider("Stroke Width", 1, 25, 10)
|
| 75 |
stroke_color = st.sidebar.color_picker("Stroke Color", "#FFFFFF")
|
| 76 |
bg_color = st.sidebar.color_picker("Background Color", "#000000")
|
| 77 |
-
realtime_update = st.sidebar.checkbox("Update
|
| 78 |
|
| 79 |
-
# Load
|
| 80 |
@st.cache_resource
|
| 81 |
def load_mnist_model():
|
| 82 |
return load_model("digit_recognization.keras")
|
| 83 |
|
| 84 |
model = load_mnist_model()
|
| 85 |
|
| 86 |
-
# Canvas Area
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# Prediction
|
| 103 |
if canvas_result.image_data is not None:
|
| 104 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 105 |
img_resized = cv2.resize(img, (28, 28))
|
|
@@ -109,7 +109,7 @@ if canvas_result.image_data is not None:
|
|
| 109 |
prediction = model.predict(img_reshaped)
|
| 110 |
predicted_digit = np.argmax(prediction)
|
| 111 |
|
| 112 |
-
# Show prediction result
|
| 113 |
st.markdown(f"<div class='prediction-box'>Prediction: {predicted_digit}</div>", unsafe_allow_html=True)
|
| 114 |
st.markdown(f"<div class='emoji'>{['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣'][predicted_digit]}</div>", unsafe_allow_html=True)
|
| 115 |
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
+
# --- Page Config ---
|
| 8 |
+
st.set_page_config(page_title="Digit AI", layout="centered")
|
| 9 |
|
| 10 |
+
# --- Custom UI Styling ---
|
| 11 |
st.markdown("""
|
| 12 |
<style>
|
| 13 |
+
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@500&family=Outfit:wght@300;400;600&display=swap');
|
| 14 |
|
| 15 |
html, body, [class*="css"] {
|
| 16 |
+
font-family: 'Outfit', sans-serif;
|
| 17 |
+
background: linear-gradient(to right, #090909, #1f1c2c);
|
| 18 |
color: white;
|
| 19 |
}
|
| 20 |
|
| 21 |
h1 {
|
| 22 |
font-family: 'Orbitron', sans-serif;
|
| 23 |
+
color: #00fff7;
|
|
|
|
| 24 |
font-size: 3em;
|
| 25 |
+
text-align: center;
|
| 26 |
+
text-shadow: 0 0 10px #00fff7;
|
| 27 |
+
margin-bottom: 0;
|
| 28 |
}
|
| 29 |
|
| 30 |
+
p {
|
| 31 |
text-align: center;
|
| 32 |
+
font-size: 1.1em;
|
| 33 |
+
color: #ccc;
|
| 34 |
+
margin-top: 0;
|
| 35 |
margin-bottom: 30px;
|
| 36 |
}
|
| 37 |
|
| 38 |
+
.canvas-wrapper {
|
| 39 |
+
display: flex;
|
| 40 |
+
justify-content: center;
|
| 41 |
+
background: rgba(255,255,255,0.05);
|
| 42 |
+
padding: 25px;
|
| 43 |
+
border-radius: 25px;
|
| 44 |
+
box-shadow: 0 8px 24px rgba(0,255,255,0.1);
|
| 45 |
+
backdrop-filter: blur(10px);
|
| 46 |
+
margin-bottom: 30px;
|
| 47 |
}
|
| 48 |
|
| 49 |
.prediction-box {
|
|
|
|
|
|
|
| 50 |
font-size: 2.2em;
|
| 51 |
+
font-weight: 600;
|
|
|
|
|
|
|
|
|
|
| 52 |
text-align: center;
|
| 53 |
+
color: #00fff7;
|
| 54 |
+
background: rgba(255, 255, 255, 0.05);
|
| 55 |
+
padding: 20px;
|
| 56 |
+
border-radius: 15px;
|
| 57 |
+
box-shadow: 0 0 15px rgba(0,255,255,0.4);
|
| 58 |
}
|
| 59 |
|
| 60 |
.emoji {
|
| 61 |
text-align: center;
|
| 62 |
font-size: 3em;
|
| 63 |
+
margin-top: 10px;
|
| 64 |
}
|
| 65 |
</style>
|
| 66 |
""", unsafe_allow_html=True)
|
| 67 |
|
| 68 |
+
# --- Title + Description ---
|
| 69 |
st.markdown("<h1>Digit Recognizer</h1>", unsafe_allow_html=True)
|
| 70 |
+
st.markdown("<p>Draw a digit (0–9) below and see what the AI thinks it is!</p>", unsafe_allow_html=True)
|
| 71 |
|
| 72 |
+
# --- Sidebar Settings ---
|
| 73 |
+
st.sidebar.markdown("### ✏️ Drawing Settings")
|
| 74 |
+
drawing_mode = st.sidebar.selectbox("Tool", ("freedraw", "line", "rect", "circle", "transform"))
|
| 75 |
stroke_width = st.sidebar.slider("Stroke Width", 1, 25, 10)
|
| 76 |
stroke_color = st.sidebar.color_picker("Stroke Color", "#FFFFFF")
|
| 77 |
bg_color = st.sidebar.color_picker("Background Color", "#000000")
|
| 78 |
+
realtime_update = st.sidebar.checkbox("Update Realtime", True)
|
| 79 |
|
| 80 |
+
# --- Load Model ---
|
| 81 |
@st.cache_resource
|
| 82 |
def load_mnist_model():
|
| 83 |
return load_model("digit_recognization.keras")
|
| 84 |
|
| 85 |
model = load_mnist_model()
|
| 86 |
|
| 87 |
+
# --- Canvas Drawing Area ---
|
| 88 |
+
st.markdown('<div class="canvas-wrapper">', unsafe_allow_html=True)
|
| 89 |
+
canvas_result = st_canvas(
|
| 90 |
+
fill_color="rgba(255, 255, 255, 0.05)",
|
| 91 |
+
stroke_width=stroke_width,
|
| 92 |
+
stroke_color=stroke_color,
|
| 93 |
+
background_color=bg_color,
|
| 94 |
+
update_streamlit=realtime_update,
|
| 95 |
+
height=280,
|
| 96 |
+
width=280,
|
| 97 |
+
drawing_mode=drawing_mode,
|
| 98 |
+
key="canvas"
|
| 99 |
+
)
|
| 100 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 101 |
+
|
| 102 |
+
# --- Prediction Display ---
|
|
|
|
| 103 |
if canvas_result.image_data is not None:
|
| 104 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 105 |
img_resized = cv2.resize(img, (28, 28))
|
|
|
|
| 109 |
prediction = model.predict(img_reshaped)
|
| 110 |
predicted_digit = np.argmax(prediction)
|
| 111 |
|
|
|
|
| 112 |
st.markdown(f"<div class='prediction-box'>Prediction: {predicted_digit}</div>", unsafe_allow_html=True)
|
| 113 |
st.markdown(f"<div class='emoji'>{['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣'][predicted_digit]}</div>", unsafe_allow_html=True)
|
| 114 |
|
| 115 |
+
|