lgaleana commited on
Commit
0eb1950
0 Parent(s):

Generate headlines

Browse files
.env.example ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_KEY_PERSONAL=<openai_key>
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ venv
2
+ .vscode
3
+ logs
4
+ .env
ai/__init__.py ADDED
File without changes
ai/llm.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from typing import Any, Dict, List, Optional
4
+
5
+ import openai
6
+
7
+
8
+ openai.api_key = os.environ["OPENAI_KEY_PERSONAL"]
9
+ MODEL = "gpt-3.5-turbo"
10
+ TEMPERATURE = 0.6
11
+
12
+
13
+ def call(
14
+ messages: List[Dict[str, str]],
15
+ model: Optional[str] = None,
16
+ temperature: Optional[float] = None,
17
+ stop: Optional[str] = None,
18
+ ) -> Dict[str, Any]:
19
+ if not model:
20
+ model = MODEL
21
+ if not temperature:
22
+ temperature = TEMPERATURE
23
+
24
+ return openai.ChatCompletion.create( # type: ignore
25
+ model=model,
26
+ messages=messages,
27
+ temperature=temperature,
28
+ stop=stop,
29
+ )
30
+
31
+
32
+ def next(
33
+ messages: List[Dict[str, str]],
34
+ model: Optional[str] = None,
35
+ temperature: Optional[float] = None,
36
+ stop: Optional[str] = None,
37
+ ) -> str:
38
+ return call(messages, model, temperature, stop)["choices"][0]["message"]["content"]
ai_tasks/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+
ai_tasks/best_headlines.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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, temperature=0)
code_tasks/__init__.py ADDED
File without changes
code_tasks/url_text.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ChatGPT-4 prompt: write a python function that given an url returns all text in the website
3
+ """
4
+
5
+ import requests
6
+ from bs4 import BeautifulSoup, Comment
7
+
8
+ def get_text_from_url(url):
9
+ response = requests.get(url)
10
+ soup = BeautifulSoup(response.text, 'html.parser')
11
+
12
+ # remove all script and style elements
13
+ for script in soup(["script", "style"]):
14
+ script.decompose() # rip it out
15
+
16
+ # get text
17
+ text = soup.get_text()
18
+
19
+ # break into lines and remove leading and trailing spaces on each
20
+ lines = (line.strip() for line in text.splitlines())
21
+ # break multi-headlines into a line each
22
+ chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
23
+ # remove blank lines
24
+ text = '\n'.join(chunk for chunk in chunks if chunk)
25
+
26
+ return text
control_flow/__init__.py ADDED
File without changes
control_flow/main.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
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_system, user_input
4
+
5
+
6
+ def run():
7
+ url = user_input("URL: ")
8
+ text = get_text_from_url(url)
9
+ headlines = get_headlines(text)
10
+ print_system(headlines)
main.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
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()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ beautifulsoup4
2
+ openai
3
+ python-dotenv
4
+ requests==2.28.1
utils/__init__.py ADDED
File without changes
utils/io.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def print_system(message) -> str:
2
+ print(f"\033[0;0m{message}")
3
+ return message
4
+
5
+
6
+ def print_assistant(message) -> str:
7
+ print(f"\033[92m{message}")
8
+ return message
9
+
10
+
11
+ def user_input(message: str = "") -> str:
12
+ return input(f"\033[1;34m{message}")