-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgptwritelang.py
More file actions
163 lines (137 loc) Β· 6 KB
/
gptwritelang.py
File metadata and controls
163 lines (137 loc) Β· 6 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
NUM_THREADS = 10
ERROR_SLEEP = 5
openai.api_key = ""
openai.base_url = ""
model=None
from cstitle import *
def main():
topic = get_random_cs_topic()
topic_prompt = (
f"The phrase \"{topic}\" was extracted from Stack Overflow and Reddit etc. "
"It may come from a question or descriptive phrase.\n\n"
"Your task is to rewrite this phrase as the name of a **programming language, software development tool, or operating system only**.\n\n"
"Rules:\n"
"- Only return **1 name**. Don't too common\n"
"- The result must be a **real, widely used** language (e.g., Python, C++, JavaScript), tool (e.g., Git, Docker, GCC, Vim), or OS (e.g., Linux, Windows), or framework (e.g. Flask, Django).\n"
"- Do NOT include niche libraries, company names, course titles, or made-up terms.\n"
"- Only return the name, with **no punctuation, no quotes, and no explanation**.\n"
"- Keep correct casing (e.g., C++, JavaScript, TypeScript, macOS).\n\n"
f"Random seed: {seed}"
)
topic = openai.chat.completions.create(
model=model,
messages=[{"role": "user", "content": topic_prompt}],
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
article_prompt = f"""
Write a long and detailed Wikipedia-style article about '''{topic}''' in pure MediaWiki markup.
== REQUIRED FORMAT ==
- Use only MediaWiki formatting (== Section headings ==, '''bold''', [[internal links]], etc.).
- The article must contain **at least 6 main sections**, each with its own == heading ==. Subsections must use === subheadings ===.
- Do not include bullet lists as substitutes for content.
- Never output a flat article without headings.
- Do not use markdown, bullet points, or explanatory assistant messages.
- Content must be at least 3000 words, well-organized and fully structured like a real Wikipedia article.
== EXAMPLE HEADINGS ==
- Background or History
- Architecture or Design
- Implementation or Applications
- Real-world Examples
- Criticism or Limitations
- See also
- References (only official websites or trusted docs, no academic papers)
== STYLE ==
- Use formal, encyclopedic tone.
- Explain clearly for general readers.
- Avoid generic filler or vague summaries.
- Avoid duplicating the topic name excessively.
- The article must contain at least 6 sections, each starting with == Heading == and every sections use the == Heading ==. Do not skip this. Articles without enough section headers will be rejected. Do not include an '== Introduction ==' section β instead, place the introduction before any headers.
Start with the following exact format:
'''{topic}''' is ...
== Other Heading... ==
...
Then continue with at least 4 more major sections.
== OUTPUT ==
Return only the article in MediaWiki format. No additional instructions or notes.
"""
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()
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)
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'}")
def log_error(e):
with open("error.log", "a", encoding="utf-8") as f:
f.write(f"\n[{datetime.datetime.now().isoformat()}] ERROR:\n")
f.write(traceback.format_exc())
f.write("\n" + "-"*80 + "\n")
def worker(tid):
run_count = 0
while True:
try:
start = time.time()
print(f"\nπ§΅[T{tid}] β±οΈ Run #{run_count+1} "
f"{datetime.datetime.now().isoformat()}")
main()
run_count += 1
print(f"π§΅[T{tid}] β
Done #{run_count} "
f"{time.time()-start:.2f}s")
except KeyboardInterrupt:
print(f"π§΅[T{tid}] π User interrupt, exit.")
break
except Exception as e:
print(f"π§΅[T{tid}] β οΈ Error, logged & retry in "
f"{ERROR_SLEEP}s.")
log_error(e, tid)
time.sleep(ERROR_SLEEP)
def loop_run_multithread():
threads = []
for tid in range(NUM_THREADS):
t = threading.Thread(target=worker, args=(tid,), daemon=True)
t.start()
threads.append(t)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("π Master interrupt, exitingβ¦")
if __name__ == "__main__":
loop_run_multithread()