Spaces:
Runtime error
Runtime error
Ads generated from website images
Browse files- ai/image.py +4 -0
- ai/llm.py +3 -0
- ai_tasks/headlines_for_images.py +41 -0
- ai_tasks/text_summary.py +20 -0
- code_tasks/custom.py +14 -7
- code_tasks/image_dimensions.py +8 -0
- control_flow/main.py +32 -10
- main.py +0 -5
ai/image.py
CHANGED
@@ -2,6 +2,10 @@ import os
|
|
2 |
from typing import Any, Dict, List
|
3 |
|
4 |
import openai
|
|
|
|
|
|
|
|
|
5 |
|
6 |
openai.api_key = os.environ["OPENAI_KEY_PERSONAL"]
|
7 |
|
|
|
2 |
from typing import Any, Dict, List
|
3 |
|
4 |
import openai
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
|
10 |
openai.api_key = os.environ["OPENAI_KEY_PERSONAL"]
|
11 |
|
ai/llm.py
CHANGED
@@ -2,6 +2,9 @@ import os
|
|
2 |
from typing import Any, Dict, List, Optional
|
3 |
|
4 |
import openai
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
openai.api_key = os.environ["OPENAI_KEY_PERSONAL"]
|
|
|
2 |
from typing import Any, Dict, List, Optional
|
3 |
|
4 |
import openai
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
|
9 |
|
10 |
openai.api_key = os.environ["OPENAI_KEY_PERSONAL"]
|
ai_tasks/headlines_for_images.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from typing import Dict, List
|
3 |
+
|
4 |
+
from ai import llm
|
5 |
+
from utils.io import print_system
|
6 |
+
|
7 |
+
|
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 |
+
|
15 |
+
Summary of the website:
|
16 |
+
{summary}
|
17 |
+
|
18 |
+
Image urls, with labels and dimensions:
|
19 |
+
{images}
|
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,
|
37 |
+
images=json.dumps(image_labels, indent=2),
|
38 |
+
dimensions=dimensions,
|
39 |
+
)
|
40 |
+
messages = [{"role": "user", "content": instructions}]
|
41 |
+
return llm.next(messages, temperature=0)
|
ai_tasks/text_summary.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from typing import Dict, List
|
3 |
+
|
4 |
+
from ai import llm
|
5 |
+
from utils.io import print_system
|
6 |
+
|
7 |
+
|
8 |
+
PROMPT = """
|
9 |
+
Here is the text from a website:
|
10 |
+
{text}
|
11 |
+
|
12 |
+
What is this website about?
|
13 |
+
"""
|
14 |
+
|
15 |
+
|
16 |
+
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)
|
code_tasks/custom.py
CHANGED
@@ -1,22 +1,29 @@
|
|
1 |
from concurrent.futures import ThreadPoolExecutor
|
2 |
from typing import Dict, List
|
3 |
|
|
|
4 |
from google.cloud import vision
|
5 |
|
6 |
-
from
|
7 |
|
|
|
8 |
|
9 |
-
|
|
|
10 |
client = vision.ImageAnnotatorClient()
|
11 |
image = vision.Image()
|
12 |
image.source.image_uri = url # type: ignore
|
13 |
|
14 |
response = client.label_detection(image=image) # type: ignore
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
-
def
|
19 |
-
print_system("Generating labels for images...")
|
20 |
with ThreadPoolExecutor() as executor:
|
21 |
-
|
22 |
-
return {urls[i]: labels[i] for i, url in enumerate(labels)}
|
|
|
1 |
from concurrent.futures import ThreadPoolExecutor
|
2 |
from typing import Dict, List
|
3 |
|
4 |
+
from dotenv import load_dotenv
|
5 |
from google.cloud import vision
|
6 |
|
7 |
+
from code_tasks.image_dimensions import get_image_dimensions
|
8 |
|
9 |
+
load_dotenv()
|
10 |
|
11 |
+
|
12 |
+
def get_image_info(url: str) -> Dict:
|
13 |
client = vision.ImageAnnotatorClient()
|
14 |
image = vision.Image()
|
15 |
image.source.image_uri = url # type: ignore
|
16 |
|
17 |
response = client.label_detection(image=image) # type: ignore
|
18 |
+
labels = [label.description for label in response.label_annotations]
|
19 |
+
dimensions = get_image_dimensions(url)
|
20 |
+
return {
|
21 |
+
"url": url,
|
22 |
+
"labels": ", ".join(labels),
|
23 |
+
"dimensions": f"{dimensions[0]}x{dimensions[1]}",
|
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))
|
|
code_tasks/image_dimensions.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import requests
|
3 |
+
from io import BytesIO
|
4 |
+
|
5 |
+
def get_image_dimensions(image_url):
|
6 |
+
response = requests.get(image_url)
|
7 |
+
img = Image.open(BytesIO(response.content))
|
8 |
+
return img.size
|
control_flow/main.py
CHANGED
@@ -1,22 +1,44 @@
|
|
|
|
|
|
1 |
from ai import image
|
2 |
-
|
|
|
|
|
3 |
from ai_tasks.image_prompt import generate_prompt
|
4 |
-
from
|
|
|
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 utils.io import print_assistant, print_system, user_input
|
8 |
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def run():
|
11 |
-
url = user_input("URL: ")
|
|
|
|
|
12 |
text = get_text_from_url(url)
|
13 |
images = get_images_from_url(url)
|
14 |
-
|
|
|
15 |
|
16 |
-
|
|
|
|
|
17 |
print_assistant(headlines)
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
print_assistant(
|
|
|
|
|
|
|
|
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
|
13 |
|
14 |
|
15 |
+
DIMENSIONS = [
|
16 |
+
"300x50",
|
17 |
+
"300x250",
|
18 |
+
"300x600",
|
19 |
+
"728x90",
|
20 |
+
"160x600",
|
21 |
+
]
|
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))
|
main.py
CHANGED
@@ -1,10 +1,5 @@
|
|
1 |
-
from dotenv import load_dotenv
|
2 |
-
|
3 |
from control_flow.main import run
|
4 |
|
5 |
|
6 |
-
load_dotenv()
|
7 |
-
|
8 |
-
|
9 |
if __name__ == "__main__":
|
10 |
run()
|
|
|
|
|
|
|
1 |
from control_flow.main import run
|
2 |
|
3 |
|
|
|
|
|
|
|
4 |
if __name__ == "__main__":
|
5 |
run()
|