File size: 3,390 Bytes
dd5ae0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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("<br><br><h5 style='text-align: center;'>Developed by M.Nabeel</h5>", unsafe_allow_html=True)

if __name__ == "__main__":
    main()