tonyliu404 commited on
Commit
015eded
·
verified ·
1 Parent(s): c738c48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -72
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
- print("get_response query: ", query)
180
- return rag_chain.invoke(query)
 
 
181
 
182
 
183
  ##############################################
@@ -292,8 +295,9 @@ selected_dish = st.sidebar.selectbox(
292
  options=class_names,
293
  index=0
294
  )
 
295
 
296
- # Right title
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
- if not uploaded_image:
317
- placeholder = Image.open("dish-placeholder.jpg")
318
- st.image(placeholder, caption="Placeholder Image.", use_container_width=True)
319
- st.write("Top Predictions:")
320
- st.markdown(f"*Donuts*: 98.11%")
 
 
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
- # Display the image
332
- st.image(input_image, caption="Uploaded Image.", use_container_width=True)
333
-
334
- predictions = classifyImage(input_image)
335
- print("Predictions: ", predictions)
336
- fpredictions = ""
337
-
338
- # Show the top predictions with percentages
339
- st.write("Top Predictions:")
340
- for class_name, confidence in predictions:
341
- if int(confidence) > 0.05:
342
- fpredictions += f"{class_name}: {confidence:.2f}%,"
343
- if int(confidence) > 5:
344
- class_name = class_name.replace("_", " ")
345
- class_name = class_name.title()
346
- st.markdown(f"*{class_name}*: {confidence:.2f}%")
347
- print(fpredictions)
 
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
- if query:
384
- # Call the OpenAI API
385
- openAIresponse = llm.invoke(openAICall)
386
- print("AI CALL RESPONSE: ", openAIresponse.content, "END AI CALL RESONSE")
387
-
388
- RAGresponse = get_response(openAIresponse.content + " " + query)
389
- else:
390
- RAGresponse = get_response(predictions[0][0])
391
- print("RAGresponse: ", RAGresponse)
392
-
393
- with st.expander("Recipe Generation", expanded=True, icon=':material/menu_book:'):
394
- st.title('Results: RAG')
395
- display_response(RAGresponse)
396
  elif uploaded_image is not None:
397
- with st.expander("**Food Classification**", expanded=True, icon=':material/search_insights:'):
398
- st.title("Results: Image Classification")
399
- # Open the image
400
- input_image = Image.open(uploaded_image)
401
-
402
- # Display the image
403
- st.image(input_image, caption="Uploaded Image.", use_container_width=True)
404
-
405
- # Classify the image and display the result
406
- predictions = classifyImage(input_image)
407
- fpredictions = ""
408
-
409
- # Show the top predictions with percentages
410
- st.write("Top Predictions:")
411
- for class_name, confidence in predictions:
412
- if int(confidence) > 0.05:
413
- fpredictions += f"{class_name}: {confidence:.2f}%,"
414
- if int(confidence) > 5:
415
- class_name = class_name.replace("_", " ")
416
- class_name = class_name.title()
417
- st.markdown(f"*{class_name}*: {confidence:.2f}%")
418
- print(fpredictions)
 
419
 
420
  elif recipe_submit:
421
- response = get_response(query)
422
- print(response)
423
- with st.expander("**Recipe Generation**", expanded=True, icon=':material/menu_book:'):
424
- st.title("Results: RAG")
425
- display_response(response)
 
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