anamjafar6 commited on
Commit
3147f21
Β·
verified Β·
1 Parent(s): 984b079

Update app.py

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