Spaces:
Runtime error
Runtime error
Add image generation
Browse files- ai/image.py +15 -0
- ai/llm.py +0 -1
- ai_tasks/best_headlines.py +5 -5
- ai_tasks/image_prompt.py +18 -0
- control_flow/main.py +8 -1
ai/image.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import Any, Dict, List
|
3 |
+
|
4 |
+
import openai
|
5 |
+
|
6 |
+
openai.api_key = os.environ["OPENAI_KEY_PERSONAL"]
|
7 |
+
|
8 |
+
|
9 |
+
def gen(prompt: str, n: int, size: str) -> Dict[str, Any]:
|
10 |
+
return openai.Image.create(prompt=prompt, n=n, size=size) # type: ignore
|
11 |
+
|
12 |
+
|
13 |
+
def urls(prompt: str, n: int = 4, size: str = "512x512") -> List[str]:
|
14 |
+
images = gen(prompt, n, size)
|
15 |
+
return [i["url"] for i in images["data"]] # type: ignore
|
ai/llm.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import json
|
2 |
import os
|
3 |
from typing import Any, Dict, List, Optional
|
4 |
|
|
|
|
|
1 |
import os
|
2 |
from typing import Any, Dict, List, Optional
|
3 |
|
ai_tasks/best_headlines.py
CHANGED
@@ -3,15 +3,15 @@ from utils.io import print_system
|
|
3 |
|
4 |
|
5 |
PROMPT = """
|
6 |
-
Here is the text from a website
|
7 |
-
Extract or create the 10 best headlines for an ad about the website.
|
8 |
-
|
9 |
{text}
|
|
|
|
|
10 |
"""
|
11 |
|
12 |
|
13 |
-
def get_headlines(text) -> str:
|
14 |
print_system("Generating headlines...")
|
15 |
instructions = PROMPT.format(text=text)
|
16 |
messages = [{"role": "user", "content": instructions}]
|
17 |
-
return llm.next(messages
|
|
|
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/image_prompt.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
Don't include people or text.
|
11 |
+
"""
|
12 |
+
|
13 |
+
|
14 |
+
def generate_prompt(text: str) -> str:
|
15 |
+
print_system("Generating prompt for image...")
|
16 |
+
instructions = PROMPT.format(text=text)
|
17 |
+
messages = [{"role": "user", "content": instructions}]
|
18 |
+
return llm.next(messages, temperature=0)
|
control_flow/main.py
CHANGED
@@ -1,6 +1,8 @@
|
|
|
|
1 |
from ai_tasks.best_headlines import get_headlines
|
|
|
2 |
from code_tasks.url_text import get_text_from_url
|
3 |
-
from utils.io import print_assistant, user_input
|
4 |
|
5 |
|
6 |
def run():
|
@@ -8,3 +10,8 @@ def run():
|
|
8 |
text = get_text_from_url(url)
|
9 |
headlines = get_headlines(text)
|
10 |
print_assistant(headlines)
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from ai import image
|
2 |
from ai_tasks.best_headlines import get_headlines
|
3 |
+
from ai_tasks.image_prompt import generate_prompt
|
4 |
from code_tasks.url_text import get_text_from_url
|
5 |
+
from utils.io import print_assistant, print_system, user_input
|
6 |
|
7 |
|
8 |
def run():
|
|
|
10 |
text = get_text_from_url(url)
|
11 |
headlines = get_headlines(text)
|
12 |
print_assistant(headlines)
|
13 |
+
prompt = generate_prompt(text)
|
14 |
+
print_assistant(prompt)
|
15 |
+
print_system("Generating images...")
|
16 |
+
image_urls = image.urls(prompt, n=4)
|
17 |
+
print_assistant("\n\n".join(image_urls))
|