Spaces:
Sleeping
Sleeping
File size: 3,137 Bytes
04f475a e73380c 04f475a 29afa83 7b63336 f6d41de 7b63336 29afa83 e4d47cc 29afa83 f825898 04f475a f825898 800f4d4 04f475a f825898 29afa83 800f4d4 29afa83 3500d25 800f4d4 29afa83 4bfa63a 29afa83 f83534a 29afa83 f83534a 85a22e3 f83534a 29afa83 f83534a 85a22e3 f83534a 29afa83 f83534a f6d41de 4458d42 f6d41de 4bbf585 f6d41de f83534a 85a22e3 |
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 |
import streamlit as st
from transformers import pipeline
from PIL import Image
from huggingface_hub import InferenceClient
import os
from gradio_client import Client
# Hugging Face API key
API_KEY = st.secrets["HF_API_KEY"]
# Initialize the Hugging Face Inference Client
client = InferenceClient(api_key=API_KEY)
# Load the image classification pipeline
@st.cache_resource
def load_image_classification_pipeline():
"""
Load the image classification pipeline using a pretrained model.
"""
return pipeline("image-classification", model="Shresthadev403/food-image-classification")
pipe_classification = load_image_classification_pipeline()
# Function to generate ingredients using Hugging Face Inference Client
def get_ingredients_qwen(food_name):
"""
Generate a list of ingredients for the given food item using Qwen NLP model.
Returns a clean, comma-separated list of ingredients.
"""
messages = [
{
"role": "user",
"content": f"List only the main ingredients for {food_name}. "
f"Respond in a concise, comma-separated list without any extra text or explanations."
}
]
try:
completion = client.chat.completions.create(
model="Qwen/Qwen2.5-Coder-32B-Instruct",
messages=messages,
max_tokens=50
)
generated_text = completion.choices[0].message["content"].strip()
return generated_text
except Exception as e:
return f"Error generating ingredients: {e}"
# Streamlit app setup
st.title("Food Image Recognition with Ingredients")
# Add banner image
st.image("IR_IMAGE.png", caption="Food Recognition Model", use_container_width=True)
# Sidebar for model information
st.sidebar.title("Model Information")
st.sidebar.write("**Image Classification Model**: Shresthadev403/food-image-classification")
st.sidebar.write("**LLM for Ingredients**: Qwen/Qwen2.5-Coder-32B-Instruct")
# Upload image
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
# Display the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_container_width=True)
st.write("Classifying...")
# Make predictions
predictions = pipe_classification(image)
# Display only the top prediction
top_food = predictions[0]['label']
st.header(f"Food: {top_food}")
# Generate and display ingredients for the top prediction
st.subheader("Ingredients")
try:
ingredients = get_ingredients_qwen(top_food)
st.write(ingredients)
except Exception as e:
st.error(f"Error generating ingredients: {e}")
st.subheader("Healthier alternatives:")
try:
client = Client("https://8a56cb969da1f9d721.gradio.live/")
result = client.predict(query=f"What's a healthy {top_food} recipe, and why is it healthy?", api_name="/get_response")
st.write(result)
except Exception as e:
st.error(f"Unable to contact RAG: {e}")
# Footer
st.sidebar.markdown("Developed by Muhammad Hassan Butt.") |