lgaleana commited on
Commit
feda34f
1 Parent(s): d0e06f7

E2E generative image

Browse files
ai/__init__.py CHANGED
@@ -0,0 +1 @@
 
 
1
+ from . import image, llm
ai_tasks/headlines_for_ai_images.py CHANGED
@@ -24,24 +24,18 @@ Generate a JSON in the following format:
24
  "ad_dimension":
25
  "dimension_to_map":
26
  "headline":
27
- "prompt":
28
  }}
29
  ```
30
  """
31
 
32
 
33
- def generate_headline_and_prompt(summary: str, dimensions: str) -> Dict[str, str]:
34
- print_system("Generating headline for website...")
35
- instructions = PROMPT.format(
36
- summary=summary,
37
- dimensions=dimensions,
38
- )
39
- messages = [{"role": "user", "content": instructions}]
40
- return _parse_output(llm.next(messages, temperature=0))
41
 
42
 
43
- def _parse_output(assistant_message: str) -> Dict[str, str]:
44
- # Might throw
45
- match = re.search("({.*})", assistant_message, re.DOTALL)
46
- json_request = match.group(0) # type: ignore
47
- return json.loads(json_request)
 
24
  "ad_dimension":
25
  "dimension_to_map":
26
  "headline":
27
+ "ai_prompt":
28
  }}
29
  ```
30
  """
31
 
32
 
33
+ def generate_headline_and_prompt(summary: str, dimensions: str) -> str:
34
+ return _generate_headline_and_prompt(PROMPT, summary=summary, dimensions=dimensions)
 
 
 
 
 
 
35
 
36
 
37
+ def _generate_headline_and_prompt(prompt: str, **kwargs) -> str:
38
+ print_system("Generating headline for website...")
39
+ instructions = prompt.format(**kwargs)
40
+ messages = [{"role": "user", "content": instructions}]
41
+ return llm.next(messages, temperature=0)
ai_tasks/headlines_for_images.py CHANGED
@@ -26,7 +26,7 @@ Use the following format.
26
  ```
27
  {{
28
  "reason": Why the image was chosen
29
- "url":
30
  "headline"":
31
  "image_dimensions": The dimensions of the original image
32
  }}
 
26
  ```
27
  {{
28
  "reason": Why the image was chosen
29
+ "image_url":
30
  "headline"":
31
  "image_dimensions": The dimensions of the original image
32
  }}
gradio_app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
 
 
3
  import ai_tasks
4
  import code_tasks
5
  import custom_code
@@ -31,7 +32,16 @@ def get_images_analysis(images):
31
  return custom_code.image_analysis.analyze_images(eval(images))
32
 
33
 
34
- def summarize_text(prompt, url, dimensions, text, images, image_infos, summary):
 
 
 
 
 
 
 
 
 
35
  return ai_tasks.text_summary._summarize_text(
36
  prompt,
37
  url=url,
@@ -40,11 +50,23 @@ def summarize_text(prompt, url, dimensions, text, images, image_infos, summary):
40
  images=images,
41
  image_infos=image_infos,
42
  summary=summary,
 
43
  )
44
 
45
 
46
- def get_headline_for_image(prompt, url, dimensions, text, images, image_infos, summary):
47
- return ai_tasks.headlines_for_images._get_headline_for_image(
 
 
 
 
 
 
 
 
 
 
 
48
  prompt,
49
  url=url,
50
  dimensions=dimensions,
@@ -52,13 +74,45 @@ def get_headline_for_image(prompt, url, dimensions, text, images, image_infos, s
52
  images=images,
53
  image_infos=image_infos,
54
  summary=summary,
 
55
  )
 
56
 
57
 
58
- def set_image(headline):
 
 
 
 
 
 
 
 
 
59
  import json
60
 
61
- return json.loads(headline)["url"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
 
64
  with gr.Blocks() as demo:
@@ -127,7 +181,7 @@ with gr.Blocks() as demo:
127
  with gr.Column():
128
  summary_prompt = gr.Textbox(
129
  ai_tasks.text_summary.PROMPT,
130
- label="Instructions",
131
  interactive=True,
132
  )
133
  with gr.Column():
@@ -141,7 +195,7 @@ with gr.Blocks() as demo:
141
  with gr.Column():
142
  headline_prompt = gr.Textbox(
143
  ai_tasks.headlines_for_images.PROMPT,
144
- label="Instructions",
145
  interactive=True,
146
  lines=20,
147
  )
@@ -152,9 +206,58 @@ with gr.Blocks() as demo:
152
  max_lines=10,
153
  interactive=False,
154
  )
155
- headline_image = gr.Image()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
- vars_ = [url, dimensions, text, images, image_infos, summary]
 
 
 
 
 
 
 
 
158
 
159
  execute.click(
160
  get_text_and_images_from_url, inputs=[url], outputs=[text, images]
@@ -169,11 +272,19 @@ with gr.Blocks() as demo:
169
  ).success(
170
  get_headline_for_image,
171
  inputs=[headline_prompt] + vars_, # type: ignore
172
- outputs=[headline],
 
 
 
 
 
 
 
 
 
 
173
  ).success(
174
- set_image,
175
- inputs=[headline],
176
- outputs=[headline_image],
177
  )
178
 
179
  demo.launch()
 
1
  import gradio as gr
2
 
3
+ import ai
4
  import ai_tasks
5
  import code_tasks
6
  import custom_code
 
32
  return custom_code.image_analysis.analyze_images(eval(images))
33
 
34
 
35
+ def summarize_text(
36
+ prompt,
37
+ url,
38
+ dimensions,
39
+ text,
40
+ images,
41
+ image_infos,
42
+ summary,
43
+ headline,
44
+ ):
45
  return ai_tasks.text_summary._summarize_text(
46
  prompt,
47
  url=url,
 
50
  images=images,
51
  image_infos=image_infos,
52
  summary=summary,
53
+ headline=headline,
54
  )
55
 
56
 
57
+ def get_headline_for_image(
58
+ prompt,
59
+ url,
60
+ dimensions,
61
+ text,
62
+ images,
63
+ image_infos,
64
+ summary,
65
+ headline,
66
+ ):
67
+ import json
68
+
69
+ output = ai_tasks.headlines_for_images._get_headline_for_image(
70
  prompt,
71
  url=url,
72
  dimensions=dimensions,
 
74
  images=images,
75
  image_infos=image_infos,
76
  summary=summary,
77
+ headline=headline,
78
  )
79
+ return output, json.loads(output)["image_url"]
80
 
81
 
82
+ def get_headline_and_prompt(
83
+ prompt,
84
+ url,
85
+ dimensions,
86
+ text,
87
+ images,
88
+ image_infos,
89
+ summary,
90
+ headline,
91
+ ):
92
  import json
93
 
94
+ output = ai_tasks.headlines_for_ai_images._generate_headline_and_prompt(
95
+ prompt,
96
+ url=url,
97
+ dimensions=dimensions,
98
+ text=text,
99
+ images=images,
100
+ image_infos=image_infos,
101
+ summary=summary,
102
+ headline=headline,
103
+ )
104
+ output_dict = json.loads(output)
105
+ return (
106
+ output,
107
+ output_dict["ai_prompt"],
108
+ output_dict["ai_prompt"],
109
+ output_dict["dimension_to_map"],
110
+ output_dict["dimension_to_map"],
111
+ )
112
+
113
+
114
+ def generate_image(prompt, dimensions):
115
+ return ai.image.urls(prompt, 1, dimensions)[0]
116
 
117
 
118
  with gr.Blocks() as demo:
 
181
  with gr.Column():
182
  summary_prompt = gr.Textbox(
183
  ai_tasks.text_summary.PROMPT,
184
+ label="Instructions:",
185
  interactive=True,
186
  )
187
  with gr.Column():
 
195
  with gr.Column():
196
  headline_prompt = gr.Textbox(
197
  ai_tasks.headlines_for_images.PROMPT,
198
+ label="Instructions:",
199
  interactive=True,
200
  lines=20,
201
  )
 
206
  max_lines=10,
207
  interactive=False,
208
  )
209
+ headline_image = gr.Image(interactive=False)
210
+
211
+ with gr.Box():
212
+ gr.Markdown("AI task: generate headline and prompt for image")
213
+ with gr.Row():
214
+ with gr.Column():
215
+ ai_prompt_prompt = gr.Textbox(
216
+ ai_tasks.headlines_for_ai_images.PROMPT,
217
+ label="Instructions:",
218
+ interactive=True,
219
+ )
220
+ with gr.Column():
221
+ headline_and_prompt = gr.Textbox(
222
+ label="Output: {headline_prompt}",
223
+ lines=20,
224
+ max_lines=20,
225
+ interactive=False,
226
+ )
227
+ dimension_to_map = gr.Textbox(
228
+ label="Output: {dimension_to_map}",
229
+ interactive=False,
230
+ )
231
+ ai_prompt = gr.Textbox(
232
+ label="Output: {ai_prompt}",
233
+ interactive=False,
234
+ )
235
+
236
+ with gr.Box():
237
+ gr.Markdown("AI task: generate image")
238
+ with gr.Row():
239
+ with gr.Column():
240
+ with gr.Box():
241
+ ai_image_prompt = gr.Textbox(
242
+ label="Instructions: {ai_prompt}",
243
+ interactive=False,
244
+ )
245
+ image_dimensions = gr.Textbox(
246
+ label="Input: {dimension_to_map}",
247
+ interactive=False,
248
+ )
249
+ with gr.Column():
250
+ ai_image = gr.Image()
251
 
252
+ vars_ = [
253
+ url,
254
+ dimensions,
255
+ text,
256
+ images,
257
+ image_infos,
258
+ summary,
259
+ headline,
260
+ ]
261
 
262
  execute.click(
263
  get_text_and_images_from_url, inputs=[url], outputs=[text, images]
 
272
  ).success(
273
  get_headline_for_image,
274
  inputs=[headline_prompt] + vars_, # type: ignore
275
+ outputs=[headline, headline_image],
276
+ ).success(
277
+ get_headline_and_prompt,
278
+ inputs=[ai_prompt_prompt] + vars_, # type: ignore
279
+ outputs=[
280
+ headline_and_prompt,
281
+ ai_prompt,
282
+ ai_image_prompt,
283
+ dimension_to_map,
284
+ image_dimensions,
285
+ ],
286
  ).success(
287
+ generate_image, inputs=[ai_image_prompt, image_dimensions], outputs=[ai_image]
 
 
288
  )
289
 
290
  demo.launch()