tdurzynski commited on
Commit
4c6fe8f
·
verified ·
1 Parent(s): 522835b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -37
app.py CHANGED
@@ -98,16 +98,17 @@ st.checkbox("I agree to the terms and conditions", key="terms_accepted")
98
 
99
  # Upload Image
100
  uploaded_file = st.file_uploader("Upload an image of your food", type=["jpg", "png", "jpeg"])
101
- # Ensure an image is uploaded before displaying
 
102
  if uploaded_file:
103
  original_img = Image.open(uploaded_file)
104
  st.session_state["original_image"] = original_img
105
 
106
- # If an image has been uploaded, process and display it
107
  if "original_image" in st.session_state:
108
  original_img = st.session_state["original_image"]
109
 
110
- # Resize while maintaining aspect ratio (longest side = 512)
111
  def resize_image(image, max_size=512):
112
  aspect_ratio = image.width / image.height
113
  if image.width > image.height:
@@ -120,8 +121,8 @@ if "original_image" in st.session_state:
120
 
121
  processed_img = resize_image(original_img)
122
 
123
- # UI Layout: Two Columns for Images
124
- col1, col2 = st.columns(2)
125
 
126
  with col1:
127
  st.subheader("📷 Original Image")
@@ -133,44 +134,66 @@ if "original_image" in st.session_state:
133
 
134
  # Store processed image in session state for annotations
135
  st.session_state["processed_image"] = processed_img
136
- else:
137
- st.warning("⚠️ Please upload an image first.")
138
-
139
 
140
- # Create a table-like structure for food annotation
141
  st.subheader("📝 Add Annotations")
142
 
143
- # Define columns for table structure
144
- col_food, col_qty, col_unit, col_ing = st.columns([2, 1, 1, 2]) # Define column widths
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
- # Food Item Dropdown (with Intellisense)
147
- food_item = col_food.selectbox("Food Item", options=suggested_foods, index=None, placeholder="Select or enter food")
148
 
149
- # Quantity Input
150
- quantity = col_qty.number_input("Qty", min_value=0, step=1)
 
 
 
 
151
 
152
- # Unit Dropdown (Grams, Ounces, etc.)
153
- unit = col_unit.selectbox("Unit", ["g", "oz", "tsp", "tbsp", "cup", "slice", "piece"])
 
154
 
155
- # Ingredients Section with Expandable Input
156
- with col_ing:
157
- ingredients = []
158
- if st.button("+ Add Ingredients"):
159
- ingredient = st.text_input("Enter ingredient")
160
- if ingredient:
161
- ingredients.append(ingredient)
162
 
163
- # Display added annotations in a structured format
164
  if st.button("Save Annotations"):
165
- new_entry = {
166
- "name": food_item,
167
- "quantity": quantity,
168
- "unit": unit,
169
- "ingredients": ingredients,
170
- }
171
- st.session_state["annotations"].append(new_entry)
172
- st.success(" Annotation saved!")
173
-
174
- # Show existing annotations
175
- st.write("### Current Annotations")
176
- st.table(st.session_state["annotations"])
 
 
98
 
99
  # Upload Image
100
  uploaded_file = st.file_uploader("Upload an image of your food", type=["jpg", "png", "jpeg"])
101
+
102
+ # Ensure uploaded file is valid before proceeding
103
  if uploaded_file:
104
  original_img = Image.open(uploaded_file)
105
  st.session_state["original_image"] = original_img
106
 
107
+ # Ensure an image is available in session state before processing
108
  if "original_image" in st.session_state:
109
  original_img = st.session_state["original_image"]
110
 
111
+ # Resize image while maintaining aspect ratio (longest side = 512)
112
  def resize_image(image, max_size=512):
113
  aspect_ratio = image.width / image.height
114
  if image.width > image.height:
 
121
 
122
  processed_img = resize_image(original_img)
123
 
124
+ # Layout Update: Original and Processed images spanning two columns each
125
+ col1, col2 = st.columns([2, 2])
126
 
127
  with col1:
128
  st.subheader("📷 Original Image")
 
134
 
135
  # Store processed image in session state for annotations
136
  st.session_state["processed_image"] = processed_img
 
 
 
137
 
138
+ # Food Annotation Table
139
  st.subheader("📝 Add Annotations")
140
 
141
+ # Initialize session state for annotations if not already present
142
+ if "annotations" not in st.session_state:
143
+ st.session_state["annotations"] = []
144
+
145
+ # Table-like structured input for food annotations
146
+ food_data = pd.DataFrame(columns=["Food Item", "Quantity", "Unit", "Ingredients"])
147
+
148
+ # Function to add a new food item
149
+ def add_food_item():
150
+ st.session_state["annotations"].append({
151
+ "name": "",
152
+ "quantity": "",
153
+ "unit": "",
154
+ "ingredients": []
155
+ })
156
+
157
+ # Render the existing food items in a table layout
158
+ for i, food in enumerate(st.session_state["annotations"]):
159
+ cols = st.columns([2, 1, 1, 2]) # Adjust widths
160
+
161
+ # Food Item (Dropdown with Intellisense + Custom Input)
162
+ food["name"] = cols[0].selectbox(f"Food Item {i+1}", FOOD_SUGGESTIONS + ["Other..."], index=None, key=f"food_{i}")
163
+
164
+ # Quantity Input
165
+ food["quantity"] = cols[1].number_input(f"Qty {i+1}", min_value=0, step=1, key=f"qty_{i}")
166
 
167
+ # Unit Dropdown
168
+ food["unit"] = cols[2].selectbox(f"Unit {i+1}", UNIT_OPTIONS, key=f"unit_{i}")
169
 
170
+ # Ingredients Section
171
+ with cols[3]:
172
+ ing_input = st.text_input(f"Ingredient {i+1}", key=f"ing_{i}")
173
+ if st.button(f"+ Add Ingredient {i+1}", key=f"btn_ing_{i}"):
174
+ if ing_input:
175
+ food["ingredients"].append(ing_input)
176
 
177
+ # Display Added Ingredients
178
+ if food["ingredients"]:
179
+ st.write(f"Ingredients: {', '.join(food['ingredients'])}")
180
 
181
+ # Button to add new food row
182
+ if st.button("+ Add Another Food Item"):
183
+ add_food_item()
 
 
 
 
184
 
185
+ # Save annotations when user is done
186
  if st.button("Save Annotations"):
187
+ metadata_table.update_item(
188
+ Key={"image_id": uploaded_file.name},
189
+ UpdateExpression="SET user_id = :u, annotations = :a, processing_status = :p, s3_url = :s, tokens_earned = :t",
190
+ ExpressionAttributeValues={
191
+ ":u": st.session_state["user_id"],
192
+ ":a": st.session_state["annotations"],
193
+ ":p": "processed",
194
+ ":s": f"s3://{S3_BUCKET_NAME}/raw-uploads/{uploaded_file.name}",
195
+ ":t": len(st.session_state["annotations"]) # Tokens = total annotations
196
+ },
197
+ )
198
+ st.success(f"✅ Annotations saved! You earned {len(st.session_state['annotations'])} tokens.")
199
+