SumanthKarnati commited on
Commit
aa1545c
·
1 Parent(s): f2b30ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -2
app.py CHANGED
@@ -1,3 +1,134 @@
1
- import gradio as gr
 
 
 
 
 
 
 
2
 
3
- gr.Interface.load("models/SumanthKarnati/Image2Ingredients").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import nltk
2
+ from transformers import VisionEncoderDecoderModel, AutoTokenizer, ViTImageProcessor, GPT2LMHeadModel, GPT2Tokenizer
3
+ import torch
4
+ from PIL import Image
5
+ import streamlit as st
6
+ from nltk.corpus import stopwords
7
+ import os
8
+ from io import BytesIO
9
 
10
+ # os.system('pip install --upgrade transformers')
11
+ # os.system('pip install nltk')
12
+ nltk.download('stopwords')
13
+
14
+ # Load the pre-trained model
15
+ model = VisionEncoderDecoderModel.from_pretrained(
16
+ "SumanthKarnati/Image2Ingredients")
17
+ model.eval()
18
+
19
+ # Define the feature extractor
20
+ feature_extractor = ViTImageProcessor.from_pretrained(
21
+ 'nlpconnect/vit-gpt2-image-captioning')
22
+
23
+ # Load the tokenizer
24
+ tokenizer = AutoTokenizer.from_pretrained(
25
+ 'nlpconnect/vit-gpt2-image-captioning')
26
+
27
+ # Load GPT-2 model and tokenizer
28
+ gpt2_model = GPT2LMHeadModel.from_pretrained('gpt2')
29
+ gpt2_tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
30
+
31
+ # Device configuration
32
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
33
+
34
+ # Transfer the model to GPU if available
35
+ model = model.to(device)
36
+
37
+ # Set prediction arguments
38
+ max_length = 16
39
+ num_beams = 4
40
+ gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
41
+
42
+ # Function to predict ingredients from images
43
+
44
+
45
+ def predict_step(image_files, model, feature_extractor, tokenizer, device, gen_kwargs):
46
+ images = []
47
+ for image_file in image_files:
48
+ if image_file is not None:
49
+ # Create a BytesIO object from the UploadedFile (image_file)
50
+ byte_stream = BytesIO(image_file.getvalue())
51
+ image = Image.open(byte_stream)
52
+ if image.mode != "RGB":
53
+ image = image.convert(mode="RGB")
54
+ images.append(image)
55
+
56
+ if not images:
57
+ return None
58
+
59
+ inputs = feature_extractor(images=images, return_tensors="pt")
60
+ inputs.to(device)
61
+ output_ids = model.generate(inputs["pixel_values"], **gen_kwargs)
62
+
63
+ preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
64
+ preds = [pred.strip() for pred in preds]
65
+ return preds
66
+
67
+
68
+ # Get the list of English stop words
69
+ stop_words = set(stopwords.words('english'))
70
+
71
+ # Function to remove stop words from a list of words
72
+
73
+
74
+ def remove_stop_words(word_list):
75
+ return [word for word in word_list if word not in stop_words]
76
+
77
+ # Streamlit app code
78
+
79
+
80
+ def main():
81
+ st.title("Image2Nutrients: Food Ingredient Recognition")
82
+ st.write("Upload an image of your food to recognize the ingredients!")
83
+
84
+ # File upload
85
+ uploaded_file = st.file_uploader(
86
+ "Choose an image", type=["jpg", "jpeg", "png"])
87
+
88
+ if uploaded_file is not None:
89
+ # Display the uploaded image
90
+ image = Image.open(uploaded_file)
91
+ st.image(image, caption="Uploaded Image", use_column_width=True)
92
+
93
+ # Perform ingredient recognition
94
+ preds = predict_step([uploaded_file], model,
95
+ feature_extractor, tokenizer, device, gen_kwargs)
96
+
97
+ preds = preds[0].split('-')
98
+ # remove numbers
99
+ preds = [x for x in preds if not any(c.isdigit() for c in x)]
100
+ # remove empty strings
101
+ preds = list(filter(None, preds))
102
+ # remove duplicates
103
+
104
+ preds = list(dict.fromkeys(preds))
105
+
106
+ preds = remove_stop_words(preds)
107
+
108
+ # Display the recognized ingredients
109
+ st.subheader("Recognized Ingredients:")
110
+ for ingredient in preds:
111
+ st.write(ingredient)
112
+
113
+ preds_str = ', '.join(preds)
114
+
115
+ # Prepare the prompt
116
+ prompt = f"You are a knowledgeable assistant that provides nutritional advice based on a list of ingredients. The identified ingredients are: {preds_str}. Note that some ingredients may not make sense, so use the ones that do. Can you provide a nutritional analysis and suggestions for improvement?"
117
+
118
+ # Encode and add special tokens
119
+ input_ids = gpt2_tokenizer.encode(prompt, return_tensors='pt')
120
+
121
+ # Generate a sequence of text
122
+ output = gpt2_model.generate(
123
+ input_ids, max_length=200, temperature=0.7, pad_token_id=gpt2_tokenizer.eos_token_id)
124
+
125
+ # Decode the output
126
+ suggestions = gpt2_tokenizer.decode(
127
+ output[:, input_ids.shape[-1]:][0], clean_up_tokenization_spaces=True)
128
+
129
+ st.subheader("Nutritional Analysis and Suggestions:")
130
+ st.write(suggestions)
131
+
132
+
133
+ if __name__ == "__main__":
134
+ main()