tdurzynski commited on
Commit
9d30b87
Β·
verified Β·
1 Parent(s): 094dc67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -40
app.py CHANGED
@@ -7,6 +7,7 @@ import boto3
7
  from PIL import Image
8
  import firebase_admin
9
  from firebase_admin import credentials, auth
 
10
 
11
  # Load AWS credentials using correct HF Secrets
12
  AWS_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY")
@@ -38,6 +39,16 @@ dynamodb = boto3.resource(
38
  )
39
  metadata_table = dynamodb.Table(DYNAMODB_TABLE)
40
 
 
 
 
 
 
 
 
 
 
 
41
  # Streamlit Layout - Authentication Section
42
  st.sidebar.title("πŸ”‘ User Authentication")
43
  auth_option = st.sidebar.radio("Select an option", ["Login", "Sign Up", "Logout"])
@@ -74,7 +85,7 @@ if "user_id" not in st.session_state:
74
 
75
  # Streamlit Layout - Three Panel Design
76
  st.title("🍽️ Food Image Review & Annotation")
77
- col1, col2, col3 = st.columns([1, 1, 1])
78
 
79
  # Compliance & Disclaimer Section
80
  st.markdown("### **Terms & Conditions**")
@@ -101,49 +112,57 @@ def resize_image(image, new_width=512):
101
  if "original_image" in st.session_state:
102
  original_img = st.session_state["original_image"]
103
  processed_img = resize_image(original_img)
104
-
105
  # πŸ–ΌοΈ Panel 1: Original Image
106
  with col1:
107
  st.subheader("πŸ“· Original Image")
108
- st.image(original_img, caption=f"Uploaded ({original_img.width}x{original_img.height} pixels)", use_container_width=True)
109
 
110
  # πŸ–ΌοΈ Panel 2: Resized Image (512x512 Maintaining Aspect Ratio)
111
  with col2:
112
  st.subheader("πŸ–ΌοΈ Processed Image")
113
- st.image(processed_img, caption=f"Processed ({processed_img.width}x{processed_img.height} pixels)", use_container_width=True)
114
-
115
- # ✏️ Panel 3: Food Annotations with Structured Input
116
- with col3:
117
- st.subheader("πŸ“ Add Annotations")
118
- food_items = []
119
-
120
- # User enters food items one at a time
121
- new_food = st.text_input("Enter food item")
122
- if st.button("+ Add Food Item"):
123
- if new_food:
124
- food_items.append({"name": new_food, "ingredients": [], "weight": None})
125
-
126
- # Display added food items
127
- for idx, food in enumerate(food_items):
128
- st.write(f"{idx+1}. {food['name']}")
129
- sub_ingredient = st.text_input(f"Add ingredient for {food['name']}", key=f"sub_{idx}")
130
- if st.button(f"+ Add Ingredient to {food['name']}", key=f"btn_{idx}"):
131
- food["ingredients"].append(sub_ingredient)
132
-
133
- weight = st.text_input(f"Enter weight for {food['name']} (grams)", key=f"wt_{idx}")
134
- if weight:
135
- food["weight"] = weight
136
-
137
- if st.button("Save Annotations"):
138
- metadata_table.update_item(
139
- Key={"image_id": uploaded_file.name},
140
- UpdateExpression="SET user_id = :u, annotations = :a, processing_status = :p, s3_url = :s, tokens_earned = :t",
141
- ExpressionAttributeValues={
142
- ":u": st.session_state["user_id"],
143
- ":a": food_items,
144
- ":p": "processed",
145
- ":s": f"s3://{S3_BUCKET_NAME}/raw-uploads/{uploaded_file.name}",
146
- ":t": len(food_items)
147
- },
148
- )
149
- st.success("βœ… Annotations saved successfully!")
 
 
 
 
 
 
 
 
 
7
  from PIL import Image
8
  import firebase_admin
9
  from firebase_admin import credentials, auth
10
+ import pandas as pd
11
 
12
  # Load AWS credentials using correct HF Secrets
13
  AWS_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY")
 
39
  )
40
  metadata_table = dynamodb.Table(DYNAMODB_TABLE)
41
 
42
+ # Food Intellisense List
43
+ FOOD_SUGGESTIONS = [
44
+ "Apple", "Banana", "Pizza", "Burger", "Pasta", "Sushi", "Tacos", "Salad",
45
+ "Chicken Curry", "Steak", "Fish & Chips", "Dumplings", "Kimchi", "Pancakes",
46
+ "Biryani", "Croissant", "Baguette", "Miso Soup", "Ramen", "Pierogi"
47
+ ] # Extend this list with diverse cuisines
48
+
49
+ # Unit options for food weight/volume
50
+ UNIT_OPTIONS = ["grams", "ounces", "teaspoons", "tablespoons", "cups", "pieces"]
51
+
52
  # Streamlit Layout - Authentication Section
53
  st.sidebar.title("πŸ”‘ User Authentication")
54
  auth_option = st.sidebar.radio("Select an option", ["Login", "Sign Up", "Logout"])
 
85
 
86
  # Streamlit Layout - Three Panel Design
87
  st.title("🍽️ Food Image Review & Annotation")
88
+ col1, col2 = st.columns([1, 1])
89
 
90
  # Compliance & Disclaimer Section
91
  st.markdown("### **Terms & Conditions**")
 
112
  if "original_image" in st.session_state:
113
  original_img = st.session_state["original_image"]
114
  processed_img = resize_image(original_img)
115
+
116
  # πŸ–ΌοΈ Panel 1: Original Image
117
  with col1:
118
  st.subheader("πŸ“· Original Image")
119
+ st.image(original_img, caption=f"Uploaded ({original_img.width}x{original_img.height} pixels)", use_column_width=True)
120
 
121
  # πŸ–ΌοΈ Panel 2: Resized Image (512x512 Maintaining Aspect Ratio)
122
  with col2:
123
  st.subheader("πŸ–ΌοΈ Processed Image")
124
+ st.image(processed_img, caption=f"Processed ({processed_img.width}x{processed_img.height} pixels)", use_column_width=True)
125
+
126
+ # πŸ“ Annotation Table
127
+ st.subheader("πŸ“ Add Annotations")
128
+
129
+ if "annotations" not in st.session_state:
130
+ st.session_state["annotations"] = []
131
+
132
+ # Create dataframe for annotations
133
+ annotation_df = pd.DataFrame(st.session_state["annotations"], columns=["Food Item", "Unit", "Quantity", "Ingredients"])
134
+
135
+ # Food input with intellisense
136
+ food_item = st.selectbox("Select food item or type custom:", FOOD_SUGGESTIONS + ["Other"])
137
+ if food_item == "Other":
138
+ food_item = st.text_input("Enter food item manually:")
139
+
140
+ unit = st.selectbox("Select unit:", UNIT_OPTIONS)
141
+ quantity = st.number_input("Enter quantity:", min_value=0.1, step=0.1)
142
+
143
+ # Dynamic ingredient input
144
+ ingredients = st.text_area("Enter ingredients (one per line):")
145
+ ingredient_list = [i.strip() for i in ingredients.split("\n") if i.strip()]
146
+
147
+ if st.button("Add Food Item"):
148
+ if food_item and unit and quantity:
149
+ st.session_state["annotations"].append([food_item, unit, quantity, ingredient_list])
150
+ st.success(f"βœ… Added {food_item} successfully!")
151
+
152
+ # Display annotation table
153
+ st.dataframe(pd.DataFrame(st.session_state["annotations"], columns=["Food Item", "Unit", "Quantity", "Ingredients"]))
154
+
155
+ # Save Annotations to DynamoDB
156
+ if st.button("Save Annotations"):
157
+ metadata_table.update_item(
158
+ Key={"image_id": uploaded_file.name},
159
+ UpdateExpression="SET user_id = :u, annotations = :a, processing_status = :p, s3_url = :s, tokens_earned = :t",
160
+ ExpressionAttributeValues={
161
+ ":u": st.session_state["user_id"],
162
+ ":a": st.session_state["annotations"],
163
+ ":p": "processed",
164
+ ":s": f"s3://{S3_BUCKET_NAME}/raw-uploads/{uploaded_file.name}",
165
+ ":t": len(st.session_state["annotations"])
166
+ },
167
+ )
168
+ st.success("βœ… Annotations saved successfully!")