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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
if uploaded_file is not None:
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
pred = model.predict(img_array)
|
| 29 |
-
pred_label = np.argmax(pred)
|
| 30 |
-
|
| 31 |
-
# Show results
|
| 32 |
-
st.image(img, caption=f"Predicted Digit: {pred_label}", width=150)
|
| 33 |
-
st.write("Prediction Probabilities:", pred)
|
|
|
|
| 1 |
+
# ---- PAGE CONFIG ----
|
| 2 |
+
st.set_page_config(
|
| 3 |
+
page_title="Digit Recognition App",
|
| 4 |
+
page_icon="βοΈ",
|
| 5 |
+
layout="wide"
|
| 6 |
+
)
|
| 7 |
|
| 8 |
+
# ---- CUSTOM CSS STYLING ----
|
| 9 |
+
st.markdown("""
|
| 10 |
+
<style>
|
| 11 |
+
/* Import Google Fonts */
|
| 12 |
+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap');
|
| 13 |
+
|
| 14 |
+
/* Global Styles */
|
| 15 |
+
.stApp {
|
| 16 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 17 |
+
font-family: 'Poppins', sans-serif;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
/* Main container */
|
| 21 |
+
.main-container {
|
| 22 |
+
background: rgba(255, 255, 255, 0.95);
|
| 23 |
+
backdrop-filter: blur(10px);
|
| 24 |
+
border-radius: 20px;
|
| 25 |
+
padding: 2rem;
|
| 26 |
+
margin: 1rem;
|
| 27 |
+
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
|
| 28 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
/* Header styling */
|
| 32 |
+
.app-header {
|
| 33 |
+
text-align: center;
|
| 34 |
+
background: linear-gradient(135deg, #667eea, #764ba2);
|
| 35 |
+
-webkit-background-clip: text;
|
| 36 |
+
-webkit-text-fill-color: transparent;
|
| 37 |
+
background-clip: text;
|
| 38 |
+
font-size: 3rem;
|
| 39 |
+
font-weight: 700;
|
| 40 |
+
margin-bottom: 1rem;
|
| 41 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.app-subtitle {
|
| 45 |
+
text-align: center;
|
| 46 |
+
color: #666;
|
| 47 |
+
font-size: 1.1rem;
|
| 48 |
+
font-weight: 400;
|
| 49 |
+
margin-bottom: 2rem;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
/* Sidebar styling */
|
| 53 |
+
.css-1d391kg {
|
| 54 |
+
background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
.sidebar-content {
|
| 58 |
+
background: rgba(255, 255, 255, 0.1);
|
| 59 |
+
backdrop-filter: blur(10px);
|
| 60 |
+
border-radius: 15px;
|
| 61 |
+
padding: 1.5rem;
|
| 62 |
+
margin: 1rem 0;
|
| 63 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
.sidebar-header {
|
| 67 |
+
color: white;
|
| 68 |
+
font-size: 1.3rem;
|
| 69 |
+
font-weight: 600;
|
| 70 |
+
margin-bottom: 1rem;
|
| 71 |
+
text-align: center;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
.sidebar-text {
|
| 75 |
+
color: rgba(255, 255, 255, 0.9);
|
| 76 |
+
font-size: 0.95rem;
|
| 77 |
+
line-height: 1.6;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
/* Upload section */
|
| 81 |
+
.upload-section {
|
| 82 |
+
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 83 |
+
padding: 2rem;
|
| 84 |
+
border-radius: 20px;
|
| 85 |
+
margin: 2rem 0;
|
| 86 |
+
text-align: center;
|
| 87 |
+
box-shadow: 0 15px 30px rgba(240, 147, 251, 0.3);
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
.upload-title {
|
| 91 |
+
color: white;
|
| 92 |
+
font-size: 1.5rem;
|
| 93 |
+
font-weight: 600;
|
| 94 |
+
margin-bottom: 1rem;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
/* Results section */
|
| 98 |
+
.results-container {
|
| 99 |
+
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
| 100 |
+
padding: 2rem;
|
| 101 |
+
border-radius: 20px;
|
| 102 |
+
margin: 1rem 0;
|
| 103 |
+
box-shadow: 0 15px 30px rgba(79, 172, 254, 0.3);
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
.prediction-result {
|
| 107 |
+
background: white;
|
| 108 |
+
color: #333;
|
| 109 |
+
font-size: 2.5rem;
|
| 110 |
+
font-weight: 700;
|
| 111 |
+
text-align: center;
|
| 112 |
+
padding: 1rem;
|
| 113 |
+
border-radius: 15px;
|
| 114 |
+
margin: 1rem 0;
|
| 115 |
+
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
|
| 116 |
+
border-left: 5px solid #4facfe;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
/* Image container */
|
| 120 |
+
.image-container {
|
| 121 |
+
background: white;
|
| 122 |
+
padding: 1.5rem;
|
| 123 |
+
border-radius: 20px;
|
| 124 |
+
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
|
| 125 |
+
text-align: center;
|
| 126 |
+
margin: 1rem 0;
|
| 127 |
+
border: 3px solid #667eea;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
/* Chart styling */
|
| 131 |
+
.chart-container {
|
| 132 |
+
background: white;
|
| 133 |
+
padding: 1.5rem;
|
| 134 |
+
border-radius: 20px;
|
| 135 |
+
margin-top: 1rem;
|
| 136 |
+
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
|
| 137 |
+
border-left: 5px solid #f093fb;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
.chart-title {
|
| 141 |
+
color: #333;
|
| 142 |
+
font-size: 1.3rem;
|
| 143 |
+
font-weight: 600;
|
| 144 |
+
margin-bottom: 1rem;
|
| 145 |
+
text-align: center;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
/* Footer */
|
| 149 |
+
.app-footer {
|
| 150 |
+
text-align: center;
|
| 151 |
+
color: #666;
|
| 152 |
+
font-size: 1rem;
|
| 153 |
+
margin-top: 3rem;
|
| 154 |
+
padding: 2rem;
|
| 155 |
+
background: rgba(255, 255, 255, 0.1);
|
| 156 |
+
backdrop-filter: blur(10px);
|
| 157 |
+
border-radius: 15px;
|
| 158 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
/* Divider */
|
| 162 |
+
.custom-divider {
|
| 163 |
+
height: 3px;
|
| 164 |
+
background: linear-gradient(90deg, #667eea, #764ba2, #f093fb);
|
| 165 |
+
border: none;
|
| 166 |
+
border-radius: 2px;
|
| 167 |
+
margin: 2rem 0;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
/* Animation for elements */
|
| 171 |
+
.fade-in {
|
| 172 |
+
animation: fadeIn 0.6s ease-in;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
@keyframes fadeIn {
|
| 176 |
+
from { opacity: 0; transform: translateY(20px); }
|
| 177 |
+
to { opacity: 1; transform: translateY(0); }
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
/* Button styling */
|
| 181 |
+
.stButton > button {
|
| 182 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 183 |
+
color: white;
|
| 184 |
+
border: none;
|
| 185 |
+
border-radius: 25px;
|
| 186 |
+
padding: 0.75rem 2rem;
|
| 187 |
+
font-weight: 600;
|
| 188 |
+
font-size: 1rem;
|
| 189 |
+
transition: all 0.3s ease;
|
| 190 |
+
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
.stButton > button:hover {
|
| 194 |
+
transform: translateY(-2px);
|
| 195 |
+
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
/* Hide Streamlit elements */
|
| 199 |
+
#MainMenu {visibility: hidden;}
|
| 200 |
+
footer {visibility: hidden;}
|
| 201 |
+
header {visibility: hidden;}
|
| 202 |
+
|
| 203 |
+
/* File uploader styling */
|
| 204 |
+
.uploadedFile {
|
| 205 |
+
border-radius: 15px;
|
| 206 |
+
overflow: hidden;
|
| 207 |
+
}
|
| 208 |
+
</style>
|
| 209 |
+
""", unsafe_allow_html=True)
|
| 210 |
|
| 211 |
+
# ---- HEADER ----
|
| 212 |
+
st.markdown('<div class="main-container fade-in">', unsafe_allow_html=True)
|
| 213 |
+
st.markdown("<h1 class='app-header'>ποΈ Handwritten Digit Recognizer</h1>", unsafe_allow_html=True)
|
| 214 |
+
st.markdown("<p class='app-subtitle'>Upload an image of a digit (0β9) and watch our AI model predict it with stunning accuracy!</p>", unsafe_allow_html=True)
|
| 215 |
+
st.markdown('<hr class="custom-divider">', unsafe_allow_html=True)
|
| 216 |
|
| 217 |
+
# ---- SIDEBAR ----
|
| 218 |
+
with st.sidebar:
|
| 219 |
+
st.markdown('<div class="sidebar-content">', unsafe_allow_html=True)
|
| 220 |
+
st.markdown("<h2 class='sidebar-header'>π How to Use</h2>", unsafe_allow_html=True)
|
| 221 |
+
st.markdown("""
|
| 222 |
+
<div class='sidebar-text'>
|
| 223 |
+
<strong>Step 1:</strong> Upload a clear image of a single digit (PNG/JPG)<br><br>
|
| 224 |
+
<strong>Step 2:</strong> Wait for our AI model to process your image<br><br>
|
| 225 |
+
<strong>Step 3:</strong> View the predicted digit and confidence scores<br><br>
|
| 226 |
+
<em>π‘ Tip: Use images with good contrast for best results!</em>
|
| 227 |
+
</div>
|
| 228 |
+
""", unsafe_allow_html=True)
|
| 229 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 230 |
+
|
| 231 |
+
st.markdown('<hr class="custom-divider">', unsafe_allow_html=True)
|
| 232 |
+
|
| 233 |
+
st.markdown('<div class="sidebar-content">', unsafe_allow_html=True)
|
| 234 |
+
st.markdown("<h3 class='sidebar-header'>π About This App</h3>", unsafe_allow_html=True)
|
| 235 |
+
st.markdown("""
|
| 236 |
+
<div class='sidebar-text'>
|
| 237 |
+
This application uses a deep learning neural network trained on the MNIST dataset to recognize handwritten digits with high accuracy.
|
| 238 |
+
<br><br>
|
| 239 |
+
<strong>Technologies:</strong><br>
|
| 240 |
+
β’ Streamlit for the interface<br>
|
| 241 |
+
β’ TensorFlow for AI predictions<br>
|
| 242 |
+
β’ Computer Vision processing<br>
|
| 243 |
+
<br>
|
| 244 |
+
Built with β€οΈ for digit recognition
|
| 245 |
+
</div>
|
| 246 |
+
""", unsafe_allow_html=True)
|
| 247 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 248 |
|
| 249 |
+
# ---- FILE UPLOAD ----
|
| 250 |
+
st.markdown('<div class="upload-section fade-in">', unsafe_allow_html=True)
|
| 251 |
+
st.markdown("<h2 class='upload-title'>π Upload Your Digit Image</h2>", unsafe_allow_html=True)
|
| 252 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"], label_visibility="collapsed")
|
| 253 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 254 |
|
| 255 |
if uploaded_file is not None:
|
| 256 |
+
col1, col2 = st.columns([1,2]) # image left, results right
|
| 257 |
+
|
| 258 |
+
with col1:
|
| 259 |
+
st.markdown('<div class="image-container fade-in">', unsafe_allow_html=True)
|
| 260 |
+
st.image(uploaded_file, caption="πΈ Your Uploaded Image", width=150)
|
| 261 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 262 |
+
|
| 263 |
+
with col2:
|
| 264 |
+
# Preprocess (keeping original logic intact)
|
| 265 |
+
img = Image.open(uploaded_file).convert('L')
|
| 266 |
+
img = img.resize((28,28))
|
| 267 |
+
img_array = np.array(img) / 255.0
|
| 268 |
+
img_array = img_array.reshape(1,28,28,1)
|
| 269 |
+
|
| 270 |
+
# Predict (keeping original logic intact)
|
| 271 |
+
pred = model.predict(img_array)
|
| 272 |
+
pred_label = np.argmax(pred)
|
| 273 |
+
confidence = np.max(pred) * 100
|
| 274 |
+
|
| 275 |
+
# ---- SHOW RESULT ----
|
| 276 |
+
st.markdown('<div class="results-container fade-in">', unsafe_allow_html=True)
|
| 277 |
+
st.markdown(f"<div class='prediction-result'>π― Predicted Digit: {pred_label}</div>", unsafe_allow_html=True)
|
| 278 |
+
st.markdown(f"<p style='text-align: center; color: white; font-size: 1.2rem; margin-top: 1rem;'>Confidence: {confidence:.1f}%</p>", unsafe_allow_html=True)
|
| 279 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 280 |
+
|
| 281 |
+
# Chart with custom styling
|
| 282 |
+
st.markdown('<div class="chart-container fade-in">', unsafe_allow_html=True)
|
| 283 |
+
st.markdown("<h3 class='chart-title'>π Prediction Probabilities</h3>", unsafe_allow_html=True)
|
| 284 |
+
st.bar_chart(pred[0]) # visualize probabilities (keeping original logic)
|
| 285 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 286 |
|
| 287 |
+
# ---- FOOTER ----
|
| 288 |
+
st.markdown('<hr class="custom-divider">', unsafe_allow_html=True)
|
| 289 |
+
st.markdown("""
|
| 290 |
+
<div class='app-footer fade-in'>
|
| 291 |
+
<h3 style='color: #667eea; margin-bottom: 1rem;'>β¨ Thank you for using our Digit Recognition App!</h3>
|
| 292 |
+
<p>Powered by cutting-edge AI technology β’ Built with β€οΈ using Streamlit & TensorFlow</p>
|
| 293 |
+
<p style='font-size: 0.9rem; opacity: 0.8; margin-top: 1rem;'>
|
| 294 |
+
π¬ Continuously learning and improving β’ π Accuracy rates above 95%
|
| 295 |
+
</p>
|
| 296 |
+
</div>
|
| 297 |
+
""", unsafe_allow_html=True)
|
| 298 |
|
| 299 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|