Spaces:
Sleeping
Sleeping
File size: 3,602 Bytes
8a7a466 8bb74bc 8f31dd0 8bb74bc 8f31dd0 a6c5937 8bb74bc a6c5937 865085d 496c07a a6c5937 496c07a 865085d a6c5937 496c07a a6c5937 496c07a 8bb74bc a6c5937 8bb74bc a6c5937 8a7a466 a6c5937 496c07a a6c5937 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
import streamlit as st
import json
import os
import io
import base64
import boto3
from PIL import Image
from streamlit_tags import st_tags
# Load AWS credentials using correct HF Secrets
AWS_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY")
AWS_SECRET_KEY = os.getenv("AWS_SECRET_KEY")
AWS_REGION = os.getenv("AWS_REGION", "us-east-1")
S3_BUCKET_NAME = os.getenv("S3_BUCKET_NAME", "food-image-crowdsourcing")
DYNAMODB_TABLE = os.getenv("DYNAMODB_TABLE", "image_metadata")
# Load Firebase credentials
FIREBASE_CONFIG = json.loads(os.getenv("FIREBASE_CONFIG", "{}"))
# Initialize AWS Services (S3 & DynamoDB)
s3 = boto3.client(
"s3",
aws_access_key_id=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_KEY,
)
dynamodb = boto3.resource(
"dynamodb",
region_name=AWS_REGION,
aws_access_key_id=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_KEY,
)
metadata_table = dynamodb.Table(DYNAMODB_TABLE)
# Streamlit Layout - Three Panel Design
st.title("🍽️ Food Image Review & Annotation")
col1, col2, col3 = st.columns([1, 1, 1]) # Equal size columns
# Upload Image
uploaded_file = st.file_uploader("Upload an image of your food", type=["jpg", "png", "jpeg"])
if uploaded_file:
image = Image.open(uploaded_file)
st.session_state["original_image"] = image
# Resize image in Streamlit before sending to S3
def resize_image(image):
aspect_ratio = image.width / image.height
new_width = 512
new_height = int(new_width / aspect_ratio)
resized_image = image.resize((new_width, new_height))
return resized_image
if "original_image" in st.session_state:
original_img = st.session_state["original_image"]
processed_img = resize_image(original_img)
# 🖼️ Panel 1: Original Image
with col1:
st.subheader("📷 Original Image")
st.image(original_img, caption="Uploaded", use_container_width=True)
# 🖼️ Panel 2: Resized Image (512x512 Maintaining Aspect Ratio)
with col2:
st.subheader("🖼️ Processed Image")
st.image(processed_img, caption="512x512 Maintained Aspect", use_container_width=True)
# ✏️ Panel 3: Food Annotations with Intellisense
with col3:
st.subheader("📝 Add Annotations")
# Expanded food suggestions
suggested_foods = [
"Pizza", "Pasta", "Tacos", "Sushi", "Ramen", "Kimchi", "Bratwurst", "Jambalaya",
"Chicken", "Rice", "Steak", "Bread", "Cheese", "Salmon", "Avocado", "Eggs",
"Carrots", "Tomatoes", "Cucumber", "Yogurt", "Peanuts", "Lettuce", "Pierogies", "Mongolian Beef", "Spaghetti Bolognese",
"Bigos", "Stuffed Cabbage", "Zurek", "Schnitzel", "Tomato Soup", "Potato Pancakes", "Blintzes", "Broccoli Chicken"
]
# User selects food items from suggestions
food_items = st_tags(
label="Enter food items",
text="Add items...",
value=[],
suggestions=suggested_foods,
maxtags=10,
)
if st.button("Save Annotations"):
metadata_table.update_item(
Key={"image_id": uploaded_file.name},
UpdateExpression="SET annotations = :a, processing_status = :p, s3_url = :s, tokens_earned = :t",
ExpressionAttributeValues={
":a": food_items,
":p": "processed",
":s": f"s3://{S3_BUCKET_NAME}/raw-uploads/{uploaded_file.name}",
":t": 1 if food_items else 0
},
)
st.success("✅ Annotations saved successfully!")
|