Spaces:
Build error
Build error
Update Home.py
Browse files
Home.py
CHANGED
|
@@ -7,7 +7,7 @@ import cv2
|
|
| 7 |
# --- Page Configuration ---
|
| 8 |
st.set_page_config(page_title="Digit Recognizer", layout="centered")
|
| 9 |
|
| 10 |
-
# --- Custom CSS
|
| 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');
|
|
@@ -35,21 +35,6 @@ p {
|
|
| 35 |
margin-bottom: 35px;
|
| 36 |
}
|
| 37 |
|
| 38 |
-
.center-wrapper {
|
| 39 |
-
display: flex;
|
| 40 |
-
justify-content: center;
|
| 41 |
-
align-items: center;
|
| 42 |
-
padding: 30px 0;
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
.canvas-container {
|
| 46 |
-
background: rgba(255, 255, 255, 0.05);
|
| 47 |
-
padding: 25px;
|
| 48 |
-
border-radius: 20px;
|
| 49 |
-
box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
|
| 50 |
-
backdrop-filter: blur(10px);
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
.prediction-box {
|
| 54 |
font-size: 2.2em;
|
| 55 |
font-weight: 600;
|
|
@@ -70,11 +55,11 @@ p {
|
|
| 70 |
</style>
|
| 71 |
""", unsafe_allow_html=True)
|
| 72 |
|
| 73 |
-
# ---
|
| 74 |
st.markdown("<h1>Digit Recognizer</h1>", unsafe_allow_html=True)
|
| 75 |
st.markdown("<p>Draw a digit from 0–9 below. The AI will predict what you wrote.</p>", unsafe_allow_html=True)
|
| 76 |
|
| 77 |
-
# --- Sidebar
|
| 78 |
st.sidebar.title("✏️ Drawing Settings")
|
| 79 |
drawing_mode = st.sidebar.selectbox("Drawing Tool", ("freedraw", "line", "rect", "circle", "transform"))
|
| 80 |
stroke_width = st.sidebar.slider("Stroke Width", 1, 25, 10)
|
|
@@ -82,15 +67,24 @@ stroke_color = st.sidebar.color_picker("Stroke Color", "#FFFFFF")
|
|
| 82 |
bg_color = st.sidebar.color_picker("Background Color", "#000000")
|
| 83 |
realtime_update = st.sidebar.checkbox("Update Realtime", True)
|
| 84 |
|
| 85 |
-
# --- Load
|
| 86 |
@st.cache_resource
|
| 87 |
def load_mnist_model():
|
| 88 |
return load_model("digit_recognization.keras")
|
| 89 |
|
| 90 |
model = load_mnist_model()
|
| 91 |
|
| 92 |
-
# --- Centered Canvas Pad ---
|
| 93 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
canvas_result = st_canvas(
|
| 96 |
fill_color="rgba(255, 255, 255, 0.1)",
|
|
@@ -104,9 +98,9 @@ canvas_result = st_canvas(
|
|
| 104 |
key="canvas"
|
| 105 |
)
|
| 106 |
|
| 107 |
-
st.markdown(
|
| 108 |
|
| 109 |
-
# --- Prediction
|
| 110 |
if canvas_result.image_data is not None:
|
| 111 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 112 |
img_resized = cv2.resize(img, (28, 28))
|
|
@@ -118,3 +112,4 @@ if canvas_result.image_data is not None:
|
|
| 118 |
|
| 119 |
st.markdown(f"<div class='prediction-box'>Prediction: {predicted_digit}</div>", unsafe_allow_html=True)
|
| 120 |
st.markdown(f"<div class='emoji'>{['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣'][predicted_digit]}</div>", unsafe_allow_html=True)
|
|
|
|
|
|
| 7 |
# --- Page Configuration ---
|
| 8 |
st.set_page_config(page_title="Digit Recognizer", layout="centered")
|
| 9 |
|
| 10 |
+
# --- Custom CSS 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');
|
|
|
|
| 35 |
margin-bottom: 35px;
|
| 36 |
}
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
.prediction-box {
|
| 39 |
font-size: 2.2em;
|
| 40 |
font-weight: 600;
|
|
|
|
| 55 |
</style>
|
| 56 |
""", unsafe_allow_html=True)
|
| 57 |
|
| 58 |
+
# --- Title + Instructions ---
|
| 59 |
st.markdown("<h1>Digit Recognizer</h1>", unsafe_allow_html=True)
|
| 60 |
st.markdown("<p>Draw a digit from 0–9 below. The AI will predict what you wrote.</p>", unsafe_allow_html=True)
|
| 61 |
|
| 62 |
+
# --- Sidebar Controls ---
|
| 63 |
st.sidebar.title("✏️ Drawing Settings")
|
| 64 |
drawing_mode = st.sidebar.selectbox("Drawing Tool", ("freedraw", "line", "rect", "circle", "transform"))
|
| 65 |
stroke_width = st.sidebar.slider("Stroke Width", 1, 25, 10)
|
|
|
|
| 67 |
bg_color = st.sidebar.color_picker("Background Color", "#000000")
|
| 68 |
realtime_update = st.sidebar.checkbox("Update Realtime", True)
|
| 69 |
|
| 70 |
+
# --- Load Model ---
|
| 71 |
@st.cache_resource
|
| 72 |
def load_mnist_model():
|
| 73 |
return load_model("digit_recognization.keras")
|
| 74 |
|
| 75 |
model = load_mnist_model()
|
| 76 |
|
| 77 |
+
# --- Centered Canvas Drawing Pad ---
|
| 78 |
+
st.markdown("""
|
| 79 |
+
<div style="display: flex; justify-content: center; align-items: center; padding: 30px 0;">
|
| 80 |
+
<div style="
|
| 81 |
+
background: rgba(255, 255, 255, 0.05);
|
| 82 |
+
padding: 25px;
|
| 83 |
+
border-radius: 20px;
|
| 84 |
+
box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
|
| 85 |
+
backdrop-filter: blur(10px);
|
| 86 |
+
">
|
| 87 |
+
""", unsafe_allow_html=True)
|
| 88 |
|
| 89 |
canvas_result = st_canvas(
|
| 90 |
fill_color="rgba(255, 255, 255, 0.1)",
|
|
|
|
| 98 |
key="canvas"
|
| 99 |
)
|
| 100 |
|
| 101 |
+
st.markdown("</div></div>", unsafe_allow_html=True)
|
| 102 |
|
| 103 |
+
# --- Prediction Output ---
|
| 104 |
if canvas_result.image_data is not None:
|
| 105 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 106 |
img_resized = cv2.resize(img, (28, 28))
|
|
|
|
| 112 |
|
| 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 |
+
|