anirudh-gk commited on
Commit
fae00b5
·
1 Parent(s): 7e64dce

Adding app.py which contains the code written by the FLL holographic designers team

Browse files
Files changed (2) hide show
  1. app.py +176 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import base64
4
+
5
+ from langchain.chat_models import ChatOpenAI
6
+ from langchain.schema import AIMessage, HumanMessage, SystemMessage
7
+ import openai
8
+ import gradio as gr
9
+
10
+
11
+ # This was copy and pasted from https://www.gradio.app/guides/creating-a-chatbot-fast
12
+
13
+
14
+
15
+
16
+
17
+ def predict(message, history, ingredients, servings, appliances, caloriesmin, caloriesmax, detected_ingredients, types_of_food, different_diets, cultures, additional_ingredients, pastry_or_not, openai_api_key):
18
+ llm = ChatOpenAI(temperature=1.0, openai_api_key=openai_api_key, model='gpt-3.5-turbo-0613', )
19
+
20
+ history_langchain_format = []
21
+ history_langchain_format.append(
22
+ SystemMessage(content=f"""
23
+ Imagine that you are robust yet friendly chef that help new cooks cook.
24
+ The cook that you are going help has {ingredients}, {detected_ingredients}, {additional_ingredients} and {appliances}.
25
+ I am cooking for {servings} people. They want to cook this type of food : {pastry_or_not}.
26
+ The number of calories in the dish should be in the range from {caloriesmin} to {caloriesmax}.
27
+ The only categories of food it should use should be: {types_of_food}.
28
+ The user is on the following diets: {different_diets}.The dish must be from this culture: {cultures}. Give a small amount of background knowledge/where this dish came from.
29
+ Recommend a good recipe when Rec Plz is typed that uses the ingredients, appliances on hand
30
+ but is also easy for beginners to cook.
31
+ """))
32
+
33
+
34
+
35
+ # this converts the history to langchain format
36
+
37
+ for human, ai in history:
38
+ history_langchain_format.append(HumanMessage(content=human))
39
+ history_langchain_format.append(AIMessage(content=ai))
40
+
41
+ # this converts the message to langchain format
42
+ history_langchain_format.append(HumanMessage(content=message))
43
+
44
+ # Calling chat gpt
45
+ gpt_response = llm(history_langchain_format)
46
+
47
+ return gpt_response.content
48
+
49
+
50
+
51
+ # def echo_image(input_image_filepath):
52
+ # #we copy and pasted this code from replicate
53
+ # print(input_image_filepath)
54
+
55
+ # output = replicate.run(
56
+ # "kiransom/fll_detic:161277b70ee6ea38847ba2e1c56523dcdf77143ac029d52a795327c70404846e", # Model ID
57
+ # input={
58
+ # "image": open(input_image_filepath, "rb"),
59
+
60
+ # }
61
+ # )
62
+ # print(output["output_path"])
63
+ # print(output["predictions_set"])
64
+ # return(output["output_path"], output["predictions_set"])
65
+
66
+ # Function to encode the image
67
+ # Getting the base64 string
68
+ # base64 is compact encoding of the bytes of the image
69
+ def encode_image(image_path):
70
+ with open(image_path, "rb") as image_file:
71
+ return base64.b64encode(image_file.read()).decode('utf-8')
72
+
73
+
74
+ def process_image(image_path, openai_api_key):
75
+ base64_image = encode_image(image_path)
76
+ question = "This is an image of ingredients available for cooking. Please list all the ingredients and approximate quantity of each ingredient in a numbered list."
77
+
78
+ headers = {
79
+ "Content-Type": "application/json",
80
+ "Authorization": f"Bearer {openai_api_key}"
81
+ }
82
+
83
+ payload = {
84
+ "model": "gpt-4-vision-preview",
85
+ "messages": [
86
+ {
87
+ "role": "user",
88
+ "content": [
89
+ {
90
+ "type": "text",
91
+ "text": question
92
+ },
93
+ {
94
+ "type": "image_url",
95
+ "image_url": {
96
+ "url": f"data:image/jpeg;base64,{base64_image}"
97
+ }
98
+ }
99
+ ]
100
+ }
101
+ ],
102
+ "max_tokens": 300
103
+ }
104
+
105
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
106
+ print(response)
107
+
108
+ return response.json()["choices"][0]["message"]["content"]
109
+
110
+
111
+
112
+ with gr.Blocks() as demo:
113
+
114
+
115
+ with gr.Row():
116
+ openai_api_key = gr.Textbox(placeholder="Type in this box first.", label="Please enter your OpenAI key. If you do not have a key, please visit this site: https://platform.openai.com/signup/ . This textbox may duplicate. If so, DO NOT click the second textbox.")
117
+
118
+
119
+ with gr.Row():
120
+ detected_ingredients = gr.Text()
121
+ gr.Interface(fn=process_image,
122
+ inputs=[gr.Image(width=400, height=400, type="filepath"), openai_api_key],
123
+ outputs=detected_ingredients
124
+ )
125
+
126
+ with gr.Row():
127
+ ingredients = gr.CheckboxGroup(choices=["Salt", "Pepper", "Flour","Oil", "Pasta", "Rice", ], label="Common ingredients")
128
+
129
+
130
+ with gr.Row():
131
+ appliances = gr.CheckboxGroup(choices=["stove", "blender", "oven", "pots", "air fryer", "pressure cooker", "microwave"], label="Appliances")
132
+
133
+ with gr.Row():
134
+ servings = gr.Slider(1, 20, step=1, label="Servings")
135
+
136
+ with gr.Row():
137
+ cultures = gr.Radio(choices=["Italian", "French", "American", "Japanese", "Korean", "Chinese", "Jewish", "German", "Indian"], label="Cultures")
138
+
139
+
140
+ with gr.Row():
141
+
142
+ caloriesmin = gr.Slider(50, 2000, value=100, step=25, label="Calories Min")
143
+ caloriesmax = gr.Slider(100, 2000, value=1500, step=25, label="Calories Max")
144
+
145
+ with gr.Row():
146
+ types_of_food = gr.CheckboxGroup(choices=["fruits", "vegetables", "grains", "protein", "starch-rich food", "dairy", "fat",], label="Types of food you would like to include in your diet")
147
+
148
+ with gr.Row():
149
+ additional_ingredients = gr.Textbox(lines=2, label="Addtional Ingredients", placeholder="Please add any addtional ingredients the model missed.")
150
+
151
+ with gr.Row():
152
+ different_diets = gr.CheckboxGroup(choices=["Ketogenic Diet", "Meditarranean Diet", "Paleo Diet", "Whole30 Diet", "Vegan Diet", "Vegetarian Diet", "Raw Food Diet", "Ayurvedic Diet", "Carb Cycling", "Macrobiotic Diet"], label="Diets", info="Other - if you have another diet, please just enter the foods you are supposed to avoid into the Dietary Restrictions textbox and do not select this checkbox.")
153
+
154
+ with gr.Row():
155
+ pastry_or_not = gr.Radio(choices=["Pastry", "Other"], label="Pastry")
156
+
157
+ with gr.Row():
158
+ gr.ChatInterface(fn=predict,
159
+ additional_inputs=[
160
+ ingredients,
161
+ servings,
162
+ appliances,
163
+ caloriesmin,
164
+ caloriesmax,
165
+ detected_ingredients,
166
+ types_of_food,
167
+ different_diets,
168
+ cultures,
169
+ additional_ingredients,
170
+ pastry_or_not,
171
+ openai_api_key
172
+ ],
173
+ )
174
+
175
+
176
+ demo.launch(share=False)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ openai
3
+ langchain