|
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
|
|
|
|
|
|
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"
|
|
recipe_text += "-" * 40 + "\n"
|
|
else:
|
|
recipe_text = "No recipes found."
|
|
|
|
return recipe_text
|
|
|
|
|
|
def main():
|
|
st.title("Food Classifier and Recipe Finder")
|
|
st.write("Choose an option to get food recipes.")
|
|
|
|
|
|
option = st.radio("Choose an option", ("Search Food Recipe", "Upload Image to Predict Food"))
|
|
|
|
|
|
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)
|
|
|
|
|
|
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)
|
|
|
|
|
|
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:
|
|
|
|
image = Image.open(image_file).convert("RGB")
|
|
|
|
|
|
st.image(image, caption="Uploaded Image", use_container_width=True)
|
|
|
|
|
|
label_with_pipeline = classify_food_with_pipeline(image)
|
|
st.write(f"**Predicted Food**: {label_with_pipeline}")
|
|
|
|
|
|
recipes = fetch_recipe(label_with_pipeline)
|
|
|
|
|
|
recipe_text = display_recipes(recipes)
|
|
st.text_area("Recipe Details", recipe_text, height=300)
|
|
|
|
|
|
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)
|
|
|
|
st.markdown("<br><br><h5 style='text-align: center;'>Developed by M.Nabeel</h5>", unsafe_allow_html=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|