import streamlit as st
from PIL import Image
from image_classifier import classify_food_with_pipeline
from recipe_fetcher import fetch_recipe
from pdf_generator import generate_pdf
# Function to display recipes in a readable format
def display_recipes(recipes):
recipe_text = ""
if recipes:
for recipe in recipes:
recipe_text += f"**Title**: {recipe['title']}\n"
recipe_text += "**Ingredients**:\n"
for ingredient in recipe['ingredients'].split('|'):
recipe_text += f"- {ingredient}\n"
recipe_text += f"**Servings**: {recipe['servings']}\n"
recipe_text += "**Instructions**:\n"
recipe_text += f"{recipe['instructions'][:300]}...\n" # Truncate for brevity
recipe_text += "-" * 40 + "\n"
else:
recipe_text = "No recipes found."
return recipe_text
# Main Streamlit app UI
def main():
st.title("Food Classifier and Recipe Finder")
st.write("Choose an option to get food recipes.")
# Radio buttons to select the mode (search or upload)
option = st.radio("Choose an option", ("Search Food Recipe", "Upload Image to Predict Food"))
# If 'Search Food Recipe' is selected
if option == "Search Food Recipe":
query = st.text_input("Enter a food name", "")
if query:
recipes = fetch_recipe(query)
recipe_text = display_recipes(recipes)
st.text_area("Recipe Details", recipe_text, height=300)
# Only show PDF download button if recipes are found
if "No recipes found." not in recipe_text:
pdf_file = generate_pdf(recipe_text, query)
with open(pdf_file, "rb") as f:
st.download_button("Download Recipe as PDF", f, file_name=pdf_file)
# If 'Upload Image to Predict Food' is selected
elif option == "Upload Image to Predict Food":
st.write("Upload an image to predict the food item and get the recipe.")
image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if image_file is not None:
# Open the uploaded image
image = Image.open(image_file).convert("RGB")
# Display the image
st.image(image, caption="Uploaded Image", use_container_width=True)
# Classify the food using the pipeline
label_with_pipeline = classify_food_with_pipeline(image)
st.write(f"**Predicted Food**: {label_with_pipeline}")
# Fetch the recipe based on the predicted label
recipes = fetch_recipe(label_with_pipeline)
# Display the fetched recipe(s)
recipe_text = display_recipes(recipes)
st.text_area("Recipe Details", recipe_text, height=300)
# Only show PDF download button if recipes are found
if "No recipes found." not in recipe_text:
pdf_file = generate_pdf(recipe_text, label_with_pipeline)
with open(pdf_file, "rb") as f:
st.download_button("Download Recipe as PDF", f, file_name=pdf_file)
# Add monogram at the bottom of the Streamlit app
st.markdown("