Liori25 commited on
Commit
6ff0916
·
verified ·
1 Parent(s): a67040e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -56
app.py CHANGED
@@ -39,12 +39,10 @@ except Exception as e:
39
  # ==========================================
40
  def image_to_base64(image_path):
41
  if not os.path.exists(image_path):
42
- # Returns a 1x1 transparent pixel if image is missing to avoid errors
43
  return "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
44
  with open(image_path, "rb") as img_file:
45
  return base64.b64encode(img_file.read()).decode('utf-8')
46
 
47
- # Ensure these files are in your Spaces "Files" tab
48
  logo_b64 = image_to_base64("logo.jpg")
49
  profile_b64 = image_to_base64("232px-Tv_the_muppet_show_bein_green.jpg")
50
 
@@ -120,10 +118,11 @@ body, .gradio-container { background-color: #f0f2f5; }
120
  }
121
 
122
  .logo-area { display: flex; align-items: center; gap: 15px; }
123
- .logo-img { height: 40px; width: 40px; border-radius: 50%; object-fit: cover; }
 
124
  .app-name {
125
  font-weight: 800;
126
- font-size: 24px;
127
  background: -webkit-linear-gradient(45deg, #1877f2, #6b21a8);
128
  -webkit-background-clip: text;
129
  -webkit-text-fill-color: transparent;
@@ -144,7 +143,13 @@ body, .gradio-container { background-color: #f0f2f5; }
144
  transition: all 0.2s ease;
145
  }
146
  .nav-btn:hover { background-color: #e4e6eb !important; color: #050505 !important; }
147
- .nav-btn.selected { background-color: #e7f3ff !important; color: #1877f2 !important; }
 
 
 
 
 
 
148
 
149
  /* Feed Cards */
150
  .content-card {
@@ -170,6 +175,17 @@ body, .gradio-container { background-color: #f0f2f5; }
170
  background: linear-gradient(135deg, #ffffff 0%, #f0f7ff 100%);
171
  border: 1px solid #1877f2;
172
  }
 
 
 
 
 
 
 
 
 
 
 
173
  """
174
 
175
  # ==========================================
@@ -184,7 +200,7 @@ with gr.Blocks(title="Legacy Kitchen") as demo:
184
  <img src="data:image/jpeg;base64,{logo_b64}" class="logo-img">
185
  <span class="app-name">Legacy Kitchen</span>
186
  </div>
187
- <div style="color: #65676b; font-weight: 600;">v2.0 Beta</div>
188
  </div>
189
  """)
190
 
@@ -192,22 +208,42 @@ with gr.Blocks(title="Legacy Kitchen") as demo:
192
 
193
  # --- LEFT SIDEBAR (Navigation) ---
194
  with gr.Column(scale=1, min_width=200):
 
195
  gr.HTML(f"""
196
- <div style="display:flex; align-items:center; padding: 10px; margin-bottom: 10px;">
197
- <img src="data:image/jpeg;base64,{profile_b64}" style="width:36px; height:36px; border-radius:50%; margin-right:10px; object-fit:cover;">
198
- <b style="font-size: 15px;">My Profile</b>
199
  </div>
200
  """)
201
 
202
- nav_feed = gr.Button("📰 News Feed", elem_classes=["nav-btn", "selected"])
203
- nav_digital = gr.Button("✨ AI Digitizer", elem_classes=["nav-btn"])
 
 
 
 
204
  nav_about = gr.Button("ℹ️ About", elem_classes=["nav-btn"])
205
 
206
  # --- CENTER CONTENT ---
207
  with gr.Column(scale=3):
208
 
209
- # === VIEW 1: FEED ===
210
- with gr.Group(visible=True) as feed_view:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  # "What's on your mind" Box
212
  with gr.Group(elem_classes=["content-card"]):
213
  with gr.Row():
@@ -222,7 +258,6 @@ with gr.Blocks(title="Legacy Kitchen") as demo:
222
  emoji = random.choice(["🥘", "🥗", "🍰", "🌮"])
223
 
224
  with gr.Group(elem_classes=["content-card"]):
225
- # Header of the Post
226
  gr.HTML(f"""
227
  <div class="feed-user-row">
228
  <div class="feed-avatar">{emoji}</div>
@@ -232,13 +267,10 @@ with gr.Blocks(title="Legacy Kitchen") as demo:
232
  </div>
233
  </div>
234
  """)
235
-
236
- # Content
237
  gr.Markdown(f"### {row['Title']}")
238
  gr.Markdown(f"{str(row['Raw_Output'])[:250]}...")
239
  gr.Markdown(f"*[Click to see full recipe...]*")
240
 
241
- # Action Buttons
242
  with gr.Row():
243
  gr.Button("👍 Like", size="sm", variant="secondary")
244
  gr.Button("💬 Comment", size="sm", variant="secondary")
@@ -246,62 +278,69 @@ with gr.Blocks(title="Legacy Kitchen") as demo:
246
  else:
247
  gr.Markdown("⚠️ Database is empty. Please check your CSV file.")
248
 
249
- # === VIEW 2: AI DIGITALIZER ===
250
- with gr.Group(visible=False) as digitalizer_view:
251
- gr.Markdown("## 📸 Recipe Digitalizer", elem_classes=["content-card"])
252
-
253
- with gr.Row():
254
- with gr.Column(elem_classes=["content-card", "digitalizer-container"]):
255
- input_img = gr.Image(type="filepath", label="Upload Handwritten Note", height=300)
256
- magic_btn = gr.Button("✨ Convert to Text", variant="primary", size="lg")
257
-
258
- with gr.Column(elem_classes=["content-card"]):
259
- out_text = gr.Textbox(label="Digitized Text", lines=6, interactive=False)
260
- out_sim = gr.Textbox(label="Similar Recipes in Database", lines=6)
261
-
262
- magic_btn.click(magic_pipeline, input_img, [out_text, out_sim])
263
-
264
  # === VIEW 3: ABOUT ===
265
  with gr.Group(visible=False) as about_view:
266
  with gr.Group(elem_classes=["content-card"]):
267
  gr.Markdown("""
268
  # ℹ️ About Legacy Kitchen
269
-
270
  **Preserving Heritage through AI.**
271
-
272
  Legacy Kitchen allows you to upload photos of your grandmother's handwritten recipe cards and instantly converts them into digital text using OCR and AI models.
273
-
274
- ### Tech Stack
275
- * **Hugging Face Spaces** for hosting
276
- * **Gradio** for the UI
277
- * **Transformers** for embeddings
278
  """)
279
 
280
  # --- RIGHT COLUMN (Trending) ---
281
  with gr.Column(scale=1, min_width=200):
282
  gr.Markdown("### Trending Recipes")
283
- gr.HTML("""
284
- <div style="background:white; padding:10px; border-radius:8px; margin-bottom:10px; box-shadow:0 1px 2px rgba(0,0,0,0.1);">
285
- <b>🍜 Ramen Hack</b><br>
286
- <span style="color:gray; font-size:12px;">12k likes</span>
287
- </div>
288
- <div style="background:white; padding:10px; border-radius:8px; box-shadow:0 1px 2px rgba(0,0,0,0.1);">
289
- <b>🍪 Best Cookies</b><br>
290
- <span style="color:gray; font-size:12px;">8k likes</span>
291
- </div>
292
- """)
 
 
 
 
293
 
294
  # ==========================================
295
  # 6. JAVASCRIPT LOGIC (Client Side)
296
  # ==========================================
297
- def go_feed(): return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
298
- def go_digi(): return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
299
- def go_about(): return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
301
- nav_feed.click(go_feed, None, [feed_view, digitalizer_view, about_view])
302
- nav_digital.click(go_digi, None, [feed_view, digitalizer_view, about_view])
303
- nav_about.click(go_about, None, [feed_view, digitalizer_view, about_view])
 
 
 
 
 
 
 
 
 
 
304
 
305
  if __name__ == "__main__":
306
- # Theme and CSS injected here to avoid Warnings
307
  demo.launch(theme=theme, css=modern_css)
 
39
  # ==========================================
40
  def image_to_base64(image_path):
41
  if not os.path.exists(image_path):
 
42
  return "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
43
  with open(image_path, "rb") as img_file:
44
  return base64.b64encode(img_file.read()).decode('utf-8')
45
 
 
46
  logo_b64 = image_to_base64("logo.jpg")
47
  profile_b64 = image_to_base64("232px-Tv_the_muppet_show_bein_green.jpg")
48
 
 
118
  }
119
 
120
  .logo-area { display: flex; align-items: center; gap: 15px; }
121
+ .logo-img { height: 120px; width: 120px; border-radius: 12px; object-fit: cover; border: 1px solid #ddd; }
122
+
123
  .app-name {
124
  font-weight: 800;
125
+ font-size: 28px;
126
  background: -webkit-linear-gradient(45deg, #1877f2, #6b21a8);
127
  -webkit-background-clip: text;
128
  -webkit-text-fill-color: transparent;
 
143
  transition: all 0.2s ease;
144
  }
145
  .nav-btn:hover { background-color: #e4e6eb !important; color: #050505 !important; }
146
+
147
+ /* Selected State */
148
+ .nav-btn.selected {
149
+ background-color: #e7f3ff !important;
150
+ color: #1877f2 !important;
151
+ border-left: 4px solid #1877f2 !important;
152
+ }
153
 
154
  /* Feed Cards */
155
  .content-card {
 
175
  background: linear-gradient(135deg, #ffffff 0%, #f0f7ff 100%);
176
  border: 1px solid #1877f2;
177
  }
178
+
179
+ /* Right Side Trending Box */
180
+ .trend-box {
181
+ background:white;
182
+ padding:10px;
183
+ border-radius:8px;
184
+ margin-bottom:10px;
185
+ box-shadow:0 1px 2px rgba(0,0,0,0.1);
186
+ transition: background 0.2s;
187
+ }
188
+ .trend-box:hover { background: #f0f2f5; cursor: pointer; }
189
  """
190
 
191
  # ==========================================
 
200
  <img src="data:image/jpeg;base64,{logo_b64}" class="logo-img">
201
  <span class="app-name">Legacy Kitchen</span>
202
  </div>
203
+ <div style="color: #65676b; font-weight: 600;">v2.2 Beta</div>
204
  </div>
205
  """)
206
 
 
208
 
209
  # --- LEFT SIDEBAR (Navigation) ---
210
  with gr.Column(scale=1, min_width=200):
211
+ # My Profile Section
212
  gr.HTML(f"""
213
+ <div style="display:flex; align-items:center; padding: 10px 10px 5px 10px;">
214
+ <img src="data:image/jpeg;base64,{profile_b64}" style="width:40px; height:40px; border-radius:50%; margin-right:10px; object-fit:cover;">
215
+ <b style="font-size: 16px;">My Profile</b>
216
  </div>
217
  """)
218
 
219
+ # Separator
220
+ gr.HTML("<hr style='border: 0; border-top: 1px solid #e4e6eb; margin: 10px 0 20px 0;'>")
221
+
222
+ # FIX: Digitalizer is now FIRST and SELECTED by default
223
+ nav_digital = gr.Button("✨ AI Digitizer", elem_classes=["nav-btn", "selected"])
224
+ nav_feed = gr.Button("📰 News Feed", elem_classes=["nav-btn"])
225
  nav_about = gr.Button("ℹ️ About", elem_classes=["nav-btn"])
226
 
227
  # --- CENTER CONTENT ---
228
  with gr.Column(scale=3):
229
 
230
+ # === VIEW 1: AI DIGITALIZER (Now DEFAULT: visible=True) ===
231
+ with gr.Group(visible=True) as digitalizer_view:
232
+ gr.Markdown("## 📸 Recipe Digitalizer", elem_classes=["content-card"])
233
+
234
+ with gr.Row():
235
+ with gr.Column(elem_classes=["content-card", "digitalizer-container"]):
236
+ input_img = gr.Image(type="filepath", label="Upload Handwritten Note", height=300)
237
+ magic_btn = gr.Button("✨ Convert to Text", variant="primary", size="lg")
238
+
239
+ with gr.Column(elem_classes=["content-card"]):
240
+ out_text = gr.Textbox(label="Digitized Text", lines=6, interactive=False)
241
+ out_sim = gr.Textbox(label="Similar Recipes in Database", lines=6)
242
+
243
+ magic_btn.click(magic_pipeline, input_img, [out_text, out_sim])
244
+
245
+ # === VIEW 2: FEED (Now HIDDEN by default: visible=False) ===
246
+ with gr.Group(visible=False) as feed_view:
247
  # "What's on your mind" Box
248
  with gr.Group(elem_classes=["content-card"]):
249
  with gr.Row():
 
258
  emoji = random.choice(["🥘", "🥗", "🍰", "🌮"])
259
 
260
  with gr.Group(elem_classes=["content-card"]):
 
261
  gr.HTML(f"""
262
  <div class="feed-user-row">
263
  <div class="feed-avatar">{emoji}</div>
 
267
  </div>
268
  </div>
269
  """)
 
 
270
  gr.Markdown(f"### {row['Title']}")
271
  gr.Markdown(f"{str(row['Raw_Output'])[:250]}...")
272
  gr.Markdown(f"*[Click to see full recipe...]*")
273
 
 
274
  with gr.Row():
275
  gr.Button("👍 Like", size="sm", variant="secondary")
276
  gr.Button("💬 Comment", size="sm", variant="secondary")
 
278
  else:
279
  gr.Markdown("⚠️ Database is empty. Please check your CSV file.")
280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  # === VIEW 3: ABOUT ===
282
  with gr.Group(visible=False) as about_view:
283
  with gr.Group(elem_classes=["content-card"]):
284
  gr.Markdown("""
285
  # ℹ️ About Legacy Kitchen
 
286
  **Preserving Heritage through AI.**
 
287
  Legacy Kitchen allows you to upload photos of your grandmother's handwritten recipe cards and instantly converts them into digital text using OCR and AI models.
 
 
 
 
 
288
  """)
289
 
290
  # --- RIGHT COLUMN (Trending) ---
291
  with gr.Column(scale=1, min_width=200):
292
  gr.Markdown("### Trending Recipes")
293
+
294
+ # Helper to create trending box
295
+ def trend_box(title, likes):
296
+ return f"""
297
+ <div class="trend-box">
298
+ <b>{title}</b><br>
299
+ <span style="color:gray; font-size:12px;">{likes} likes</span>
300
+ </div>"""
301
+
302
+ # FIX: Added the 2 requested items
303
+ gr.HTML(trend_box("🍜 Ramen Hack", "12k") +
304
+ trend_box("🍪 Best Cookies", "8k") +
305
+ trend_box("🍰 Cheese Cake", "15k") +
306
+ trend_box("🍪 Nana's Tahini Cookies", "9k"))
307
 
308
  # ==========================================
309
  # 6. JAVASCRIPT LOGIC (Client Side)
310
  # ==========================================
311
+
312
+ # FIX: Updated logic to match the new button order
313
+ # ORDER OF OUTPUTS: [DigitalizerView, FeedView, AboutView, NavDigital, NavFeed, NavAbout]
314
+
315
+ def go_digi():
316
+ return (
317
+ gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), # Views: Digi, Feed, About
318
+ gr.update(elem_classes=["nav-btn", "selected"]), # Nav Digi
319
+ gr.update(elem_classes=["nav-btn"]), # Nav Feed
320
+ gr.update(elem_classes=["nav-btn"]) # Nav About
321
+ )
322
+
323
+ def go_feed():
324
+ return (
325
+ gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
326
+ gr.update(elem_classes=["nav-btn"]),
327
+ gr.update(elem_classes=["nav-btn", "selected"]),
328
+ gr.update(elem_classes=["nav-btn"])
329
+ )
330
 
331
+ def go_about():
332
+ return (
333
+ gr.update(visible=False), gr.update(visible=False), gr.update(visible=True),
334
+ gr.update(elem_classes=["nav-btn"]),
335
+ gr.update(elem_classes=["nav-btn"]),
336
+ gr.update(elem_classes=["nav-btn", "selected"])
337
+ )
338
+
339
+ outputs_ui = [digitalizer_view, feed_view, about_view, nav_digital, nav_feed, nav_about]
340
+
341
+ nav_digital.click(go_digi, None, outputs_ui)
342
+ nav_feed.click(go_feed, None, outputs_ui)
343
+ nav_about.click(go_about, None, outputs_ui)
344
 
345
  if __name__ == "__main__":
 
346
  demo.launch(theme=theme, css=modern_css)