Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -117,9 +117,9 @@ Use the provided context to generate a result based on the following JSON format
|
|
| 117 |
"n_steps": 0,
|
| 118 |
"description": "A GENERAL description of the recipe goes here.",
|
| 119 |
"ingredients": [
|
| 120 |
-
"ingredient1",
|
| 121 |
-
"ingredient2",
|
| 122 |
-
"ingredient3"
|
| 123 |
],
|
| 124 |
"n_ingredients": 0,
|
| 125 |
"formatted_nutrition": [
|
|
@@ -145,6 +145,7 @@ Instructions:
|
|
| 145 |
4. Base the response on simple, healthy, and accessible ingredients and techniques.
|
| 146 |
5. Rewrite the description in third person
|
| 147 |
6. If context does not match {question} at all, return []
|
|
|
|
| 148 |
|
| 149 |
When choosing a recipe from the context, FOLLOW these instructions:
|
| 150 |
0. If context does not match {question} at all, return []
|
|
@@ -176,8 +177,10 @@ rag_chain = (
|
|
| 176 |
)
|
| 177 |
|
| 178 |
def get_response(query):
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
| 183 |
##############################################
|
|
@@ -292,8 +295,9 @@ selected_dish = st.sidebar.selectbox(
|
|
| 292 |
options=class_names,
|
| 293 |
index=0
|
| 294 |
)
|
|
|
|
| 295 |
|
| 296 |
-
#
|
| 297 |
st.title("Welcome to FOOD CHAIN!")
|
| 298 |
with st.expander("**What is FOOD CHAIN?**"):
|
| 299 |
st.markdown(
|
|
@@ -313,38 +317,39 @@ with st.expander("**What is FOOD CHAIN?**"):
|
|
| 313 |
)
|
| 314 |
#################
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
|
|
|
|
|
|
| 321 |
|
| 322 |
-
st.sidebar.write("Upload an image and/or enter a query to get started! Explore our trained dish types listed below for guidance.")
|
| 323 |
-
|
| 324 |
# Image Classification Section
|
| 325 |
if uploaded_image and recipe_submit:
|
| 326 |
with st.expander("**Food Classification**", expanded=True, icon=':material/search_insights:'):
|
| 327 |
st.title("Results: Image Classification")
|
| 328 |
# Open the image
|
| 329 |
input_image = Image.open(uploaded_image)
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
|
|
|
| 348 |
|
| 349 |
# call openai to pick the best classification result based on query
|
| 350 |
openAICall = [
|
|
@@ -379,50 +384,52 @@ if uploaded_image and recipe_submit:
|
|
| 379 |
"""
|
| 380 |
),
|
| 381 |
]
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
elif uploaded_image is not None:
|
| 397 |
-
with
|
| 398 |
-
st.
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
|
|
|
| 419 |
|
| 420 |
elif recipe_submit:
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
st.
|
| 425 |
-
|
|
|
|
| 426 |
else:
|
| 427 |
st.warning("Please input an image and/or a prompt.", icon=':material/no_meals:')
|
| 428 |
|
|
|
|
| 117 |
"n_steps": 0,
|
| 118 |
"description": "A GENERAL description of the recipe goes here.",
|
| 119 |
"ingredients": [
|
| 120 |
+
"0 tablespoons ingredient1",
|
| 121 |
+
"0 cups ingredient2",
|
| 122 |
+
"0 teaspoons ingredient3"
|
| 123 |
],
|
| 124 |
"n_ingredients": 0,
|
| 125 |
"formatted_nutrition": [
|
|
|
|
| 145 |
4. Base the response on simple, healthy, and accessible ingredients and techniques.
|
| 146 |
5. Rewrite the description in third person
|
| 147 |
6. If context does not match {question} at all, return []
|
| 148 |
+
7. Include the ingredient amounts and say them in the steps.
|
| 149 |
|
| 150 |
When choosing a recipe from the context, FOLLOW these instructions:
|
| 151 |
0. If context does not match {question} at all, return []
|
|
|
|
| 177 |
)
|
| 178 |
|
| 179 |
def get_response(query):
|
| 180 |
+
if query:
|
| 181 |
+
print("get_response query: ", query)
|
| 182 |
+
return rag_chain.invoke(query)
|
| 183 |
+
return ""
|
| 184 |
|
| 185 |
|
| 186 |
##############################################
|
|
|
|
| 295 |
options=class_names,
|
| 296 |
index=0
|
| 297 |
)
|
| 298 |
+
st.sidebar.write("Upload an image and/or enter a query to get started! Explore our trained dish types listed below for guidance.")
|
| 299 |
|
| 300 |
+
# Main title
|
| 301 |
st.title("Welcome to FOOD CHAIN!")
|
| 302 |
with st.expander("**What is FOOD CHAIN?**"):
|
| 303 |
st.markdown(
|
|
|
|
| 317 |
)
|
| 318 |
#################
|
| 319 |
|
| 320 |
+
col1, col2 = st.columns(2)
|
| 321 |
+
with col1:
|
| 322 |
+
if not uploaded_image:
|
| 323 |
+
placeholder = Image.open("dish-placeholder.jpg")
|
| 324 |
+
st.image(placeholder, caption="Placeholder Image.", use_container_width=True)
|
| 325 |
+
st.write("Top Predictions:")
|
| 326 |
+
st.markdown(f"*Donuts*: 98.11%")
|
| 327 |
|
|
|
|
|
|
|
| 328 |
# Image Classification Section
|
| 329 |
if uploaded_image and recipe_submit:
|
| 330 |
with st.expander("**Food Classification**", expanded=True, icon=':material/search_insights:'):
|
| 331 |
st.title("Results: Image Classification")
|
| 332 |
# Open the image
|
| 333 |
input_image = Image.open(uploaded_image)
|
| 334 |
+
|
| 335 |
+
with col1:
|
| 336 |
+
# Display the image
|
| 337 |
+
st.image(input_image, caption="Uploaded Image.", use_container_width=True)
|
| 338 |
+
|
| 339 |
+
predictions = classifyImage(input_image)
|
| 340 |
+
print("Predictions: ", predictions)
|
| 341 |
+
fpredictions = ""
|
| 342 |
+
|
| 343 |
+
# Show the top predictions with percentages
|
| 344 |
+
st.write("Top Predictions:")
|
| 345 |
+
for class_name, confidence in predictions:
|
| 346 |
+
if int(confidence) > 0.05:
|
| 347 |
+
fpredictions += f"{class_name}: {confidence:.2f}%,"
|
| 348 |
+
if int(confidence) > 5:
|
| 349 |
+
class_name = class_name.replace("_", " ")
|
| 350 |
+
class_name = class_name.title()
|
| 351 |
+
st.markdown(f"*{class_name}*: {confidence:.2f}%")
|
| 352 |
+
print(fpredictions)
|
| 353 |
|
| 354 |
# call openai to pick the best classification result based on query
|
| 355 |
openAICall = [
|
|
|
|
| 384 |
"""
|
| 385 |
),
|
| 386 |
]
|
| 387 |
+
with col2:
|
| 388 |
+
if query:
|
| 389 |
+
# Call the OpenAI API
|
| 390 |
+
openAIresponse = llm.invoke(openAICall)
|
| 391 |
+
print("AI CALL RESPONSE: ", openAIresponse.content, "END AI CALL RESONSE")
|
| 392 |
+
|
| 393 |
+
RAGresponse = get_response(openAIresponse.content + " " + query)
|
| 394 |
+
else:
|
| 395 |
+
RAGresponse = get_response(predictions[0][0])
|
| 396 |
+
print("RAGresponse: ", RAGresponse)
|
| 397 |
+
|
| 398 |
+
with st.expander("Recipe Generation", expanded=True, icon=':material/menu_book:'):
|
| 399 |
+
st.title('Results: RAG')
|
| 400 |
+
display_response(RAGresponse)
|
| 401 |
elif uploaded_image is not None:
|
| 402 |
+
with col1:
|
| 403 |
+
with st.expander("**Food Classification**", expanded=True, icon=':material/search_insights:'):
|
| 404 |
+
st.title("Results: Image Classification")
|
| 405 |
+
# Open the image
|
| 406 |
+
input_image = Image.open(uploaded_image)
|
| 407 |
+
|
| 408 |
+
# Display the image
|
| 409 |
+
st.image(input_image, caption="Uploaded Image.", use_container_width=True)
|
| 410 |
+
|
| 411 |
+
# Classify the image and display the result
|
| 412 |
+
predictions = classifyImage(input_image)
|
| 413 |
+
fpredictions = ""
|
| 414 |
+
|
| 415 |
+
# Show the top predictions with percentages
|
| 416 |
+
st.write("Top Predictions:")
|
| 417 |
+
for class_name, confidence in predictions:
|
| 418 |
+
if int(confidence) > 0.05:
|
| 419 |
+
fpredictions += f"{class_name}: {confidence:.2f}%,"
|
| 420 |
+
if int(confidence) > 5:
|
| 421 |
+
class_name = class_name.replace("_", " ")
|
| 422 |
+
class_name = class_name.title()
|
| 423 |
+
st.markdown(f"*{class_name}*: {confidence:.2f}%")
|
| 424 |
+
print(fpredictions)
|
| 425 |
|
| 426 |
elif recipe_submit:
|
| 427 |
+
with col2:
|
| 428 |
+
response = get_response(query)
|
| 429 |
+
print(response)
|
| 430 |
+
with st.expander("**Recipe Generation**", expanded=True, icon=':material/menu_book:'):
|
| 431 |
+
st.title("Results: RAG")
|
| 432 |
+
display_response(response)
|
| 433 |
else:
|
| 434 |
st.warning("Please input an image and/or a prompt.", icon=':material/no_meals:')
|
| 435 |
|