Spaces:
Runtime error
Runtime error
Commit ·
44e0053
1
Parent(s): 5228227
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from rembg import remove
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import base64
|
| 7 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 8 |
+
import random
|
| 9 |
+
|
| 10 |
+
st.set_page_config(layout="wide", page_title="Image Classification App")
|
| 11 |
+
st.write("## Image Food Classification App")
|
| 12 |
+
st.sidebar.write("## Upload and download :gear:")
|
| 13 |
+
|
| 14 |
+
# Initialize image classification and recipe generation models
|
| 15 |
+
image_classifier = pipeline("image-classification", model="mjsp/sweet")
|
| 16 |
+
recipe_model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 17 |
+
recipe_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 18 |
+
|
| 19 |
+
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB
|
| 20 |
+
|
| 21 |
+
def convert_image(img):
|
| 22 |
+
buf = BytesIO()
|
| 23 |
+
img.save(buf, format="PNG")
|
| 24 |
+
byte_im = buf.getvalue()
|
| 25 |
+
return byte_im
|
| 26 |
+
|
| 27 |
+
def fix_image(upload):
|
| 28 |
+
image = Image.open(upload)
|
| 29 |
+
col1.write("Original Image :camera:")
|
| 30 |
+
col1.image(image)
|
| 31 |
+
|
| 32 |
+
fixed = remove(image)
|
| 33 |
+
col2.write("Fixed Image :wrench:")
|
| 34 |
+
col2.image(fixed)
|
| 35 |
+
st.sidebar.markdown("\n")
|
| 36 |
+
st.sidebar.download_button("Download fixed image", convert_image(fixed), "fixed.png", "image/png")
|
| 37 |
+
|
| 38 |
+
def generate_recipe(title, max_length=200):
|
| 39 |
+
matching_entries = [entry for entry in dataset if entry["title"] == title]
|
| 40 |
+
|
| 41 |
+
if matching_entries:
|
| 42 |
+
selected_entry = random.choice(matching_entries)
|
| 43 |
+
title = selected_entry["title"]
|
| 44 |
+
ingredients = selected_entry["ingredients"]
|
| 45 |
+
else:
|
| 46 |
+
title = "Default Recipe Title"
|
| 47 |
+
ingredients = []
|
| 48 |
+
|
| 49 |
+
input_text = f"Title: {title}\nIngredients: {', '.join(ingredients)}\n Instructions:"
|
| 50 |
+
input_ids = recipe_tokenizer.encode(input_text, return_tensors="pt")
|
| 51 |
+
|
| 52 |
+
output = recipe_model.generate(input_ids, max_length=max_length, num_return_sequences=1)
|
| 53 |
+
|
| 54 |
+
generated_recipe = recipe_tokenizer.decode(output[0], skip_special_tokens=True)
|
| 55 |
+
return generated_recipe
|
| 56 |
+
|
| 57 |
+
# Your dataset of titles, ingredients, and recipes
|
| 58 |
+
dataset = [
|
| 59 |
+
{
|
| 60 |
+
"title": "Gulab Jamun",
|
| 61 |
+
"ingredients": ["milk powder", "ghee", "rose water", "saffron", "cardamom", "sugar syrup"],
|
| 62 |
+
"recipe": "Instructions for making Gulab Jamun..."
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"title": "Jalebi",
|
| 66 |
+
"ingredients": ["all-purpose flour", "yogurt", "sugar", "water", "saffron strands", "cardamom powder", "ghee or oil for frying"],
|
| 67 |
+
"recipe": "Instructions for making Jalebi..."
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
"title": "Rasgulla",
|
| 71 |
+
"ingredients": ["milk", "sugar", "lemon juice", "rose water"],
|
| 72 |
+
"recipe": "Instructions for making Rasgulla..."
|
| 73 |
+
}
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
col1, col2 = st.columns(2)
|
| 77 |
+
my_upload = st.sidebar.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 78 |
+
|
| 79 |
+
st.write()
|
| 80 |
+
st.markdown("""---""")
|
| 81 |
+
st.write()
|
| 82 |
+
|
| 83 |
+
# global label
|
| 84 |
+
# label = ""
|
| 85 |
+
|
| 86 |
+
if my_upload is not None:
|
| 87 |
+
st.image(my_upload, caption="Uploaded Image", use_column_width=True)
|
| 88 |
+
|
| 89 |
+
if st.sidebar.button("Classify"):
|
| 90 |
+
st.sidebar.text("Classifying...")
|
| 91 |
+
|
| 92 |
+
image = Image.open(my_upload)
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
classification_result = image_classifier(image)
|
| 96 |
+
top_prediction = classification_result[0]
|
| 97 |
+
label = top_prediction['label']
|
| 98 |
+
score = top_prediction['score']
|
| 99 |
+
st.sidebar.text("Top Prediction:")
|
| 100 |
+
st.sidebar.text(f"Label: {label}, Score: {score:.3f}")
|
| 101 |
+
except Exception as e:
|
| 102 |
+
st.error(f"Error during classification: {str(e)}")
|
| 103 |
+
|
| 104 |
+
if my_upload.size > MAX_FILE_SIZE:
|
| 105 |
+
st.error("The uploaded file is too large. Please upload an image smaller than 5MB.")
|
| 106 |
+
else:
|
| 107 |
+
fix_image(upload=my_upload)
|
| 108 |
+
else:
|
| 109 |
+
fix_image("jalebi.jpg")
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# Recipe generation based on selected item
|
| 114 |
+
st.write("## Recipe Generation")
|
| 115 |
+
|
| 116 |
+
selected_item = st.selectbox("Select a food item", [entry["title"] for entry in dataset])
|
| 117 |
+
if st.button("Generate Recipe"):
|
| 118 |
+
generated_recipe = generate_recipe(selected_item, max_length=200)
|
| 119 |
+
st.write(f"Recipe for {selected_item}:\n{generated_recipe}")
|
| 120 |
+
|
| 121 |
+
# Add some descriptions and instructions
|
| 122 |
+
st.sidebar.markdown("### Instructions")
|
| 123 |
+
st.sidebar.markdown("1. Upload an image.")
|
| 124 |
+
st.sidebar.markdown("2. Click the 'Classify' button to get the classification results.")
|
| 125 |
+
st.sidebar.markdown("3. Select a food item to generate a recipe.")
|
| 126 |
+
st.sidebar.markdown("4. Click the 'Generate Recipe' button to get the recipe.")
|
| 127 |
+
|
| 128 |
+
# Display a placeholder for the main content
|
| 129 |
+
st.write("Please upload an image and use the sidebar to classify it and generate a recipe.")
|