Matan Kriel commited on
Commit
e0338da
Β·
1 Parent(s): 26e1d1d

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -56
app.py CHANGED
@@ -35,7 +35,7 @@ except Exception as e:
35
  print(f"❌ Error loading parquet file: {e}")
36
  db_features = None
37
 
38
- # --- 4. CORE SEARCH LOGIC (FIXED) ---
39
  def find_best_matches(query_features, top_k=3):
40
  if db_features is None:
41
  return []
@@ -51,17 +51,19 @@ def find_best_matches(query_features, top_k=3):
51
  for idx, score in zip(indices[0], scores[0]):
52
  idx = idx.item()
53
 
54
- # CRITICAL FIX: Save image to disk instead of returning raw PIL object
55
- # This prevents the "Too much data" error
56
  img_data = dataset[idx]['image']
57
 
58
- # Create a unique filename for the cache
59
- save_path = f"temp_result_{idx}.jpg"
 
 
 
60
  img_data.save(save_path)
61
 
62
  label = df.iloc[idx]['label_name']
63
 
64
- # Return the PATH (string) instead of the IMAGE (object)
65
  results.append((save_path, f"{label} ({score:.2f})"))
66
 
67
  return results
@@ -81,87 +83,46 @@ def search_by_text(input_text):
81
  features = model.get_text_features(**inputs)
82
  return find_best_matches(features)
83
 
84
- # --- 6. BUILD UI (Centered & Fixed) ---
85
-
86
- # CSS to center the app and limit width to 1000px
87
  custom_css = """
88
- .gradio-container {
89
- width: 100%;
90
- max-width: 1000px;
91
- margin: 0 auto !important;
92
- }
93
- h1 {
94
- text-align: center;
95
- color: #E67E22;
96
- }
97
  """
98
 
99
  with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="Food Matcher AI") as demo:
100
 
101
- # --- Header ---
102
  with gr.Row():
103
  gr.Markdown("# πŸ” Visual Dish Matcher (SigLIP)")
104
 
105
- gr.Markdown(
106
- "Upload a food photo or describe a craving. We'll find the closest matches from our database.",
107
- elem_classes=["center-text"]
108
- )
109
 
110
- # --- Video Accordion ---
111
  with gr.Accordion("πŸ“Ί Watch Demo Video", open=False):
112
  gr.HTML('<div style="display:flex; justify-content:center;"><iframe width="560" height="315" src="https://www.youtube.com/embed/IXeIxYHi0Es" frameborder="0" allowfullscreen></iframe></div>')
113
 
114
- # --- Main Interface ---
115
  with gr.Tab("πŸ–ΌοΈ Search by Image"):
116
- # Split layout: 1/3 Left (Input), 2/3 Right (Results)
117
  with gr.Row():
118
-
119
- # --- Left Column: Input ---
120
  with gr.Column(scale=1):
121
- img_input = gr.Image(
122
- type="pil",
123
- label="Your Photo",
124
- height=300 # Fixed height
125
- )
126
  btn_img = gr.Button("πŸ” Find Matches", variant="primary", size="lg")
127
 
128
- # --- Right Column: Output ---
129
  with gr.Column(scale=2):
130
- img_gallery = gr.Gallery(
131
- label="Similar Dishes",
132
- columns=3,
133
- height=350,
134
- object_fit="contain"
135
- )
136
 
137
  btn_img.click(search_by_image, inputs=img_input, outputs=img_gallery)
138
 
139
  with gr.Tab("πŸ“ Search by Text"):
140
  with gr.Row():
141
-
142
- # --- Left Column: Input ---
143
  with gr.Column(scale=1):
144
- txt_input = gr.Textbox(
145
- label="Describe it",
146
- placeholder="e.g. 'Spicy Tacos' or 'Chocolate Cake'",
147
- lines=4
148
- )
149
  btn_txt = gr.Button("πŸ” Search", variant="primary", size="lg")
150
 
151
- # --- Right Column: Output ---
152
  with gr.Column(scale=2):
153
- txt_gallery = gr.Gallery(
154
- label="Similar Dishes",
155
- columns=3,
156
- height=350,
157
- object_fit="contain"
158
- )
159
 
160
  btn_txt.click(search_by_text, inputs=txt_input, outputs=txt_gallery)
161
 
162
- # --- Footer ---
163
  gr.Markdown("---")
164
- gr.Markdown("By Matan Kriel & Odeya Shmuel | Powered by Google SigLIP", elem_id="footer")
165
 
166
  # Launch
167
  demo.launch()
 
35
  print(f"❌ Error loading parquet file: {e}")
36
  db_features = None
37
 
38
+ # --- 4. CORE SEARCH LOGIC (SAFE MODE) ---
39
  def find_best_matches(query_features, top_k=3):
40
  if db_features is None:
41
  return []
 
51
  for idx, score in zip(indices[0], scores[0]):
52
  idx = idx.item()
53
 
54
+ # 1. Get the raw image
 
55
  img_data = dataset[idx]['image']
56
 
57
+ # 2. Resize it to be small & fast (300x300 max)
58
+ img_data.thumbnail((300, 300))
59
+
60
+ # 3. Save to a temporary path (prevents the "Too much data" crash)
61
+ save_path = f"/tmp/temp_result_{idx}.jpg"
62
  img_data.save(save_path)
63
 
64
  label = df.iloc[idx]['label_name']
65
 
66
+ # 4. Return the PATH (string), NOT the image object
67
  results.append((save_path, f"{label} ({score:.2f})"))
68
 
69
  return results
 
83
  features = model.get_text_features(**inputs)
84
  return find_best_matches(features)
85
 
86
+ # --- 6. BUILD UI (Clean & Centered) ---
 
 
87
  custom_css = """
88
+ .gradio-container { width: 100%; max-width: 1000px; margin: 0 auto !important; }
89
+ h1 { text-align: center; color: #E67E22; }
 
 
 
 
 
 
 
90
  """
91
 
92
  with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="Food Matcher AI") as demo:
93
 
 
94
  with gr.Row():
95
  gr.Markdown("# πŸ” Visual Dish Matcher (SigLIP)")
96
 
97
+ gr.Markdown("Upload a food photo or describe a craving. We'll find the closest matches.", elem_classes=["center-text"])
 
 
 
98
 
 
99
  with gr.Accordion("πŸ“Ί Watch Demo Video", open=False):
100
  gr.HTML('<div style="display:flex; justify-content:center;"><iframe width="560" height="315" src="https://www.youtube.com/embed/IXeIxYHi0Es" frameborder="0" allowfullscreen></iframe></div>')
101
 
 
102
  with gr.Tab("πŸ–ΌοΈ Search by Image"):
 
103
  with gr.Row():
 
 
104
  with gr.Column(scale=1):
105
+ img_input = gr.Image(type="pil", label="Your Photo", height=300)
 
 
 
 
106
  btn_img = gr.Button("πŸ” Find Matches", variant="primary", size="lg")
107
 
 
108
  with gr.Column(scale=2):
109
+ img_gallery = gr.Gallery(label="Similar Dishes", columns=3, height=350, object_fit="contain")
 
 
 
 
 
110
 
111
  btn_img.click(search_by_image, inputs=img_input, outputs=img_gallery)
112
 
113
  with gr.Tab("πŸ“ Search by Text"):
114
  with gr.Row():
 
 
115
  with gr.Column(scale=1):
116
+ txt_input = gr.Textbox(label="Describe it", placeholder="e.g. 'Spicy Tacos'", lines=4)
 
 
 
 
117
  btn_txt = gr.Button("πŸ” Search", variant="primary", size="lg")
118
 
 
119
  with gr.Column(scale=2):
120
+ txt_gallery = gr.Gallery(label="Similar Dishes", columns=3, height=350, object_fit="contain")
 
 
 
 
 
121
 
122
  btn_txt.click(search_by_text, inputs=txt_input, outputs=txt_gallery)
123
 
 
124
  gr.Markdown("---")
125
+ gr.Markdown("By Matan Kriel & Odeya Shmuel | Powered by Google SigLIP")
126
 
127
  # Launch
128
  demo.launch()