ZhouChuYue
feat: UI overhaul with glassmorphism and modern styling
1d93c0f
raw
history blame
19.2 kB
# -*- 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 = "GLM_ar7snd"
# 示例数据
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 {
--primary-color: #6366f1;
--secondary-color: #ec4899;
--background-dark: #0f172a;
--panel-bg: rgba(30, 41, 59, 0.7);
--text-light: #f8fafc;
--text-gray: #94a3b8;
}
body {
background-color: var(--background-dark);
color: var(--text-light);
}
.gradio-container {
font-family: 'Inter', sans-serif !important;
background: radial-gradient(circle at top left, #1e1b4b, #0f172a) !important;
max-width: 1200px !important;
}
/* Title & Header */
.main-title {
font-family: 'Inter', sans-serif !important;
font-weight: 800 !important;
font-size: 3rem !important;
background: linear-gradient(to right, #818cf8, #c084fc, #f472b6) !important;
-webkit-background-clip: text !important;
-webkit-text-fill-color: transparent !important;
text-align: center !important;
margin-bottom: 0.5rem !important;
letter-spacing: -0.02em !important;
}
.subtitle {
text-align: center !important;
color: var(--text-gray) !important;
font-size: 1.1rem !important;
margin-bottom: 3rem !important;
font-weight: 300 !important;
}
/* Tabs */
.tabs {
border: none !important;
background: transparent !important;
margin-bottom: 2rem !important;
}
.tab-nav {
border-bottom: 1px solid rgba(148, 163, 184, 0.2) !important;
justify-content: center !important;
}
.tab-nav button {
font-weight: 600 !important;
font-size: 1rem !important;
color: var(--text-gray) !important;
transition: all 0.3s ease !important;
}
.tab-nav button.selected {
color: #818cf8 !important;
border-bottom: 2px solid #818cf8 !important;
}
/* Panels & Cards */
.glass-panel {
background: var(--panel-bg) !important;
backdrop-filter: blur(12px) !important;
border: 1px solid rgba(255, 255, 255, 0.1) !important;
border-radius: 16px !important;
padding: 24px !important;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06) !important;
}
/* Inputs */
textarea, input {
font-family: 'JetBrains Mono', monospace !important;
background: rgba(15, 23, 42, 0.6) !important;
border: 1px solid rgba(148, 163, 184, 0.2) !important;
color: var(--text-light) !important;
border-radius: 8px !important;
transition: border-color 0.2s !important;
}
textarea:focus, input:focus {
border-color: var(--primary-color) !important;
box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2) !important;
}
/* Buttons */
.gr-button-primary {
background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%) !important;
border: none !important;
color: white !important;
font-weight: 600 !important;
border-radius: 8px !important;
transition: transform 0.2s, box-shadow 0.2s !important;
}
.gr-button-primary:hover {
transform: translateY(-1px) !important;
box-shadow: 0 10px 15px -3px rgba(99, 102, 241, 0.3) !important;
}
.gr-button-secondary {
background: rgba(30, 41, 59, 0.8) !important;
border: 1px solid rgba(148, 163, 184, 0.3) !important;
color: var(--text-gray) !important;
border-radius: 8px !important;
}
.gr-button-secondary:hover {
background: rgba(51, 65, 85, 0.8) !important;
color: var(--text-light) !important;
}
/* Labels */
label span {
color: #cbd5e1 !important;
font-weight: 500 !important;
font-size: 0.9rem !important;
margin-bottom: 0.5rem !important;
}
/* Footer */
.footer-text {
text-align: center;
margin-top: 4rem;
padding: 2rem;
color: var(--text-gray);
font-size: 0.9rem;
border-top: 1px solid rgba(148, 163, 184, 0.1);
}
"""
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)