File size: 21,021 Bytes
787a7ad 968a16c 3d65e75 787a7ad af51047 787a7ad af51047 787a7ad af51047 787a7ad 968a16c 787a7ad 968a16c 787a7ad af51047 787a7ad af51047 787a7ad af51047 787a7ad af51047 787a7ad af51047 787a7ad af51047 787a7ad af51047 787a7ad af51047 787a7ad af51047 787a7ad af51047 787a7ad 1d93c0f 6ec0521 7e33616 6ec0521 1d93c0f 6ec0521 1d93c0f 787a7ad 1d93c0f 6ec0521 1d93c0f 787a7ad 1d93c0f 787a7ad 1d93c0f 6ec0521 787a7ad 6ec0521 787a7ad 6ec0521 1d93c0f f362e3f 1d93c0f 6ec0521 1d93c0f 6ec0521 1d93c0f 6ec0521 028a7af 6ec0521 91ada30 6ec0521 91ada30 6ec0521 3728ea4 6ec0521 3728ea4 928f73d 028a7af 6ec0521 928f73d 6ec0521 1b63bb6 7b999d3 6ec0521 7b999d3 1b63bb6 6ec0521 f362e3f 1d93c0f 6ec0521 91ada30 8795fbb 6ec0521 f362e3f 6ec0521 f362e3f 6ec0521 f362e3f 8795fbb f362e3f 6ec0521 1d93c0f 787a7ad 6ec0521 787a7ad 4e2080a 787a7ad 4e2080a 787a7ad f362e3f 4e2080a f362e3f 6ec0521 4e2080a 787a7ad 7e33616 f362e3f 7e33616 1d93c0f f362e3f 7e33616 1d93c0f f362e3f 6ec0521 af51047 7f3c648 2f9bb62 7f3c648 1d93c0f f362e3f 6ec0521 f362e3f 6ec0521 787a7ad 1d93c0f 787a7ad 1d93c0f 787a7ad 1d93c0f 787a7ad af51047 787a7ad af51047 787a7ad 1d93c0f 787a7ad af51047 787a7ad af51047 787a7ad 1d93c0f 787a7ad af51047 787a7ad af51047 787a7ad 1d93c0f 787a7ad af51047 787a7ad af51047 787a7ad 1d93c0f 787a7ad af51047 787a7ad af51047 787a7ad 1d93c0f 787a7ad 1d93c0f 787a7ad |
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
# -*- coding: utf-8 -*-
"""
UltraData-Math L3 Generator - Hugging Face Space Demo
"""
import os
import asyncio
import gradio as gr
from openai import AsyncOpenAI
from qa_synthesis import QA_PROMPTS, get_qa_prompt
from conversation_synthesis import CONVERSATION_PROMPTS, get_conversation_prompt
from multistyle_rewrite import MULTISTYLE_PROMPTS, get_multistyle_prompt
from knowledge_textbook import (
get_knowledge_extraction_prompt,
get_textbook_exercise_prompt,
TEXTBOOK_EXERCISE_PROMPTS,
)
from run_synthesis import (
parse_qa_output,
parse_conversation_output,
parse_rewrite_output,
parse_knowledge_output,
parse_textbook_output,
)
# API 配置从环境变量读取(通过 HF Secrets 设置)
API_KEY = os.getenv("OPENAI_API_KEY")
BASE_URL = os.getenv("OPENAI_BASE_URL", "https://llm-center.ali.modelbest.cn/llm/openai/v1")
DEFAULT_MODEL = "GEMINI_anxt74"
# 示例数据
EXAMPLE_MATH_CONTENT = """The quadratic formula is a fundamental result in algebra that provides the solutions to any quadratic equation of the form ax² + bx + c = 0, where a ≠ 0.
The formula states that the solutions are:
x = (-b ± √(b² - 4ac)) / (2a)
The term b² - 4ac is called the discriminant. It determines the nature of the roots:
- If b² - 4ac > 0, there are two distinct real roots
- If b² - 4ac = 0, there is exactly one real root (a repeated root)
- If b² - 4ac < 0, there are two complex conjugate roots
This formula was known to ancient mathematicians and remains one of the most important tools in solving polynomial equations."""
EXAMPLE_KNOWLEDGE_POINT = """Definition: A continuous function is a function f: R → R such that for every point x₀ in its domain and every ε > 0, there exists a δ > 0 such that |f(x) - f(x₀)| < ε whenever |x - x₀| < δ.
Key Properties:
1. The sum, difference, and product of continuous functions are continuous
2. The composition of continuous functions is continuous
3. A continuous function on a closed interval attains its maximum and minimum values (Extreme Value Theorem)
4. A continuous function on a closed interval takes on every value between its minimum and maximum (Intermediate Value Theorem)"""
async def call_api(prompt: str, temperature: float = 0.7) -> str:
"""调用 API 生成内容"""
if not API_KEY:
return "Error: API Key not configured. Please contact administrator."
client = AsyncOpenAI(api_key=API_KEY, base_url=BASE_URL)
try:
response = await client.chat.completions.create(
model=DEFAULT_MODEL,
messages=[{"role": "user", "content": prompt}],
temperature=temperature,
max_tokens=8192,
)
# 处理 reasoning model 的返回格式
message = response.choices[0].message
content = message.content
# 如果 content 为空,尝试获取 reasoning_content
if not content and hasattr(message, 'reasoning_content') and message.reasoning_content:
content = message.reasoning_content
return content or ""
except Exception as e:
return f"Error: {str(e)}"
def run_async(coro):
"""运行异步函数"""
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(coro)
# ============================================================================
# Task Handlers
# ============================================================================
def qa_synthesis(text: str, level: str):
"""Q&A 问答对合成"""
if not text.strip():
return "", "", ""
prompt_template = get_qa_prompt(level)
prompt = prompt_template.format(text=text)
response = run_async(call_api(prompt))
parsed = parse_qa_output(response)
return (
parsed.get("problem", ""),
parsed.get("solution", ""),
response
)
def conversation_synthesis(text: str, style: str):
"""多轮对话合成"""
if not text.strip():
return "", ""
prompt_template = get_conversation_prompt(style)
prompt = prompt_template.format(text=text)
response = run_async(call_api(prompt))
parsed = parse_conversation_output(response)
return parsed.get("content", response), response
def rewrite_synthesis(text: str, style: str):
"""多风格改写"""
if not text.strip():
return "", ""
prompt_template = get_multistyle_prompt(style)
prompt = prompt_template.format(text=text)
response = run_async(call_api(prompt))
parsed = parse_rewrite_output(response)
return parsed.get("rewritten", response), response
def knowledge_extraction(text: str):
"""知识点提取"""
if not text.strip():
return "", ""
prompt_template = get_knowledge_extraction_prompt()
prompt = prompt_template.format(text=text)
response = run_async(call_api(prompt))
parsed = parse_knowledge_output(response)
knowledge_points = parsed.get("knowledge_points", [])
formatted = "\n\n---\n\n".join(knowledge_points) if knowledge_points else "No knowledge points extracted."
return formatted, response
def textbook_exercise(knowledge_point: str, difficulty: str):
"""教材练习生成"""
if not knowledge_point.strip():
return "", ""
prompt_template = get_textbook_exercise_prompt(difficulty)
prompt = prompt_template.format(mathematical_knowledge_point=knowledge_point)
response = run_async(call_api(prompt))
parsed = parse_textbook_output(response)
return parsed.get("material", response), response
# ============================================================================
# Gradio UI
# ============================================================================
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&family=JetBrains+Mono:wght@400;500&display=swap');
:root {
--bg: #f8fafc;
--surface: #ffffff;
--surface-2: #f1f5f9;
--border: #e2e8f0;
--text: #0f172a;
--muted: #1f2937; /* darker for readability */
--accent: #4f46e5;
--accent-2: #6366f1;
}
body {
background-color: var(--bg);
color: var(--text);
}
.gradio-container {
font-family: 'Inter', sans-serif !important;
background: linear-gradient(180deg, #f8fafc 0%, #eef2ff 100%) !important;
max-width: 1200px !important;
}
/* Title & Header */
.main-title {
font-family: 'Inter', sans-serif !important;
font-weight: 800 !important;
font-size: 2.6rem !important;
background: linear-gradient(90deg, #0f172a, #4f46e5, #7c3aed) !important;
-webkit-background-clip: text !important;
-webkit-text-fill-color: transparent !important;
text-align: center !important;
margin-bottom: 0.4rem !important;
}
.subtitle {
text-align: center !important;
color: var(--muted) !important;
font-size: 1.05rem !important;
margin-bottom: 2.5rem !important;
font-weight: 400 !important;
}
/* Panels */
.glass-panel {
background: var(--surface) !important;
border: 1px solid var(--border) !important;
border-radius: 16px !important;
padding: 24px !important;
box-shadow: 0 10px 30px rgba(15, 23, 42, 0.08) !important;
}
/* Labels */
.block > label > span,
.form > label > span,
.gr-form > label > span,
.label-wrap > span {
color: var(--text) !important;
font-weight: 600 !important;
font-size: 1rem !important;
margin-bottom: 0.5rem !important;
text-shadow: none !important;
}
/* Radio group title */
fieldset legend, fieldset legend span,
.gr-radio > label, .gr-radio > label span,
.gradio-container .label-wrap, .gradio-container .label-wrap span {
color: var(--text) !important;
font-weight: 600 !important;
text-shadow: none !important;
}
/* Info Text (Description) */
span.description, .description {
color: var(--muted) !important;
font-weight: 500 !important;
text-shadow: none !important;
opacity: 1 !important;
}
/* Radio/Checkbox alignment */
fieldset label span {
margin-bottom: 0 !important;
text-shadow: none !important;
font-weight: 500 !important;
color: var(--text) !important;
display: flex !important;
align-items: center !important;
}
fieldset label.selected span {
color: #ffffff !important;
}
fieldset label.selected {
background: var(--accent) !important;
border-color: var(--accent) !important;
}
/* Inputs & Textareas */
.gr-input, textarea, input, .gr-box, .gr-check-radio, .gr-dropdown {
font-family: 'JetBrains Mono', monospace !important;
background-color: var(--surface) !important;
border: 1px solid var(--border) !important;
color: var(--text) !important;
box-shadow: none !important;
}
.gr-input:focus, textarea:focus, input:focus {
border-color: var(--accent) !important;
box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.15) !important;
}
/* Dropdown options */
ul.options, .gr-dropdown-options {
background-color: var(--surface) !important;
color: var(--text) !important;
border: 1px solid var(--border) !important;
}
/* Markdown prose */
.prose, .prose p, .prose h1, .prose h2, .prose h3, .prose strong, .prose li {
color: var(--text) !important;
}
/* Outputs */
.output-textbox textarea {
background-color: var(--surface-2) !important;
border: 1px solid var(--border) !important;
border-radius: 8px !important;
color: var(--text) !important;
}
.markdown-box {
background: var(--surface-2) !important;
border: 1px solid var(--border) !important;
border-radius: 8px !important;
padding: 16px !important;
color: var(--text) !important;
}
.markdown-box * {
color: var(--text) !important;
}
.markdown-box code, .markdown-box pre {
background: #e2e8f0 !important;
}
/* Buttons */
.gr-button-primary {
background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%) !important;
border: none !important;
color: #ffffff !important;
font-weight: 600 !important;
box-shadow: 0 6px 14px rgba(79, 70, 229, 0.25) !important;
}
.gr-button-secondary {
background: #f1f5f9 !important; /* clearer separation from page background */
border: 1px solid #cbd5e1 !important;
color: #0f172a !important;
box-shadow: 0 4px 10px rgba(15, 23, 42, 0.08) !important;
}
.gr-button-secondary:hover {
background: #e2e8f0 !important;
border-color: #94a3b8 !important;
}
/* Tabs */
/* Tabs */
.tabs button {
color: #0f172a !important; /* dark text for readability */
font-weight: 600 !important;
}
.tabs button.selected {
color: #ffffff !important;
background: var(--accent) !important;
border-radius: 6px !important;
padding: 4px 10px !important;
}
/* Radio buttons */
.gr-radio-label {
color: var(--text) !important;
}
/* Radio dot visibility (checked) */
.gr-check-radio input[type="radio"] {
width: 16px !important;
height: 16px !important;
accent-color: #111827 !important; /* black dot */
}
fieldset label.selected input[type="radio"] {
accent-color: #111827 !important;
background-color: #111827 !important;
border: 1px solid #111827 !important;
}
/* Footer */
.footer-text, .footer-text p {
color: var(--muted) !important;
}
.footer-text a {
color: var(--accent) !important;
}
"""
with gr.Blocks(title="UltraData-Math L3 Generator", css=custom_css, theme=gr.themes.Soft()) as demo:
gr.HTML('<h1 class="main-title">🧮 UltraData-Math L3 Generator</h1>')
gr.HTML('<p class="subtitle">✨ Next-Gen Mathematical Data Synthesis Powered by LLM ✨</p>')
with gr.Tabs():
# Q&A Synthesis Tab
with gr.TabItem("📝 Q&A Synthesis"):
with gr.Column(elem_classes=["glass-panel"]):
gr.Markdown("### 💡 Transform Text into Q&A Pairs\nGenerate high-quality question-answer pairs from mathematical content, tailored to different educational levels.")
with gr.Row():
with gr.Column(scale=1):
qa_input = gr.Textbox(
label="Input Mathematical Content",
placeholder="Paste your mathematical text here (e.g., definitions, theorems, proofs)...",
lines=10,
)
qa_level = gr.Radio(
choices=list(QA_PROMPTS.keys()),
value="high_school",
label="Difficulty Level",
info="Select the target audience level"
)
with gr.Row():
qa_example_btn = gr.Button("📋 Load Example", variant="secondary")
qa_btn = gr.Button("🚀 Generate Q&A", variant="primary")
with gr.Column(scale=1):
qa_problem = gr.Textbox(label="Generated Problem", lines=5, show_copy_button=True)
qa_solution = gr.Textbox(label="Generated Solution", lines=12, show_copy_button=True)
qa_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
qa_example_btn.click(
lambda: EXAMPLE_MATH_CONTENT,
outputs=[qa_input],
)
qa_btn.click(
qa_synthesis,
inputs=[qa_input, qa_level],
outputs=[qa_problem, qa_solution, qa_raw],
)
# Conversation Synthesis Tab
with gr.TabItem("💬 Conversation Synthesis"):
with gr.Column(elem_classes=["glass-panel"]):
gr.Markdown("### 🗣️ Create Multi-turn Dialogues\nConvert static mathematical text into dynamic, engaging multi-turn conversations between students and teachers.")
with gr.Row():
with gr.Column(scale=1):
conv_input = gr.Textbox(
label="Input Mathematical Content",
placeholder="Paste your mathematical text here...",
lines=10,
)
conv_style = gr.Radio(
choices=list(CONVERSATION_PROMPTS.keys()),
value="teacher_student",
label="Conversation Style",
info="Choose the persona and tone of the conversation"
)
with gr.Row():
conv_example_btn = gr.Button("📋 Load Example", variant="secondary")
conv_btn = gr.Button("🚀 Generate Conversation", variant="primary")
with gr.Column(scale=1):
conv_output = gr.Textbox(label="Generated Conversation", lines=20, show_copy_button=True)
conv_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
conv_example_btn.click(
lambda: EXAMPLE_MATH_CONTENT,
outputs=[conv_input],
)
conv_btn.click(
conversation_synthesis,
inputs=[conv_input, conv_style],
outputs=[conv_output, conv_raw],
)
# Rewrite Tab
with gr.TabItem("✨ Multi-style Rewrite"):
with gr.Column(elem_classes=["glass-panel"]):
gr.Markdown("### 🎨 Style Transfer\nRewrite mathematical content into various styles, from rigorous textbooks to engaging blog posts.")
with gr.Row():
with gr.Column(scale=1):
rewrite_input = gr.Textbox(
label="Input Mathematical Content",
placeholder="Paste your mathematical text here...",
lines=10,
)
rewrite_style = gr.Radio(
choices=list(MULTISTYLE_PROMPTS.keys()),
value="textbook",
label="Target Style",
info="Select the desired output style"
)
with gr.Row():
rewrite_example_btn = gr.Button("📋 Load Example", variant="secondary")
rewrite_btn = gr.Button("🚀 Rewrite Content", variant="primary")
with gr.Column(scale=1):
rewrite_output = gr.Textbox(label="Rewritten Content", lines=20, show_copy_button=True)
rewrite_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
rewrite_example_btn.click(
lambda: EXAMPLE_MATH_CONTENT,
outputs=[rewrite_input],
)
rewrite_btn.click(
rewrite_synthesis,
inputs=[rewrite_input, rewrite_style],
outputs=[rewrite_output, rewrite_raw],
)
# Knowledge Extraction Tab
with gr.TabItem("📚 Knowledge Extraction"):
with gr.Column(elem_classes=["glass-panel"]):
gr.Markdown("### 🧠 Extract Core Knowledge\nAutomatically identify and extract key definitions, theorems, and properties from unstructured text.")
with gr.Row():
with gr.Column(scale=1):
know_input = gr.Textbox(
label="Input Mathematical Content",
placeholder="Paste your mathematical text here...",
lines=12,
)
with gr.Row():
know_example_btn = gr.Button("📋 Load Example", variant="secondary")
know_btn = gr.Button("🚀 Extract Knowledge", variant="primary")
with gr.Column(scale=1):
know_output = gr.Textbox(label="Extracted Knowledge Points", lines=20, show_copy_button=True)
know_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
know_example_btn.click(
lambda: EXAMPLE_MATH_CONTENT,
outputs=[know_input],
)
know_btn.click(
knowledge_extraction,
inputs=[know_input],
outputs=[know_output, know_raw],
)
# Textbook Exercise Tab
with gr.TabItem("📖 Textbook Exercise"):
with gr.Column(elem_classes=["glass-panel"]):
gr.Markdown("### 📝 Generate Exercises\nCreate comprehensive textbook-style exercises and problems based on specific knowledge points.")
with gr.Row():
with gr.Column(scale=1):
textbook_input = gr.Textbox(
label="Input Knowledge Point",
placeholder="Enter a specific mathematical concept or theorem...",
lines=8,
)
textbook_diff = gr.Radio(
choices=list(TEXTBOOK_EXERCISE_PROMPTS.keys()),
value="easy",
label="Difficulty",
info="Select the problem difficulty"
)
with gr.Row():
textbook_example_btn = gr.Button("📋 Load Example", variant="secondary")
textbook_btn = gr.Button("🚀 Generate Exercise", variant="primary")
with gr.Column(scale=1):
textbook_output = gr.Textbox(label="Generated Exercise Material", lines=20, show_copy_button=True)
textbook_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
textbook_example_btn.click(
lambda: EXAMPLE_KNOWLEDGE_POINT,
outputs=[textbook_input],
)
textbook_btn.click(
textbook_exercise,
inputs=[textbook_input, textbook_diff],
outputs=[textbook_output, textbook_raw],
)
gr.HTML("""
<div class="footer-text">
<p>🔬 <strong>UltraData-Math L3 Generator</strong> - Part of the UltraData-Math Project</p>
<p>Powered by OpenBMB & ModelBest • <a href="https://huggingface.co/spaces/openbmb/UltraData-Math-L3-Generator" target="_blank" style="color: #818cf8; text-decoration: none;">View on Hugging Face</a></p>
</div>
""")
if __name__ == "__main__":
demo.launch(ssr_mode=False)
|