File size: 5,379 Bytes
8a7a466
8bb74bc
 
 
8f31dd0
34fb86a
 
9d30b87
8bb74bc
a6c5937
094dc67
 
865085d
496c07a
a6c5937
496c07a
865085d
a6c5937
496c07a
34fb86a
 
 
 
 
a6c5937
 
 
 
 
496c07a
8bb74bc
a6c5937
34fb86a
a6c5937
34fb86a
a6c5937
 
 
8bb74bc
9d30b87
 
 
372c6f5
 
 
 
 
 
9d30b87
 
1f27a2f
9d30b87
34fb86a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f27a2f
34fb86a
 
 
 
 
 
 
 
 
 
 
 
 
a6c5937
 
9d30b87
8a7a466
094dc67
 
 
 
 
b5d02dd
1f27a2f
 
 
 
094dc67
a6c5937
 
 
522835b
 
1f27a2f
a6c5937
1f27a2f
a6c5937
 
522835b
 
 
 
 
 
 
 
 
 
9d30b87
522835b
9d30b87
1f27a2f
522835b
 
1f27a2f
522835b
 
1f27a2f
522835b
9d30b87
1f27a2f
372c6f5
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import streamlit as st
import json
import os
import boto3
from PIL import Image
import firebase_admin
from firebase_admin import credentials, auth
import pandas as pd

# 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 Firebase Admin SDK (Prevent multiple initialization)
if not firebase_admin._apps:
    cred = credentials.Certificate(FIREBASE_CONFIG)
    firebase_admin.initialize_app(cred)

# 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)

# Food Intellisense List
FOOD_SUGGESTIONS = [
    "Apple", "Banana", "Pizza", "Burger", "Pasta", "Sushi", "Tacos", "Salad",
    "Chicken (Baked)", "Chicken (Roasted)", "Chicken (Grilled)", "Chicken (Boiled)",
    "Fish (Grilled)", "Fish (Fried)", "Fish (Steamed)", "Beef Steak", "Pork Chops",
    "Spaghetti Carbonara", "Lasagna", "Pad Thai", "Dim Sum", "Kimchi Fried Rice",
    "Biryani", "Croissant", "Baguette", "Miso Soup", "Ramen", "Pierogi", "Gyoza",
    "Schnitzel", "Creole Gumbo", "Jambalaya", "Tandoori Chicken", "Falafel", "Shawarma"
]  # Extended with diverse cuisines

# Unit options for food weight/volume
UNIT_OPTIONS = ["grams", "ounces", "teaspoons", "tablespoons", "cups", "slices", "pieces"]

# Streamlit Layout - Authentication Section
st.sidebar.title("๐Ÿ”‘ User Authentication")
auth_option = st.sidebar.radio("Select an option", ["Login", "Sign Up", "Logout"])

if auth_option == "Sign Up":
    email = st.sidebar.text_input("Email")
    password = st.sidebar.text_input("Password", type="password")
    if st.sidebar.button("Sign Up"):
        try:
            user = auth.create_user(email=email, password=password)
            st.sidebar.success("โœ… User created successfully! Please log in.")
        except Exception as e:
            st.sidebar.error(f"Error: {e}")

if auth_option == "Login":
    email = st.sidebar.text_input("Email")
    password = st.sidebar.text_input("Password", type="password")
    if st.sidebar.button("Login"):
        try:
            user = auth.get_user_by_email(email)
            st.session_state["user_id"] = user.uid
            st.session_state["tokens"] = 0  # Initialize token count
            st.sidebar.success("โœ… Logged in successfully!")
        except Exception as e:
            st.sidebar.error(f"Login failed: {e}")

if auth_option == "Logout" and "user_id" in st.session_state:
    del st.session_state["user_id"]
    st.sidebar.success("โœ… Logged out successfully!")

# Ensure user is logged in before uploading
if "user_id" not in st.session_state:
    st.warning("โš ๏ธ Please log in to upload images.")
    st.stop()

# Streamlit Layout - Three Panel Design
st.title("๐Ÿฝ๏ธ Food Image Review & Annotation")
col1, col2 = st.columns([1, 1])

# Compliance & Disclaimer Section
st.markdown("### **Terms & Conditions**")
st.write(
    "By uploading an image, you agree to transfer full copyright to the research team for AI training purposes."
    " You are responsible for ensuring you own the image and it does not violate any copyright laws."
    " We do not guarantee when tokens will be redeemable. Keep track of your user ID.")
terms_accepted = st.checkbox("I agree to the terms and conditions", key="terms_accepted")
if not terms_accepted:
    st.warning("โš ๏ธ You must agree to the terms before proceeding.")
    st.stop()

# Upload Image
uploaded_file = st.file_uploader("Upload an image of your food", type=["jpg", "png", "jpeg"])
if uploaded_file:
    original_img = Image.open(uploaded_file)
    st.session_state["original_image"] = original_img
    st.session_state["tokens"] += 1  # Earn 1 token for upload

# If an image has been uploaded, process and display it
if "original_image" in st.session_state:
    original_img = st.session_state["original_image"]
    
    def resize_image(image, max_size=512):
        aspect_ratio = image.width / image.height
        if image.width > image.height:
            new_width = max_size
            new_height = int(max_size / aspect_ratio)
        else:
            new_height = max_size
            new_width = int(max_size * aspect_ratio)
        return image.resize((new_width, new_height))

    processed_img = resize_image(original_img)

    col1, col2 = st.columns(2)
    with col1:
        st.subheader("๐Ÿ“ท Original Image")
        st.image(original_img, caption=f"Original ({original_img.width}x{original_img.height} pixels)", use_container_width=True)
    with col2:
        st.subheader("๐Ÿ–ผ๏ธ Processed Image")
        st.image(processed_img, caption=f"Processed ({processed_img.width}x{processed_img.height} pixels)", use_container_width=True)
    st.session_state["processed_image"] = processed_img

# Display earned tokens
st.success(f"๐ŸŽ‰ Tokens Earned: {st.session_state['tokens']}")