-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgptwiki.py
More file actions
76 lines (61 loc) · 2.35 KB
/
gptwiki.py
File metadata and controls
76 lines (61 loc) · 2.35 KB
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
import openai
import pywikibot
import re
import random
seed = random.randint(100000, 999999)
import requests
import traceback
from bs4 import BeautifulSoup
import datetime
import time
import threading
openai.api_key = ""
openai.base_url = ""
model="gpt-4o-mini"
def generate_title(topic_prompt):
topic = openai.chat.completions.create(
model=model,
messages=[{"role": "user", "content": topic_prompt}],
temperature=1.5,
top_p=0.95,
timeout=60
).choices[0].message.content.strip()
print(f"🎯 Selected topic: {topic}")
site = pywikibot.Site("en", "EdwardWiki")
page = pywikibot.Page(site, topic)
if page.exists():
print(f"⚠️ Skipping existing topic: {topic}")
return None
return topic
def generate_article(article_prompt):
article_text = openai.chat.completions.create(
model=model,
messages=[{"role": "user", "content": article_prompt}],
timeout=60
).choices[0].message.content
article_text = re.sub(r"```.*?\n", "", article_text)
article_text = re.sub(r"\n```", "", article_text)
article_text = re.sub(r'^[\"\']{1,2}(.*?)[\"\']{1,2}$', r"\1", article_text, flags=re.M)
article_text = re.sub(r'^[-•]\s+', '', article_text, flags=re.M)
article_text = re.sub(r'^\s*[*]+\s+', '* ', article_text, flags=re.M)
article_text = article_text.strip()
return article_text
def generate_categories(topic, article_text):
cat_prompt = (
f"Given the MediaWiki article titled '{topic}', list up to three appropriate Wikipedia-style "
"categories (broadest first). Respond with category names only, comma-separated, no extra text."
)
raw_cats = openai.chat.completions.create(
model=model,
messages=[{"role": "user", "content": cat_prompt}],
).choices[0].message.content
cats = [c.strip() for c in raw_cats.split(",") if c.strip()]
if cats:
article_text += "\n\n" + "\n".join(f"[[Category:{c}]]" for c in cats)
return article_text, cats
def upload_to_wiki(topic, article_text, cats):
site = pywikibot.Site("en", "EdwardWiki")
page = pywikibot.Page(site, topic)
page.text = article_text
page.save(summary=f"Created article '{topic}' with auto-categories 🏷️")
print(f"✅ Article '{topic}' published, categories: {', '.join(cats) if cats else 'none'}")