tdurzynski commited on
Commit
34fb86a
Β·
verified Β·
1 Parent(s): 5b04f8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -32
app.py CHANGED
@@ -6,6 +6,8 @@ import base64
6
  import boto3
7
  from PIL import Image
8
  from streamlit_tags import st_tags
 
 
9
 
10
  # Load AWS credentials using correct HF Secrets
11
  AWS_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY")
@@ -17,6 +19,11 @@ DYNAMODB_TABLE = os.getenv("DYNAMODB_TABLE", "image_metadata")
17
  # Load Firebase credentials
18
  FIREBASE_CONFIG = json.loads(os.getenv("FIREBASE_CONFIG", "{}"))
19
 
 
 
 
 
 
20
  # Initialize AWS Services (S3 & DynamoDB)
21
  s3 = boto3.client(
22
  "s3",
@@ -25,16 +32,50 @@ s3 = boto3.client(
25
  )
26
 
27
  dynamodb = boto3.resource(
28
- "dynamodb",
29
  region_name=AWS_REGION,
30
- aws_access_key_id=AWS_ACCESS_KEY,
31
  aws_secret_access_key=AWS_SECRET_KEY,
32
  )
33
  metadata_table = dynamodb.Table(DYNAMODB_TABLE)
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  # Streamlit Layout - Three Panel Design
36
  st.title("🍽️ Food Image Review & Annotation")
37
- col1, col2, col3 = st.columns([1, 1, 1]) # Equal size columns
38
 
39
  # Upload Image
40
  uploaded_file = st.file_uploader("Upload an image of your food", type=["jpg", "png", "jpeg"])
@@ -68,32 +109,4 @@ if "original_image" in st.session_state:
68
  with col3:
69
  st.subheader("πŸ“ Add Annotations")
70
 
71
- # Expanded food suggestions
72
- suggested_foods = [
73
- "Pizza", "Pasta", "Tacos", "Sushi", "Ramen", "Kimchi", "Bratwurst", "Jambalaya",
74
- "Chicken", "Rice", "Steak", "Bread", "Cheese", "Salmon", "Avocado", "Eggs",
75
- "Carrots", "Tomatoes", "Cucumber", "Yogurt", "Peanuts", "Lettuce", "Pierogies", "Mongolian Beef", "Spaghetti Bolognese",
76
- "Bigos", "Stuffed Cabbage", "Zurek", "Schnitzel", "Tomato Soup", "Potato Pancakes", "Blintzes", "Broccoli Chicken"
77
- ]
78
-
79
- # User selects food items from suggestions
80
- food_items = st_tags(
81
- label="Enter food items",
82
- text="Add items...",
83
- value=[],
84
- suggestions=suggested_foods,
85
- maxtags=10,
86
- )
87
-
88
- if st.button("Save Annotations"):
89
- metadata_table.update_item(
90
- Key={"image_id": uploaded_file.name},
91
- UpdateExpression="SET annotations = :a, processing_status = :p, s3_url = :s, tokens_earned = :t",
92
- ExpressionAttributeValues={
93
- ":a": food_items,
94
- ":p": "processed",
95
- ":s": f"s3://{S3_BUCKET_NAME}/raw-uploads/{uploaded_file.name}",
96
- ":t": 1 if food_items else 0
97
- },
98
- )
99
- st.success("βœ… Annotations saved successfully!")
 
6
  import boto3
7
  from PIL import Image
8
  from streamlit_tags import st_tags
9
+ import firebase_admin
10
+ from firebase_admin import credentials, auth
11
 
12
  # Load AWS credentials using correct HF Secrets
13
  AWS_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY")
 
19
  # Load Firebase credentials
20
  FIREBASE_CONFIG = json.loads(os.getenv("FIREBASE_CONFIG", "{}"))
21
 
22
+ # Initialize Firebase Admin SDK (Prevent multiple initialization)
23
+ if not firebase_admin._apps:
24
+ cred = credentials.Certificate(FIREBASE_CONFIG)
25
+ firebase_admin.initialize_app(cred)
26
+
27
  # Initialize AWS Services (S3 & DynamoDB)
28
  s3 = boto3.client(
29
  "s3",
 
32
  )
33
 
34
  dynamodb = boto3.resource(
35
+ "dynamodb",
36
  region_name=AWS_REGION,
37
+ aws_access_key_id=AWS_ACCESS_KEY,
38
  aws_secret_access_key=AWS_SECRET_KEY,
39
  )
40
  metadata_table = dynamodb.Table(DYNAMODB_TABLE)
41
 
42
+ # Streamlit Layout - Authentication Section
43
+ st.sidebar.title("πŸ”‘ User Authentication")
44
+ auth_option = st.sidebar.radio("Select an option", ["Login", "Sign Up", "Logout"])
45
+
46
+ if auth_option == "Sign Up":
47
+ email = st.sidebar.text_input("Email")
48
+ password = st.sidebar.text_input("Password", type="password")
49
+ if st.sidebar.button("Sign Up"):
50
+ try:
51
+ user = auth.create_user(email=email, password=password)
52
+ st.sidebar.success("βœ… User created successfully! Please log in.")
53
+ except Exception as e:
54
+ st.sidebar.error(f"Error: {e}")
55
+
56
+ if auth_option == "Login":
57
+ email = st.sidebar.text_input("Email")
58
+ password = st.sidebar.text_input("Password", type="password")
59
+ if st.sidebar.button("Login"):
60
+ try:
61
+ user = auth.get_user_by_email(email)
62
+ st.session_state["user_id"] = user.uid
63
+ st.sidebar.success("βœ… Logged in successfully!")
64
+ except Exception as e:
65
+ st.sidebar.error(f"Login failed: {e}")
66
+
67
+ if auth_option == "Logout" and "user_id" in st.session_state:
68
+ del st.session_state["user_id"]
69
+ st.sidebar.success("βœ… Logged out successfully!")
70
+
71
+ # Ensure user is logged in before uploading
72
+ if "user_id" not in st.session_state:
73
+ st.warning("⚠️ Please log in to upload images.")
74
+ st.stop()
75
+
76
  # Streamlit Layout - Three Panel Design
77
  st.title("🍽️ Food Image Review & Annotation")
78
+ col1, col2, col3 = st.columns([1, 1, 1])
79
 
80
  # Upload Image
81
  uploaded_file = st.file_uploader("Upload an image of your food", type=["jpg", "png", "jpeg"])
 
109
  with col3:
110
  st.subheader("πŸ“ Add Annotations")
111
 
112
+ # Expanded food suggestions