Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline
|
3 |
from PIL import Image
|
4 |
-
import
|
5 |
import os
|
6 |
|
|
|
|
|
|
|
7 |
# Load the image classification pipeline
|
8 |
@st.cache_resource
|
9 |
def load_image_classification_pipeline():
|
@@ -11,33 +14,31 @@ def load_image_classification_pipeline():
|
|
11 |
|
12 |
pipe_classification = load_image_classification_pipeline()
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
def load_llama_pipeline():
|
17 |
-
# Retrieve Hugging Face token from environment variables
|
18 |
-
token = os.getenv("HF_AUTH_TOKEN")
|
19 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
20 |
-
"meta-llama/Llama-3.2-3B-Instruct",
|
21 |
-
use_auth_token=token
|
22 |
-
)
|
23 |
-
model = AutoModelForCausalLM.from_pretrained(
|
24 |
-
"meta-llama/Llama-3.2-3B-Instruct",
|
25 |
-
use_auth_token=token
|
26 |
-
)
|
27 |
-
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
28 |
-
|
29 |
-
pipe_llama = load_llama_pipeline()
|
30 |
-
|
31 |
-
# Function to generate ingredients using Meta-Llama
|
32 |
-
def get_ingredients(food_name):
|
33 |
prompt = f"List the main ingredients typically used to prepare {food_name}:"
|
34 |
-
response =
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Streamlit app
|
38 |
-
st.title("Food Image
|
39 |
st.write("Upload an image to classify the type of food and get its ingredients!")
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
# Upload image
|
42 |
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
|
43 |
|
@@ -52,14 +53,12 @@ if uploaded_file is not None:
|
|
52 |
|
53 |
# Display only the top prediction
|
54 |
top_food = predictions[0]['label']
|
55 |
-
|
56 |
-
st.subheader("Top Prediction")
|
57 |
-
st.write(f"**{top_food}** with confidence {confidence:.2f}")
|
58 |
|
59 |
# Generate and display ingredients for the top prediction
|
60 |
st.subheader("Ingredients")
|
61 |
try:
|
62 |
-
ingredients =
|
63 |
st.write(ingredients)
|
64 |
except Exception as e:
|
65 |
st.write("Could not generate ingredients. Please try again later.")
|
|
|
1 |
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():
|
|
|
14 |
|
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("https://upload.wikimedia.org/wikipedia/commons/6/69/Classification_example_image.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"])
|
44 |
|
|
|
53 |
|
54 |
# Display only the top prediction
|
55 |
top_food = predictions[0]['label']
|
56 |
+
st.header(f"Food: {top_food}")
|
|
|
|
|
57 |
|
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.")
|