-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
290 lines (226 loc) · 9.33 KB
/
Copy pathapp.py
File metadata and controls
290 lines (226 loc) · 9.33 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import os
import pandas as pd
from flask import Flask, render_template, request, jsonify
from dotenv import load_dotenv
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import json
import re
import time
import requests
load_dotenv()
# ---------------------------------------------------------
# CONFIGURE HACKAI / OPENROUTER PROXY
# ---------------------------------------------------------
HACKAI_KEY = os.getenv("HACKAI_API_KEY")
if not HACKAI_KEY:
raise RuntimeError("❌ No HACKAI_API_KEY found in .env")
HACKAI_URL = "https://ai.hackclub.com/proxy/v1/chat/completions"
app = Flask(__name__)
limiter = Limiter(
key_func=get_remote_address,
app=app,
default_limits=["20 per minute"]
)
# ---------------------------------------------------------
# LOAD DATASET
# ---------------------------------------------------------
df = pd.read_csv("data/problems.csv")
difficulty_map = {
"intro": "intro",
"intermediate": "intermediate",
"medium": "intermediate",
"advanced": "advanced",
"hard": "advanced"
}
df["level"] = df["level"].map(difficulty_map).fillna(df["level"])
# =========================================================
# GENERATION USING HACKAI (OpenRouter format)
# =========================================================
def call_hackai_generate(prompt, num_problems, max_attempts=3, backoff=1.0):
"""
Uses HackAI proxy (OpenRouter-compatible) to generate text.
"""
last_raw = ""
for attempt in range(1, max_attempts + 1):
try:
payload = {
"model": "qwen/qwen3-32b", # can change anytime
"messages": [
{"role": "user", "content": prompt}
],
"stream": False
}
headers = {
"Authorization": f"Bearer {HACKAI_KEY}",
"Content-Type": "application/json"
}
r = requests.post(HACKAI_URL, headers=headers, json=payload, timeout=60)
if r.status_code != 200:
print(f"[ERROR] HackAI returned HTTP {r.status_code}")
print(r.text)
time.sleep(backoff * attempt)
continue
data = r.json()
raw = data["choices"][0]["message"]["content"]
last_raw = raw
problems = _block_parse(raw, num_problems)
if len(problems) >= num_problems:
return problems
except Exception as e:
print(f"[WARNING] HackAI failed (Try {attempt}): {e}")
time.sleep(backoff * attempt)
# Fallback
if not last_raw:
return [{
"title": "API Error",
"statement": "No response from HackAI (likely overload). Try again later.",
"solution": "The model returned no output."
}]
return _block_parse(last_raw, num_problems)
# =========================================================
# PARSER
# =========================================================
def _block_parse(txt, num):
if not isinstance(txt, str) or not txt.strip():
return []
blocks = re.split(r"###\s*Problem\s*\d+", txt, flags=re.IGNORECASE)
problems = []
for block in blocks:
b = block.strip()
if not b:
continue
parts = re.split(r"(Statement|Solution)\s*:\s*", b, flags=re.DOTALL | re.IGNORECASE)
statement = ""
solution = ""
for i in range(1, len(parts), 2):
key = parts[i].lower().strip()
value = parts[i+1].strip()
if key == "statement":
next_key_match = re.search(r"(Solution)\s*:\s*", value, flags=re.DOTALL | re.IGNORECASE)
statement = value[:next_key_match.start()].strip() if next_key_match else value.strip()
elif key == "solution":
solution = value.strip()
if not statement:
stmt_match = re.search(
r"(.*?)(?=Solution\s*:|$)",
b, flags=re.DOTALL | re.IGNORECASE
)
statement = stmt_match.group(1).strip() if stmt_match else b
if not solution:
solution_match = re.search(
r"Solution\s*:\s*(.*)$",
b, flags=re.DOTALL | re.IGNORECASE
)
solution = solution_match.group(1).strip() if solution_match else ""
statement = re.sub(r"###$", "", statement).strip()
solution = re.sub(r"###$", "", solution).strip()
statement = "\n".join(
ln for ln in statement.splitlines()
if not ln.lower().startswith("answer:")
).strip()
statement = re.sub(r"^Problem\s*\d+\s*", "", statement).strip()
problems.append({
"title": "",
"statement": statement,
"solution": solution
})
if len(problems) >= num:
break
return problems
# =========================================================
# ROUTES
# =========================================================
@app.route("/")
def index():
contests = sorted(df["contest"].unique())
topics = sorted(df["topic"].unique())
levels = ["intro", "intermediate", "advanced"]
return render_template("index.html", contests=contests, topics=topics, levels=levels)
@app.route("/generate", methods=["POST"])
def generate():
contest = request.form["contest"]
topic = request.form["topic"]
subtopic = request.form["subtopic"]
difficulty = request.form["difficulty"]
num_problems = int(request.form["num_problems"])
filtered = df[df["contest"] == contest]
if topic:
filtered = filtered[filtered["topic"] == topic]
if subtopic:
filtered = filtered[filtered["subtopic"] == subtopic]
if filtered.empty:
sample_texts = "No specific examples available for this filter combination."
else:
sample_texts = "\n\n".join(
filtered["problem_text"].sample(min(5, len(filtered))).tolist()
)
prompt = f"""
You are an elite math contest problem-setter.
Generate EXACTLY {num_problems} NEW and ORIGINAL math contest problems with:
Contest style: {contest}
Topic: {topic if topic else "any"}
Subtopic: {subtopic if subtopic else "any"}
Difficulty level: {difficulty}
Learn the writing style ONLY from these real problems:
{sample_texts}
REQUIREMENTS:
- DO NOT copy or modify any dataset problems.
- DO NOT include ANY introductory text.
- DO NOT say "Here are your problems", "Sure", "Okay", or anything else before Problem 1.
- Start DIRECTLY with "### Problem 1".
- Use SHORT, official contest-style wording (AMC/AIME/CMO style).
- The statement MUST NOT contain the answer.
- The answer must appear ONLY inside the solution.
- Use LaTeX notation for all math expressions, wrapped in inline ($...$) or display ($$...$$) delimiters (e.g., $x^2 + 1$, not just x^2 + 1).
- Keep LaTeX notation simple (like x^2, \sqrt{''}, \frac{''}{''}).
- No markdown formatting except the "###" separators.
CONTEST STYLE RULES (MANDATORY):
If the contest is AIME:
- Answer must be a positive integer from 0 to 999.
- No calculator use in this contest, so dont have heavy computation and approximation in these problems.
- Problems must rely on typical AIME structures: modular arithmetic, functional equations, tricky geometry ratios, combinatorics with structure, or clever integer reasoning.
- Avoid brute force search problems or problems requiring checking large ranges.
- Avoid problems where the answer is obviously > 999.
- Difficulty should resemble AIME problems #9–#15 if level = "advanced", or #1–#8 if level = "intermediate".
- Use classical AIME phrasing and conciseness.
If the contest is Euclid/CSIMC:
- Problems must be multi-step.
- Statement can be longer.
- As calculator is allowed in these contests, the solution can involve more heavy computation or approximation than AIME.
- May include proofs or partial reasoning.
- Should involve algebraic manipulation or geometry reasoning, etc., typical of Euclid.
- If it is an Advanced problem under this category:
- Very deep problem structure.
- Often requires lemma-style insight.
- Significantly more olympiad-level theory based.
- Should have a highly inductive difficult transformation or trick.
- Typically these problems are proof questions rather than expecting singular answers.
If the contest is AMC:
- Simple, elegant, one-step conceptual.
- Small numbers.
- Light computation.
Note what the difficulty level signifies too:
- Advanced is typically olympiad level difficulty
- Intermediate is Q5-Q10 if AIME, Q7-9 if Euclid, etc.
- Intro on Euclid or CSIMC would be quite easy relevatively (still contest-level), but fairly difficult on AIME since high difficulty floor (Q1-4).
Each contest has its OWN flavor.
As an elite, original, and creative problem-settor, YOU MUST match the targeted contest exactly and specific in tone, answer, problem style.
Study the given sample texts to notice possible strong patterns, and use intenret knowledge if necessary as well.
STRICT OUTPUT FORMAT (NO EXTRA TEXT):
### Problem 1
Statement: ...
Solution: ...
###
### Problem 2
Statement: ...
Solution: ...
###
(Continue until Problem {num_problems})
Produce EXACTLY {num_problems} problems in this format.
No extra commentary. No explanation outside solutions.
"""
generated = call_hackai_generate(prompt, num_problems)
return jsonify(generated)
if __name__ == "__main__":
app.run(debug=True)