lgaleana commited on
Commit
739e67a
1 Parent(s): 1bc7525

Improve to v0

Browse files
README.md CHANGED
@@ -3,9 +3,12 @@ Give an URL, generate headlines and images for ads.
3
 
4
  # Installation
5
  1. Install python (make sure to add the environment variables and install pip).
6
- a. Windows: https://www.digitalocean.com/community/tutorials/install-python-windows-10#step-1-downloading-the-python-installer.
7
- 2. Test pythong with `python -v`.
8
  3. Download: https://github.com/lgaleana/gen-ads/zipball/master.
 
 
 
9
 
10
  # Execution
11
  1. Open the command line and navigate to the `gen-ads` folder.
@@ -13,12 +16,8 @@ Give an URL, generate headlines and images for ads.
13
  3. Enter an URL and hit enter.
14
  4. Get ad headlines and images for the URL.
15
 
16
- # How does it work:
17
- The program executes the following [AI and code tasks](https://github.com/lgaleana/gen-ads/blob/main/control_flow/main.py), one after the other:
18
- 1. (Code) [get_text_from_url](https://github.com/lgaleana/gen-ads/blob/main/code_tasks/url_text.py#L2): Function generated by ChatGPT-4, that extracts text from an URL.
19
- 2. (AI) [get_headlines](https://github.com/lgaleana/gen-ads/blob/main/ai_tasks/best_headlines.py#L5): From the text, generates headlines.
20
- 3. (AI) [generate_prompt](https://github.com/lgaleana/gen-ads/blob/main/ai_tasks/image_prompt.py#L5): From text, generates a prompt to generate an image.
21
- 4. (AI) [image.urls](https://github.com/lgaleana/gen-ads/blob/main/ai/image.py): Generates AI images.
22
 
23
- # How to tune it:
24
  Update the prompts of the [AI tasks](https://github.com/lgaleana/gen-ads/tree/main/ai_tasks).
 
3
 
4
  # Installation
5
  1. Install python (make sure to add the environment variables and install pip).
6
+ - Windows: https://www.digitalocean.com/community/tutorials/install-python-windows-10#step-1-downloading-the-python-installer.
7
+ 2. Test python with `python -v`.
8
  3. Download: https://github.com/lgaleana/gen-ads/zipball/master.
9
+ 4. Get an OpenAI key.
10
+ 5. Set up a project for Google Vision and download the service key https://cloud.google.com/vision/docs/setup.
11
+ 6. Rename the file .env.example to .env and add your OpenAI key and the path to your Google Vision service key.
12
 
13
  # Execution
14
  1. Open the command line and navigate to the `gen-ads` folder.
 
16
  3. Enter an URL and hit enter.
17
  4. Get ad headlines and images for the URL.
18
 
19
+ # How it works
20
+ The program executes the AI and code tasks in: https://github.com/lgaleana/gen-ads/blob/main/control_flow/main.py
 
 
 
 
21
 
22
+ # How to tune
23
  Update the prompts of the [AI tasks](https://github.com/lgaleana/gen-ads/tree/main/ai_tasks).
ai/image.py CHANGED
@@ -14,6 +14,6 @@ def gen(prompt: str, n: int, size: str) -> Dict[str, Any]:
14
  return openai.Image.create(prompt=prompt, n=n, size=size) # type: ignore
15
 
16
 
17
- def urls(prompt: str, n: int = 4, size: str = "512x512") -> List[str]:
18
  images = gen(prompt, n, size)
19
  return [i["url"] for i in images["data"]] # type: ignore
 
14
  return openai.Image.create(prompt=prompt, n=n, size=size) # type: ignore
15
 
16
 
17
+ def urls(prompt: str, n: int = 1, size: str = "512x512") -> List[str]:
18
  images = gen(prompt, n, size)
19
  return [i["url"] for i in images["data"]] # type: ignore
ai_tasks/best_headlines.py DELETED
@@ -1,17 +0,0 @@
1
- from ai import llm
2
- from utils.io import print_system
3
-
4
-
5
- PROMPT = """
6
- Here is the text from a website:
7
- {text}
8
-
9
- Extract or create the 10 best headlines for an ad about the website.
10
- """
11
-
12
-
13
- def get_headlines(text: str) -> str:
14
- print_system("Generating headlines...")
15
- instructions = PROMPT.format(text=text)
16
- messages = [{"role": "user", "content": instructions}]
17
- return llm.next(messages)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ai_tasks/headlines_ai_images.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import re
3
+ from typing import Dict, List
4
+
5
+ from ai import llm
6
+ from utils.io import print_system
7
+
8
+
9
+ PROMPT = """
10
+ Your goal is to create an ad for each of the following dimensions: {dimensions}.
11
+ You will rely on an AI image generator to create the ads for you.
12
+
13
+ For each of the ad dimensions, create a prompt to be used by the AI image generator.
14
+ Pick a dimension to map to from this list: 256x256, 512x512, 1024x1024.
15
+ Create a headline.
16
+ Consider that the headlines will be edited on top of the generated images.
17
+
18
+ Here is the summary of the website:
19
+ {summary}
20
+
21
+ Generate a JSON in the following format:
22
+ ```
23
+ [
24
+ {{
25
+ "ad_dimension":
26
+ "dimension_to_map":
27
+ "headline":
28
+ "prompt":
29
+ }}
30
+ ]
31
+ ```
32
+ """
33
+
34
+
35
+ def generate_headlines(summary: str, dimensions: List[str]) -> List[Dict[str, str]]:
36
+ print_system("Generating headlines for website...")
37
+ instructions = PROMPT.format(
38
+ summary=summary,
39
+ dimensions=dimensions,
40
+ )
41
+ messages = [{"role": "user", "content": instructions}]
42
+ return _parse_output(llm.next(messages, temperature=0))
43
+
44
+
45
+ def _parse_output(assistant_message: str) -> List[Dict[str, str]]:
46
+ # Might throw
47
+ match = re.search(r"(\[\s*{.*}\s*\])", assistant_message, re.DOTALL)
48
+ json_request = match.group(0) # type: ignore
49
+ return json.loads(json_request)
ai_tasks/headlines_for_images.py CHANGED
@@ -8,7 +8,7 @@ from utils.io import print_system
8
  PROMPT = """
9
  Your goal is to find a set of the best images that can be used as ads for a website.
10
  The ads have to be in certain dimensions. The images can be edited or cut.
11
- So you must consider which images would be better suited to fit those dimensions.
12
  You should consider the content of the website. The content of the images has been labeled.
13
  You will create a headline for each ad.
14
 
@@ -20,17 +20,19 @@ Image urls, with labels and dimensions:
20
 
21
  Dimensions for the ads: {dimensions}
22
 
23
- Use the folliowing format. Each dimension requirement must be represented.
24
 
25
  Why the image was chosen:
26
  Url:
27
  Headline:
28
  Image dimensions:
29
- Target dimensions: An image can be a good match for more than one dimension
30
  """
31
 
32
 
33
- def get_headlines(summary: str, dimensions: List[str], image_labels: List[Dict]) -> str:
 
 
34
  print_system("Generating headlines for images...")
35
  instructions = PROMPT.format(
36
  summary=summary,
 
8
  PROMPT = """
9
  Your goal is to find a set of the best images that can be used as ads for a website.
10
  The ads have to be in certain dimensions. The images can be edited or cut.
11
+ So you must consider which images would be better suited to fit those dimensions after editing.
12
  You should consider the content of the website. The content of the images has been labeled.
13
  You will create a headline for each ad.
14
 
 
20
 
21
  Dimensions for the ads: {dimensions}
22
 
23
+ Use the folliowing format. Each target dimension must be represented.
24
 
25
  Why the image was chosen:
26
  Url:
27
  Headline:
28
  Image dimensions:
29
+ Target dimensions: It can be more than one
30
  """
31
 
32
 
33
+ def get_headlines_for_images(
34
+ summary: str, dimensions: List[str], image_labels: List[Dict]
35
+ ) -> str:
36
  print_system("Generating headlines for images...")
37
  instructions = PROMPT.format(
38
  summary=summary,
ai_tasks/image_prompt.py DELETED
@@ -1,17 +0,0 @@
1
- from ai import llm
2
- from utils.io import print_system
3
-
4
-
5
- PROMPT = """
6
- Here is the text from a website:
7
- {text}
8
-
9
- Generate a prompt to send to a generative AI assistant that will create an image for it.
10
- """
11
-
12
-
13
- def generate_prompt(text: str) -> str:
14
- print_system("Generating prompt for image...")
15
- instructions = PROMPT.format(text=text)
16
- messages = [{"role": "user", "content": instructions}]
17
- return llm.next(messages, temperature=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ai_tasks/text_summary.py CHANGED
@@ -1,6 +1,3 @@
1
- import json
2
- from typing import Dict, List
3
-
4
  from ai import llm
5
  from utils.io import print_system
6
 
@@ -17,4 +14,4 @@ def summarize_text(text: str) -> str:
17
  print_system("Summarizing text...")
18
  instructions = PROMPT.format(text=text)
19
  messages = [{"role": "user", "content": instructions}]
20
- return llm.next(messages)
 
 
 
 
1
  from ai import llm
2
  from utils.io import print_system
3
 
 
14
  print_system("Summarizing text...")
15
  instructions = PROMPT.format(text=text)
16
  messages = [{"role": "user", "content": instructions}]
17
+ return llm.next(messages, temperature=0.4)
code_tasks/custom.py CHANGED
@@ -24,6 +24,6 @@ def get_image_info(url: str) -> Dict:
24
  }
25
 
26
 
27
- def get_info_for_images(urls) -> List[Dict]:
28
  with ThreadPoolExecutor() as executor:
29
- return list(executor.map(get_image_info, urls))
 
24
  }
25
 
26
 
27
+ def run_parallel_jobs(job, inputs, max: int = 100) -> List[Dict]:
28
  with ThreadPoolExecutor() as executor:
29
+ return list(executor.map(job, inputs[:max]))
control_flow/main.py CHANGED
@@ -1,12 +1,10 @@
1
- import json
2
 
3
  from ai import image
4
-
5
- # from ai_tasks.best_headlines import get_headlines
6
- from ai_tasks.headlines_for_images import get_headlines
7
- from ai_tasks.image_prompt import generate_prompt
8
  from ai_tasks.text_summary import summarize_text
9
- from code_tasks.custom import get_info_for_images
10
  from code_tasks.images_in_url import get_images_from_url
11
  from code_tasks.text_in_url import get_text_from_url
12
  from utils.io import print_assistant, print_system, user_input
@@ -22,23 +20,28 @@ DIMENSIONS = [
22
 
23
 
24
  def run():
25
- # url = user_input("URL: ")
26
- url = "https://www.beachterracemc.com/"
 
27
  print_system("Getting URL data...")
28
  text = get_text_from_url(url)
29
  images = get_images_from_url(url)
30
- image_info = get_info_for_images(images)
31
- print_system(json.dumps(image_info, indent=2))
32
 
 
33
  summary = summarize_text(text)
34
  print_assistant(summary)
35
- headlines = get_headlines(summary, DIMENSIONS, image_info)
36
  print_assistant(headlines)
37
 
38
- # headlines = get_headlines(text)
39
- # print_assistant(headlines)
40
- # prompt = generate_prompt(text)
41
- # print_assistant(prompt)
42
- # print_system("Generating images...")
43
- # image_urls = image.urls(prompt, n=4)
44
- # print_assistant("\n\n".join(image_urls))
 
 
 
 
 
1
+ from typing import Dict
2
 
3
  from ai import image
4
+ from ai_tasks.headlines_for_images import get_headlines_for_images
5
+ from ai_tasks.headlines_ai_images import generate_headlines
 
 
6
  from ai_tasks.text_summary import summarize_text
7
+ from code_tasks.custom import get_image_info, run_parallel_jobs
8
  from code_tasks.images_in_url import get_images_from_url
9
  from code_tasks.text_in_url import get_text_from_url
10
  from utils.io import print_assistant, print_system, user_input
 
20
 
21
 
22
  def run():
23
+ url = user_input("URL: ")
24
+
25
+ # Code tasks (most code was generated by ChatGPT-4)
26
  print_system("Getting URL data...")
27
  text = get_text_from_url(url)
28
  images = get_images_from_url(url)
29
+ image_info = run_parallel_jobs(get_image_info, images, max=10)
 
30
 
31
+ # AI tasks
32
  summary = summarize_text(text)
33
  print_assistant(summary)
34
+ headlines = get_headlines_for_images(summary, DIMENSIONS, image_info)
35
  print_assistant(headlines)
36
 
37
+ headlines_prompts = generate_headlines(summary, DIMENSIONS)
38
+ print_system("Getting AI images...")
39
+ run_parallel_jobs(gen_image, headlines_prompts)
40
+
41
+
42
+ def gen_image(input: Dict[str, str]) -> None:
43
+ ai_image = image.urls(input["prompt"], size=input["dimension_to_map"])[0]
44
+ print_system(f"Prompt: {input['prompt']}")
45
+ print_assistant(input["ad_dimension"])
46
+ print_assistant(input["headline"])
47
+ print_assistant(ai_image)