lgaleana commited on
Commit
788a6f6
β€’
1 Parent(s): 2704650

Gradio app e2e

Browse files
ai_tasks/__init__.py CHANGED
@@ -1 +1 @@
1
-
 
1
+ from . import headlines_for_ai_images, headlines_for_images, text_summary
ai_tasks/{headlines_ai_images.py β†’ headlines_for_ai_images.py} RENAMED
File without changes
ai_tasks/headlines_for_images.py CHANGED
@@ -16,11 +16,11 @@ Summary of the website:
16
  {summary}
17
 
18
  Image urls, with labels and dimensions:
19
- {images}
20
 
21
  Dimensions for the ad: {dimensions}.
22
 
23
- Use the folliowing format.
24
 
25
  Why the image was chosen:
26
  Url:
@@ -30,12 +30,12 @@ Original dimensions:
30
 
31
 
32
  def get_headline_for_image(
33
- summary: str, dimensions: str, image_labels: List[Dict]
34
  ) -> str:
35
  print_system("Generating ad from images...")
36
  instructions = PROMPT.format(
37
  summary=summary,
38
- images=json.dumps(image_labels, indent=2),
39
  dimensions=dimensions,
40
  )
41
  messages = [{"role": "user", "content": instructions}]
 
16
  {summary}
17
 
18
  Image urls, with labels and dimensions:
19
+ {image_infos}
20
 
21
  Dimensions for the ad: {dimensions}.
22
 
23
+ Use the following format.
24
 
25
  Why the image was chosen:
26
  Url:
 
30
 
31
 
32
  def get_headline_for_image(
33
+ summary: str, dimensions: str, image_infos: List[Dict]
34
  ) -> str:
35
  print_system("Generating ad from images...")
36
  instructions = PROMPT.format(
37
  summary=summary,
38
+ image_infos=json.dumps(image_infos, indent=2),
39
  dimensions=dimensions,
40
  )
41
  messages = [{"role": "user", "content": instructions}]
code_tasks/__init__.py CHANGED
@@ -0,0 +1 @@
 
 
1
+ from . import images_in_url, text_in_url
control_flow/main.py CHANGED
@@ -1,13 +1,10 @@
1
- from typing import Dict
2
- import json
3
-
4
  from ai import image
 
5
  from ai_tasks.headlines_for_images import get_headline_for_image
6
- from ai_tasks.headlines_ai_images import generate_headline_and_prompt
7
  from ai_tasks.text_summary import summarize_text
8
- from code_tasks.custom import get_image_info, run_parallel_jobs
9
  from code_tasks.images_in_url import get_images_from_url
10
  from code_tasks.text_in_url import get_text_from_url
 
11
  from utils.io import print_assistant, print_system, user_input
12
 
13
 
@@ -28,19 +25,20 @@ def run():
28
  print_system("Getting URL data...")
29
  text = get_text_from_url(url)
30
  images = get_images_from_url(url)
31
- image_info = run_parallel_jobs(get_image_info, images, max=20)
32
- print_system(json.dumps(image_info, indent=2))
33
 
34
  # AI tasks
35
  summary = summarize_text(text)
36
  print_assistant(summary)
37
  # Pick an image and generate a headline
38
- headlines = get_headline_for_image(summary, dimensions, image_info)
39
- print_assistant(headlines)
40
  # Generate a headline and an image
41
  headline_prompt = generate_headline_and_prompt(summary, dimensions)
42
  print_system("Generating AI images...")
43
- ai_image = image.urls(headline_prompt["prompt"], size=headline_prompt["dimension_to_map"])[0]
 
 
44
  print_system(f"Prompt: {headline_prompt['prompt']}")
45
  print_assistant(headline_prompt["ad_dimension"])
46
  print_assistant(headline_prompt["headline"])
 
 
 
 
1
  from ai import image
2
+ from ai_tasks.headlines_for_ai_images import generate_headline_and_prompt
3
  from ai_tasks.headlines_for_images import get_headline_for_image
 
4
  from ai_tasks.text_summary import summarize_text
 
5
  from code_tasks.images_in_url import get_images_from_url
6
  from code_tasks.text_in_url import get_text_from_url
7
+ from custom_code.image_analysis import analyze_images
8
  from utils.io import print_assistant, print_system, user_input
9
 
10
 
 
25
  print_system("Getting URL data...")
26
  text = get_text_from_url(url)
27
  images = get_images_from_url(url)
28
+ image_infos = analyze_images(images)
 
29
 
30
  # AI tasks
31
  summary = summarize_text(text)
32
  print_assistant(summary)
33
  # Pick an image and generate a headline
34
+ headline = get_headline_for_image(summary, dimensions, image_infos)
35
+ print_assistant(headline)
36
  # Generate a headline and an image
37
  headline_prompt = generate_headline_and_prompt(summary, dimensions)
38
  print_system("Generating AI images...")
39
+ ai_image = image.urls(
40
+ headline_prompt["prompt"], size=headline_prompt["dimension_to_map"]
41
+ )[0]
42
  print_system(f"Prompt: {headline_prompt['prompt']}")
43
  print_assistant(headline_prompt["ad_dimension"])
44
  print_assistant(headline_prompt["headline"])
custom_code/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from . import image_analysis
code_tasks/custom.py β†’ custom_code/image_analysis.py RENAMED
@@ -15,21 +15,22 @@ from code_tasks.image_dimensions import get_image_dimensions
15
  load_dotenv()
16
 
17
 
18
- def get_image_info(url: str) -> Dict:
19
- client = vision.ImageAnnotatorClient()
20
- image = vision.Image()
21
- image.source.image_uri = url # type: ignore
22
-
23
- response = client.label_detection(image=image) # type: ignore
24
- labels = [label.description for label in response.label_annotations]
25
- dimensions = get_image_dimensions(url)
26
- return {
27
- "url": url,
28
- "labels": ", ".join(labels),
29
- "dimensions": f"{dimensions[0]}x{dimensions[1]}",
30
- }
31
-
32
-
33
- def run_parallel_jobs(job, inputs, max: int = 100) -> List[Dict]:
 
34
  with ThreadPoolExecutor() as executor:
35
- return list(executor.map(job, inputs[:max]))
 
15
  load_dotenv()
16
 
17
 
18
+ def analyze_images(images) -> List[Dict]:
19
+ max = 20
20
+
21
+ def get_image_info(image_url: str) -> Dict:
22
+ client = vision.ImageAnnotatorClient()
23
+ image = vision.Image()
24
+ image.source.image_uri = image_url # type: ignore
25
+
26
+ response = client.label_detection(image=image) # type: ignore
27
+ labels = [label.description for label in response.label_annotations]
28
+ dimensions = get_image_dimensions(image_url)
29
+ return {
30
+ "url": image_url,
31
+ "labels": ", ".join(labels),
32
+ "dimensions": f"{dimensions[0]}x{dimensions[1]}",
33
+ }
34
+
35
  with ThreadPoolExecutor() as executor:
36
+ return list(executor.map(get_image_info, images[:max]))
gradio_app.py CHANGED
@@ -1,23 +1,135 @@
1
  import gradio as gr
2
- import random
3
- import time
4
- from typing import List
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
 
7
  with gr.Blocks() as demo:
8
- chatbot = gr.Chatbot()
9
- msg = gr.Textbox()
10
- clear = gr.Button("Clear")
11
-
12
- def respond(message: str, chat_history: List):
13
- print(type(message))
14
- print(type(chat_history))
15
- bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
16
- chat_history.append((message, bot_message))
17
- time.sleep(1)
18
- return "", chat_history
19
-
20
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
21
- clear.click(lambda: None, None, chatbot, queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  demo.launch()
 
1
  import gradio as gr
2
+
3
+ import ai_tasks
4
+ import code_tasks
5
+ import custom_code
6
+ from control_flow.main import _run
7
+
8
+
9
+ def open__get_text_from_url() -> str:
10
+ with open("code_tasks/text_in_url.py") as f:
11
+ return f.read()
12
+
13
+
14
+ def open__get_images_from_url() -> str:
15
+ with open("code_tasks/images_in_url.py") as f:
16
+ return f.read()
17
+
18
+
19
+ def open__get_image_infos() -> str:
20
+ with open("custom_code/image_analysis.py") as f:
21
+ return f.read()
22
+
23
+
24
+ def get_text_and_images_from_url(url):
25
+ return (
26
+ code_tasks.text_in_url.get_text_from_url(url),
27
+ code_tasks.images_in_url.get_images_from_url(url),
28
+ )
29
+
30
+
31
+ def get_images_analysis(images):
32
+ return custom_code.image_analysis.analyze_images(eval(images))
33
 
34
 
35
  with gr.Blocks() as demo:
36
+ gr.Markdown(
37
+ """
38
+ ## Ad Generator
39
+ Enter an url and the dimensions for an image (eg, 300x600) and get the image and headline for an ad."""
40
+ )
41
+
42
+ url = gr.Textbox(label="Input: {url}")
43
+ dimensions = gr.Textbox(label="Input: {dimensions}")
44
+ execute = gr.Button("Run")
45
+
46
+ with gr.Box():
47
+ gr.Markdown("Code task")
48
+ with gr.Row():
49
+ with gr.Column():
50
+ gr.Textbox(
51
+ "write a python function that given an url returns all text in the website",
52
+ label="ChatGPT-4 prompt",
53
+ )
54
+ with gr.Accordion("Input: {url}", open=False):
55
+ gr.Code(open__get_text_from_url(), "python")
56
+ with gr.Column():
57
+ text = gr.Textbox(
58
+ label="Output: {text}", lines=10, max_lines=10, interactive=False
59
+ )
60
+
61
+ with gr.Box():
62
+ gr.Markdown("Code task")
63
+ with gr.Row():
64
+ with gr.Column():
65
+ gr.Textbox(
66
+ "write a python function that given an url returns all images in the website",
67
+ label="ChatGPT-4 prompt",
68
+ )
69
+ with gr.Accordion("Input: {url}", open=False):
70
+ gr.Code(open__get_images_from_url(), "python")
71
+ with gr.Column():
72
+ images = gr.Textbox(
73
+ label="Output: {images}", lines=10, max_lines=10, interactive=False
74
+ )
75
+
76
+ with gr.Box():
77
+ gr.Markdown("Custom code: analyze images with Google Vision")
78
+ with gr.Row():
79
+ with gr.Column():
80
+ with gr.Accordion("Input: {images}", open=False):
81
+ gr.Code(open__get_image_infos(), "python")
82
+ with gr.Column():
83
+ image_infos = gr.Textbox(
84
+ label="Output: {image_infos}",
85
+ lines=10,
86
+ max_lines=10,
87
+ interactive=False,
88
+ )
89
+
90
+ with gr.Box():
91
+ gr.Markdown("AI task: summarize text")
92
+ with gr.Row():
93
+ with gr.Column():
94
+ gr.Textbox(
95
+ ai_tasks.text_summary.PROMPT,
96
+ label="Instructions",
97
+ interactive=True,
98
+ )
99
+ with gr.Column():
100
+ summary = gr.Textbox(
101
+ label="Output: {summary}", lines=10, max_lines=10, interactive=False
102
+ )
103
+
104
+ with gr.Box():
105
+ gr.Markdown("AI task: generate headline for image")
106
+ with gr.Row():
107
+ with gr.Column():
108
+ gr.Textbox(
109
+ ai_tasks.headlines_for_images.PROMPT,
110
+ label="Instructions",
111
+ interactive=True,
112
+ )
113
+ with gr.Column():
114
+ headline = gr.Textbox(
115
+ label="Output: {headline}",
116
+ lines=20,
117
+ max_lines=10,
118
+ interactive=False,
119
+ )
120
+
121
+ execute.click(
122
+ get_text_and_images_from_url, inputs=[url], outputs=[text, images]
123
+ ).success(
124
+ get_images_analysis,
125
+ inputs=[images],
126
+ outputs=[image_infos],
127
+ ).success(
128
+ ai_tasks.text_summary.summarize_text, inputs=[text], outputs=[summary]
129
+ ).success(
130
+ ai_tasks.headlines_for_images.get_headline_for_image,
131
+ inputs=[summary, dimensions, image_infos],
132
+ outputs=[headline],
133
+ )
134
 
135
  demo.launch()