This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
topics.py
85 lines (68 loc) · 1.9 KB
/
topics.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Ordnet allen Einträgen mithilfe von KI Topics zu
from dotenv import load_dotenv
from copy import deepcopy
from os import environ, getenv
from functools import lru_cache
import openai
import utils
load_dotenv()
openai.api_key = getenv("OPENAI_API_KEY")
def main():
file_path = "WebScrapping/Kölnopendata.json"
data = utils.read_json_to_dict(file_path)
length = len(data["items"])
for i, value in enumerate(reversed(deepcopy(data["items"]))):
# if "thema" in value:
# continue
text = value["title"] + value["description"]
topic = [word.strip() for word in get_topic(text).split(",")]
data["items"][i]["thema"] = topic
print(f"{i}/{length} - {topic} - {value['title']}")
utils.write_dict_to_json(file_path, data)
@lru_cache
def get_topic(text: str) -> str:
messages = [
{
"role": "system",
"content": """Task: Classify the following Text by one or more of the following topics. Don't make topics up on your own.
Topics:
- MINT
- Politik
- Bürgerbeteiligung
- Sprache
- Handwerk
- Informatik
- Kunst/Kultur
- Geschichte
- Logikspiele/Spiele
- Sport
- Schule"""
},
{
"role": "user",
"content": "Bilderbuchgeschichten für Kinder ab 3 Jahren in der Stadtteilbibliothek Chorweiler"
},
{
"role": "assistant",
"content": "Sprache"
},
{
"role": "user",
"content": "Workshop Digitales Zeichnen"
},
{
"role": "assistant",
"content": "Informatik,Kunst/Kultur"
},
{
"role": "user",
"content": text
}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
)
return response["choices"][0]["message"]["content"]
if __name__ == "__main__":
main()