HassanDataSci commited on
Commit
ff3533c
·
verified ·
1 Parent(s): 881c292

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
app.py CHANGED
@@ -2,11 +2,13 @@ import streamlit as st
2
  from transformers import pipeline
3
  from PIL import Image
4
  import openai
5
- import os
6
 
7
- # Set your OpenAI API key (replace YOUR_OPENAI_API_KEY with your key)
8
  openai.api_key = "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"
9
 
 
 
 
10
  # Load the image classification pipeline
11
  @st.cache_resource
12
  def load_image_classification_pipeline():
@@ -15,29 +17,21 @@ def load_image_classification_pipeline():
15
  pipe_classification = load_image_classification_pipeline()
16
 
17
  # Function to generate ingredients using OpenAI
18
- def get_ingredients_openai(food_name, model="text-davinci-003"):
19
  prompt = f"List the main ingredients typically used to prepare {food_name}:"
20
  response = openai.Completion.create(
21
- engine=model, # Specify the model here
22
  prompt=prompt,
23
  max_tokens=50
24
  )
25
  return response['choices'][0]['text'].strip()
26
 
27
  # Streamlit app
28
- st.title("Food Image Recognition Model")
29
- st.write("Upload an image to classify the type of food and get its ingredients!")
30
-
31
- # Display a sample image showing the concept of image recognition
32
- st.image("/Users/hassanbutt/Desktop/Screenshot 2024-11-19 at 8.04.00 PM.png",
33
- caption="Example of an Image Recognition Model", use_column_width=True)
34
 
35
- # Select OpenAI model
36
- st.sidebar.title("Choose a Model")
37
- model_choice = st.sidebar.selectbox(
38
- "Select an OpenAI Model:",
39
- ["text-davinci-003", "gpt-3.5-turbo", "gpt-4", "curie"]
40
- )
41
 
42
  # Upload image
43
  uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
@@ -58,7 +52,7 @@ if uploaded_file is not None:
58
  # Generate and display ingredients for the top prediction
59
  st.subheader("Ingredients")
60
  try:
61
- ingredients = get_ingredients_openai(top_food, model=model_choice)
62
  st.write(ingredients)
63
  except Exception as e:
64
  st.write("Could not generate ingredients. Please try again later.")
 
2
  from transformers import pipeline
3
  from PIL import Image
4
  import openai
 
5
 
6
+ # Set your OpenAI API key
7
  openai.api_key = "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"
8
 
9
+ # OpenAI model to use
10
+ OPENAI_MODEL = "gpt-4o" # Replace with the model you want to display
11
+
12
  # Load the image classification pipeline
13
  @st.cache_resource
14
  def load_image_classification_pipeline():
 
17
  pipe_classification = load_image_classification_pipeline()
18
 
19
  # Function to generate ingredients using OpenAI
20
+ def get_ingredients_openai(food_name):
21
  prompt = f"List the main ingredients typically used to prepare {food_name}:"
22
  response = openai.Completion.create(
23
+ engine=OPENAI_MODEL,
24
  prompt=prompt,
25
  max_tokens=50
26
  )
27
  return response['choices'][0]['text'].strip()
28
 
29
  # Streamlit app
30
+ st.title("Food Image Recognition with Ingredients")
 
 
 
 
 
31
 
32
+ # Display OpenAI model being used
33
+ st.sidebar.title("Model Information")
34
+ st.sidebar.write(f"**OpenAI Model Used**: {OPENAI_MODEL}")
 
 
 
35
 
36
  # Upload image
37
  uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
 
52
  # Generate and display ingredients for the top prediction
53
  st.subheader("Ingredients")
54
  try:
55
+ ingredients = get_ingredients_openai(top_food)
56
  st.write(ingredients)
57
  except Exception as e:
58
  st.write("Could not generate ingredients. Please try again later.")