m3hrdadfi commited on
Commit
b329690
1 Parent(s): 179908e

Fix style bugs

Browse files
Files changed (2) hide show
  1. app.py +71 -23
  2. asset/css/style.css +6 -0
app.py CHANGED
@@ -11,6 +11,7 @@ from PIL import (
11
  )
12
 
13
  import re
 
14
  from examples import EXAMPLES
15
  import meta
16
  from utils import (
@@ -31,8 +32,10 @@ class TextGeneration:
31
  'cover with plastic wrap and microwave on high for 5 minutes.',
32
  'remove from the microwave and sprinkle with cheese.',
33
  'return to the microwave for 1 minute or until cheese is melted.',
 
 
 
34
  'return to the microwave for 1 minute or until cheese is melted.',
35
- 'return to the microwave for 1 minute or until cheese is melted.'
36
  ],
37
  'ingredients': [
38
  '1 potato',
@@ -53,6 +56,8 @@ class TextGeneration:
53
  self.generator = None
54
  self.task = "text2text-generation"
55
  self.model_name_or_path = "flax-community/t5-recipe-generation"
 
 
56
  self.h1_font = ImageFont.truetype("asset/fonts/PT_Serif/PTSerif-Bold.ttf", 100)
57
  self.h2_font = ImageFont.truetype("asset/fonts/PT_Serif/PTSerif-Bold.ttf", 50)
58
  self.p_font = ImageFont.truetype("asset/fonts/PT_Serif/PTSerif-Regular.ttf", 30)
@@ -100,7 +105,7 @@ class TextGeneration:
100
  font=self.h1_font,
101
  )
102
  # Ingredients
103
- hs = hs + 200
104
  im_editable.text(
105
  (ws, hs),
106
  "Ingredients",
@@ -108,9 +113,19 @@ class TextGeneration:
108
  font=self.h2_font,
109
  )
110
  hs = hs + 80
 
 
 
 
111
  im_editable.text(
112
  (ws + 10, hs),
113
- "\n".join([f"- {item}" for item in recipe["ingredients"]]),
 
 
 
 
 
 
114
  (61, 61, 70),
115
  font=self.p_font,
116
  )
@@ -123,9 +138,10 @@ class TextGeneration:
123
  font=self.h2_font,
124
  )
125
  hs = hs + 80
 
126
  im_editable.text(
127
  (ws + 10, hs),
128
- "\n".join([f"- {item}" for item in recipe["directions"]]),
129
  (61, 61, 70),
130
  font=self.p_font,
131
  )
@@ -192,8 +208,18 @@ def main():
192
  local_css("asset/css/style.css")
193
 
194
  st.sidebar.image(load_image("asset/images/chef-transformer-transparent.png"), width=310)
195
- st.sidebar.title("Choose your own chef")
196
- chef = st.sidebar.selectbox("Chef", index=0, options=["Chef Scheherazade", "Chef Giovanni"])
 
 
 
 
 
 
 
 
 
 
197
 
198
  st.markdown(meta.HEADER_INFO)
199
  prompts = list(EXAMPLES.keys()) + ["Custom"]
@@ -215,26 +241,48 @@ def main():
215
  if st.button('Get Recipe!'):
216
  entered_items.markdown("**Generate recipe for:** " + items)
217
  with st.spinner("Generating recipe..."):
218
-
219
  gen_kw = chef_top if chef == "Chef Scheherazade" else chef_beam
220
  generated_recipe = generator.generate(items, gen_kw)
221
- recipe_post = generator.generate_frame(generated_recipe)
222
-
223
- col1, col2, col3 = st.beta_columns([1, 6, 1])
224
- with col1:
225
- st.write("")
226
-
227
- with col2:
228
- st.image(
229
- recipe_post,
230
- # width=500,
231
- caption="Your recipe",
232
- use_column_width="auto",
233
- output_format="PNG"
234
- )
235
 
236
- with col3:
237
- st.write("")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
 
240
  if __name__ == '__main__':
 
11
  )
12
 
13
  import re
14
+ import textwrap
15
  from examples import EXAMPLES
16
  import meta
17
  from utils import (
 
32
  'cover with plastic wrap and microwave on high for 5 minutes.',
33
  'remove from the microwave and sprinkle with cheese.',
34
  'return to the microwave for 1 minute or until cheese is melted.',
35
+ 'return to the microwave for 1 minute or until cheese is melted. return to the microwave for 1 minute or until cheese is melted.'
36
+ 'return to the microwave for 1 minute or until cheese is melted.',
37
+ 'return to the microwave for 1 minute or until cheese is melted.',
38
  'return to the microwave for 1 minute or until cheese is melted.',
 
39
  ],
40
  'ingredients': [
41
  '1 potato',
 
56
  self.generator = None
57
  self.task = "text2text-generation"
58
  self.model_name_or_path = "flax-community/t5-recipe-generation"
59
+ self.list_division = 5
60
+ self.point = "-"
61
  self.h1_font = ImageFont.truetype("asset/fonts/PT_Serif/PTSerif-Bold.ttf", 100)
62
  self.h2_font = ImageFont.truetype("asset/fonts/PT_Serif/PTSerif-Bold.ttf", 50)
63
  self.p_font = ImageFont.truetype("asset/fonts/PT_Serif/PTSerif-Regular.ttf", 30)
 
105
  font=self.h1_font,
106
  )
107
  # Ingredients
108
+ hs = hs + 250
109
  im_editable.text(
110
  (ws, hs),
111
  "Ingredients",
 
113
  font=self.h2_font,
114
  )
115
  hs = hs + 80
116
+ ingredients = recipe["ingredients"]
117
+ ingredients_col1 = [textwrap.fill(item, 30) for item in ingredients[:self.list_division]]
118
+ ingredients_col2 = [textwrap.fill(item, 30) for item in ingredients[self.list_division:]]
119
+
120
  im_editable.text(
121
  (ws + 10, hs),
122
+ "\n".join([f"- {item}" for item in ingredients_col1]),
123
+ (61, 61, 70),
124
+ font=self.p_font,
125
+ )
126
+ im_editable.text(
127
+ (ws + 500, hs),
128
+ "\n".join([f"{self.point} {item}" for item in ingredients_col2]),
129
  (61, 61, 70),
130
  font=self.p_font,
131
  )
 
138
  font=self.h2_font,
139
  )
140
  hs = hs + 80
141
+ directions = [textwrap.fill(item, 70).replace("\n", "\n ") for item in recipe["directions"]]
142
  im_editable.text(
143
  (ws + 10, hs),
144
+ "\n".join([f"{self.point} {item}" for item in directions]),
145
  (61, 61, 70),
146
  font=self.p_font,
147
  )
 
208
  local_css("asset/css/style.css")
209
 
210
  st.sidebar.image(load_image("asset/images/chef-transformer-transparent.png"), width=310)
211
+ st.sidebar.title("Welcome to our lovely restaurant, what can I do for you?")
212
+ chef = st.sidebar.selectbox("Choose your chef", index=0, options=["Chef Scheherazade", "Chef Giovanni"])
213
+ is_text = st.sidebar.selectbox(
214
+ label='Recipe',
215
+ options=(True, False),
216
+ help="Will generate your recipe as a text post",
217
+ )
218
+ is_frame = st.sidebar.selectbox(
219
+ label='Recipe for Instagram?',
220
+ options=(True, False),
221
+ help="Will generate your recipe as an Instagram post",
222
+ )
223
 
224
  st.markdown(meta.HEADER_INFO)
225
  prompts = list(EXAMPLES.keys()) + ["Custom"]
 
241
  if st.button('Get Recipe!'):
242
  entered_items.markdown("**Generate recipe for:** " + items)
243
  with st.spinner("Generating recipe..."):
 
244
  gen_kw = chef_top if chef == "Chef Scheherazade" else chef_beam
245
  generated_recipe = generator.generate(items, gen_kw)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
 
247
+ if generated_recipe:
248
+ if is_text:
249
+ title = generated_recipe["title"]
250
+ ingredients = generated_recipe["ingredients"]
251
+ directions = [textwrap.fill(item, 70).replace("\n", "\n ") for item in
252
+ generated_recipe["directions"]]
253
+ st.markdown(
254
+ " ".join([
255
+ f"<h2>{title}</h2>",
256
+ "<h3>Ingredient</h3>",
257
+ "<ul class='ingredients-list'>",
258
+ " ".join([f'<li>{item}</li>' for item in ingredients]),
259
+ "</ul>",
260
+ "<h3>Direction</h3>",
261
+ "<ul class='ingredients-list'>",
262
+ " ".join([f'<li>{item}</li>' for item in directions]),
263
+ "</ul>",
264
+ ]),
265
+ unsafe_allow_html=True
266
+ )
267
+
268
+ if is_frame:
269
+ recipe_post = generator.generate_frame(generated_recipe)
270
+
271
+ col1, col2, col3 = st.beta_columns([1, 6, 1])
272
+ with col1:
273
+ st.write("")
274
+
275
+ with col2:
276
+ st.image(
277
+ recipe_post,
278
+ # width=500,
279
+ caption="Your recipe",
280
+ use_column_width="auto",
281
+ output_format="PNG"
282
+ )
283
+
284
+ with col3:
285
+ st.write("")
286
 
287
 
288
  if __name__ == '__main__':
asset/css/style.css CHANGED
@@ -5,4 +5,10 @@ body {
5
  .fullScreenFrame > div {
6
  display: flex;
7
  justify-content: center;
 
 
 
 
 
 
8
  }
 
5
  .fullScreenFrame > div {
6
  display: flex;
7
  justify-content: center;
8
+ }
9
+
10
+ .ingredients-list {
11
+ columns: 2;
12
+ -webkit-columns: 2;
13
+ -moz-columns: 2;
14
  }