Spaces:
Sleeping
Sleeping
File size: 10,293 Bytes
89d395c f4a985e 89d395c |
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
from io import BytesIO
import pandas as pd
import numpy as np
import requests
import streamlit as st
import easyocr
import PIL
from PIL import Image, ImageDraw
from matplotlib import pyplot as plt
import streamlit.components.v1 as components
import re
from streamlit import session_state
# from streamlit_lottie import st_lottie
import streamlit as st
import os
from dotenv import load_dotenv, find_dotenv
st.set_page_config(
page_title="Intro",
layout='wide'
)
# Load environment variables
_ = load_dotenv(find_dotenv())
# Get OpenAI API key
openai_api_key = os.getenv('OPENAI_API_KEY')
# Google Drive file ID for the video
video_file_id = '1miZ6Zz4lt0wxTGbrtR8IltVr_fOPX6Vr'
video_url = f'https://drive.google.com/uc?export=view&id={video_file_id}'
@st.cache_data
def initialize_grocery_data():
return pd.DataFrame(columns=['Name', 'Quantity', 'Additional Notes/Expiration Date'])
def manually_input_items(grocery_data):
return grocery_data
def upload_receipt():
st.header("Upload Receipt")
uploaded_file = st.file_uploader("Choose a receipt file", type=["jpg", "jpeg", "png", "pdf"])
if uploaded_file is not None:
st.success("Receipt uploaded successfully!")
# Add your code here for processing the uploaded receipt
if 'clicked' not in st.session_state:
st.session_state.clicked = False
if 'total_groceries' not in st.session_state:
st.session_state.total_groceries = {}
if 'grocery_data' not in st.session_state:
st.session_state.grocery_data = pd.DataFrame(columns=['Name', 'Quantity', 'Additional Notes/Expiration Date'])
if 'editor_key' not in st.session_state:
st.session_state.editor_key = 0
if 'manual_entry' not in st.session_state:
st.session_state.manual_entry = True
if 'file_uploaded' not in st.session_state:
st.session_state.file_uploaded = False
if 'ocr_extracted_values' not in st.session_state:
session_state.ocr_extracted_values = pd.DataFrame()
def click_save():
session_state.clicked = True
def switch_style():
session_state.manual_entry = not session_state.manual_entry
# Define the HTML and CSS styling for the title
title_html = """
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Cedarville Cursive' rel='stylesheet'>
<style>
body {
font-family: 'serif;
font-size: 22px;
text-align: center;
}
h1 {
font-family: 'Cedarville Cursive';
font-size: 56px;
display: inline-block;
}
</style>
</head>
<body>
<h1>Nom Nom!</h1>
</body>
</html>
"""
# Use st.markdown to display the title with the specified font
st.markdown(title_html, unsafe_allow_html=True)
caption_html = """
<div style="display: flex; justify-content: center; align-items: center; font-style: italic; font-weight: bold;">
<p>Helping College Students Keep Track of Groceries</p>
</div>
"""
st.markdown(caption_html, unsafe_allow_html=True)
ingredient_quantities = {}
# Initialize or retrieve the existing dataframe to store grocery items
#grocery_data = initialize_grocery_data()
tab1, tab2 = st.tabs(["Add Groceries", "Available Groceries"])
df = pd.DataFrame()
with tab1:
st.button("Switch Input", on_click=switch_style)
col1, col2 = st.columns([1.5, 1])
if session_state.manual_entry:
with col1:
if not session_state.clicked:
st.subheader("Add Groceries - Manually Input Items")
session_state.grocery_data = st.data_editor(pd.DataFrame(columns=['Name', 'Quantity', 'Additional Notes/Expiration Date']), key=session_state.editor_key, num_rows="dynamic", hide_index=True, width=550)
print(session_state.grocery_data)
st.button("Save", on_click=click_save)
else:
session_state.editor_key += 1
with col2:
file_id = '1_3FjYmWlJcY182qnLIjFPTZYj4gneUMg'
base_url = 'https://drive.google.com/uc?id='
image_url = base_url + file_id
response = requests.get(image_url)
img = Image.open(BytesIO(response.content))
# image1 = Image.open(r'https://drive.google.com/file/d/1_3FjYmWlJcY182qnLIjFPTZYj4gneUMg/view?usp=sharing')
st.image(img, width=400)
st.write("---")
else:
session_state.file_uploaded = False
with col1:
st.subheader("Upload receipt instead")
uploaded_file = st.file_uploader("Choose a receipt file", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
session_state.file_uploaded = True
st.success("Receipt uploaded successfully!")
st.write("")
if session_state.file_uploaded:
receipt_image = Image.open(uploaded_file)
st.image(receipt_image)
reader = easyocr.Reader(['en'], gpu=False)
result = reader.readtext(np.array(receipt_image))
text_list = []
confidence_list = []
for idx in range(len(result)):
pred_coor = result[idx][0]
pred_text = result[idx][1]
pred_confidence = result[idx][2]
text_list.append(pred_text)
confidence_list.append(pred_confidence)
df = pd.DataFrame({'Text': text_list})
with col2:
if session_state.file_uploaded:
st.subheader("Updated Grocery List")
df = df[~df['Text'].str.contains(r'[\d]')]
df['Add to List?'] = None
df2 = st.data_editor(df, use_container_width=True, num_rows = 'dynamic', column_config={"Text": "Item",
"Add to List?" : st.column_config.CheckboxColumn(
"Add to List?",
help="Select if you want to add this to the list.",
default=False
)}, key="OCR_DataFrame")
session_state.ocr_extracted_values = df2
st.button("Save", on_click=click_save)
def ingredient_html_maker(ingredient, num_ingredients, map_for_grocery):
return_val = f"<div class=\"card\" style=\"background-color: #dedede; padding: 10px; margin: 5px; border-radius: 10px;\">\n"
return_val += f"<h3>{ingredient}</h3>\n" # Ingredient name
return_val += f"<p>Quantity: <span id='quantity_{ingredient}'>{num_ingredients}</span></p>\n"
return_val += f"<button onclick=\"updateQuantity('{ingredient}', '-', {map_for_grocery})\">-</button>\n"
return_val += f"<button onclick=\"updateQuantity('{ingredient}', '+', {map_for_grocery})\">+</button>\n"
return_val += f"</div>\n"
return return_val
def list_display(total_groceries):
list_html = ''
with open('List.html') as list_file:
list_html = list_file.read()
list_html_without_end = list_html[:-6]
end = list_html[-6:]
final_list_html = list_html_without_end + "\n"
for item in total_groceries:
final_list_html += ingredient_html_maker(item, total_groceries[item], total_groceries)
final_list_html += "\n" + end
return components.html(final_list_html + javascript_code, height=500)
with tab2:
javascript_code = """
<script>
function updateQuantity(ingredient, operation, grocery_data) {
// Get the current quantity
var quantityElement = document.getElementById('quantity_' + ingredient);
var currentQuantity = parseInt(quantityElement.innerText);
// Update the quantity based on the operation
if (operation === '+') {
currentQuantity += 1;
} else if (operation === '-') {
currentQuantity = Math.max(0, currentQuantity - 1);
}
grocery_data[ingredient] = currentQuantity;
console.log(grocery_data);
// Update the quantity display
quantityElement.innerText = currentQuantity;
}
</script>
"""
if session_state.clicked and session_state.manual_entry:
for i in range(len(session_state.grocery_data)):
existing_quantity = st.session_state.total_groceries.get(session_state.grocery_data["Name"][i], 0)
st.session_state.total_groceries[session_state.grocery_data["Name"][i]] = int(existing_quantity) + int(session_state.grocery_data["Quantity"][i])
list_display(st.session_state.total_groceries)
session_state.clicked = False
st.rerun()
elif session_state.clicked and not session_state.manual_entry and session_state.file_uploaded:
for idx in session_state["OCR_DataFrame"]['edited_rows']:
ingredient = session_state.ocr_extracted_values['Text'].iloc[idx]
existing_quantity = st.session_state.total_groceries.get(ingredient, 0)
st.session_state.total_groceries[ingredient]= int(existing_quantity) + 1
list_display(st.session_state.total_groceries)
session_state.clicked = False
st.rerun()
else:
list_display(st.session_state.total_groceries)
st.markdown("""
<style>
.theme {
primaryColor="#4c673b"
backgroundColor="#e7f1de"
secondaryBackgroundColor="#ffffff"
textColor="#242743"
font="monospace"
}
.stTabs [data-baseweb="tab-list"] {
gap: 10px;
}
.stTabs [data-baseweb="tab"] {
height: 50px;
width: 100%;
white-space: pre-wrap;
background-color: #c1d8a9;
border-radius: 4px 4px 0px 0px;
gap: 1px;
padding-top: 10px;
padding-bottom: 10px;
color: black; /* Added this line to set the text color */
font-weight: bold; /* Added this line to make the text bold */
}
.stTabs [aria-selected="true"] {
background-color: #c1d8a9;
color: black;
font-weight: bold; /* Added this line to make the text bold */
}
</style>""", unsafe_allow_html=True)
with open("total_groceries.txt", "w") as file:
file.write(str(session_state.total_groceries))
|