import streamlit as st
from PIL import Image
from image_classifier import classify_food_with_pipeline
from recipe_fetcher import fetch_recipe, display_recipes
from nutrition_fetcher import fetch_nutrition
from pdf_generator import generate_pdf
def main():
st.title("Food Classifier and Recipe Finder")
st.write("Choose an option to get food recipes and nutrition details.")
# Option selection
option = st.radio("Choose an option", ("Search Food Recipe", "Upload Image to Predict Food"))
# Search Food Recipe Option
if option == "Search Food Recipe":
query = st.text_input("Enter a food name", "")
if query:
try:
# Fetch and display recipes
recipes = fetch_recipe(query)
recipe_text = display_recipes(recipes)
st.text_area("Recipe Details", recipe_text, height=300)
# Fetch and display nutrition details
st.write("### Nutrition Details")
nutrition_df = fetch_nutrition(query)
if nutrition_df is not None:
st.dataframe(nutrition_df)
else:
st.write("No nutrition details found.")
# Generate PDF
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)
except Exception as e:
st.error(f"An error occurred while fetching data: {e}")
# Upload Image Option
elif option == "Upload Image to Predict Food":
image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if image_file is not None:
try:
# Display and process image
image = Image.open(image_file).convert("RGB")
st.image(image, caption="Uploaded Image", use_container_width=True) # Updated parameter
# Predict Food
label = classify_food_with_pipeline(image)
st.write(f"**Predicted Food**: {label}")
# Fetch and display recipes
recipes = fetch_recipe(label)
recipe_text = display_recipes(recipes)
st.text_area("Recipe Details", recipe_text, height=300)
# Fetch and display nutrition details
st.write("### Nutrition Details")
nutrition_df = fetch_nutrition(label)
if nutrition_df is not None:
st.dataframe(nutrition_df)
else:
st.write("No nutrition details found.")
# Generate PDF
if "No recipes found." not in recipe_text:
pdf_file = generate_pdf(recipe_text, label)
with open(pdf_file, "rb") as f:
st.download_button("Download Recipe as PDF", f, file_name=pdf_file)
except Exception as e:
st.error(f"An error occurred while processing the image: {e}")
st.markdown("