-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
45 lines (36 loc) · 1.17 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import requests
from bs4 import BeautifulSoup
import html2text
import openai
import os
import sys
url_base = "https://en.wikipedia.org/wiki/"
openai.api_key = os.getenv("OPEN_API_KEY")
base_prompt = "Play as a G,PG,PG13, R or explicit rater for wikipedia pages. Format your response with Rating: \n Reasoning: "
def getWikiPage(page):
response = requests.get(url=url_base+page)
soup = BeautifulSoup(response.content,'html.parser')
paragraphs = soup.findAll('p')
return [ html2text.html2text(paragraph.text) for paragraph in paragraphs]
def rate(text):
result = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{'role': 'system', "content": base_prompt}, {'role': 'user', 'content': text}],
temperature=0
)
return result
def concatText(paragraphs):
i=0
wordCount=0
result=""
while i<len(paragraphs) and wordCount<500:
result = result+'\n'+paragraphs[i]
wordCount = wordCount+len(paragraphs[i].split())
i=i+1
return result
def main(argv):
paragraphs = getWikiPage(argv)
text = concatText(paragraphs)
print(rate(text))
if __name__ == "__main__":
main(sys.argv[1])