ZhouChuYue commited on
Commit
af51047
ยท
1 Parent(s): e22c119

feat: hide model selector, add example buttons for one-click demo

Browse files
Files changed (2) hide show
  1. README.md +1 -0
  2. app.py +79 -36
README.md CHANGED
@@ -5,6 +5,7 @@ colorFrom: purple
5
  colorTo: blue
6
  sdk: gradio
7
  sdk_version: 4.44.0
 
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
5
  colorTo: blue
6
  sdk: gradio
7
  sdk_version: 4.44.0
8
+ python_version: 3.10
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
app.py CHANGED
@@ -5,7 +5,6 @@ UltraData-Math L3 Generator - Hugging Face Space Demo
5
 
6
  import os
7
  import asyncio
8
- import json
9
  import gradio as gr
10
 
11
  from openai import AsyncOpenAI
@@ -31,8 +30,29 @@ API_KEY = os.getenv("OPENAI_API_KEY")
31
  BASE_URL = os.getenv("OPENAI_BASE_URL", "https://llm-center.ali.modelbest.cn/llm/openai/v1")
32
  DEFAULT_MODEL = "GLM_ar7snd"
33
 
 
 
34
 
35
- async def call_api(prompt: str, model: str = DEFAULT_MODEL, temperature: float = 0.7) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  """่ฐƒ็”จ API ็”Ÿๆˆๅ†…ๅฎน"""
37
  if not API_KEY:
38
  return "Error: API Key not configured. Please contact administrator."
@@ -40,7 +60,7 @@ async def call_api(prompt: str, model: str = DEFAULT_MODEL, temperature: float =
40
  client = AsyncOpenAI(api_key=API_KEY, base_url=BASE_URL)
41
  try:
42
  response = await client.chat.completions.create(
43
- model=model,
44
  messages=[{"role": "user", "content": prompt}],
45
  temperature=temperature,
46
  max_tokens=8192,
@@ -70,7 +90,7 @@ def run_async(coro):
70
  # Task Handlers
71
  # ============================================================================
72
 
73
- def qa_synthesis(text: str, level: str, model: str, temperature: float):
74
  """Q&A ้—ฎ็ญ”ๅฏนๅˆๆˆ"""
75
  if not text.strip():
76
  return "", "", ""
@@ -78,7 +98,7 @@ def qa_synthesis(text: str, level: str, model: str, temperature: float):
78
  prompt_template = get_qa_prompt(level)
79
  prompt = prompt_template.format(text=text)
80
 
81
- response = run_async(call_api(prompt, model, temperature))
82
  parsed = parse_qa_output(response)
83
 
84
  return (
@@ -88,7 +108,7 @@ def qa_synthesis(text: str, level: str, model: str, temperature: float):
88
  )
89
 
90
 
91
- def conversation_synthesis(text: str, style: str, model: str, temperature: float):
92
  """ๅคš่ฝฎๅฏน่ฏๅˆๆˆ"""
93
  if not text.strip():
94
  return "", ""
@@ -96,13 +116,13 @@ def conversation_synthesis(text: str, style: str, model: str, temperature: float
96
  prompt_template = get_conversation_prompt(style)
97
  prompt = prompt_template.format(text=text)
98
 
99
- response = run_async(call_api(prompt, model, temperature))
100
  parsed = parse_conversation_output(response)
101
 
102
  return parsed.get("content", response), response
103
 
104
 
105
- def rewrite_synthesis(text: str, style: str, model: str, temperature: float):
106
  """ๅคš้ฃŽๆ ผๆ”นๅ†™"""
107
  if not text.strip():
108
  return "", ""
@@ -110,13 +130,13 @@ def rewrite_synthesis(text: str, style: str, model: str, temperature: float):
110
  prompt_template = get_multistyle_prompt(style)
111
  prompt = prompt_template.format(text=text)
112
 
113
- response = run_async(call_api(prompt, model, temperature))
114
  parsed = parse_rewrite_output(response)
115
 
116
  return parsed.get("rewritten", response), response
117
 
118
 
119
- def knowledge_extraction(text: str, model: str, temperature: float):
120
  """็Ÿฅ่ฏ†็‚นๆๅ–"""
121
  if not text.strip():
122
  return "", ""
@@ -124,7 +144,7 @@ def knowledge_extraction(text: str, model: str, temperature: float):
124
  prompt_template = get_knowledge_extraction_prompt()
125
  prompt = prompt_template.format(text=text)
126
 
127
- response = run_async(call_api(prompt, model, temperature))
128
  parsed = parse_knowledge_output(response)
129
 
130
  knowledge_points = parsed.get("knowledge_points", [])
@@ -133,7 +153,7 @@ def knowledge_extraction(text: str, model: str, temperature: float):
133
  return formatted, response
134
 
135
 
136
- def textbook_exercise(knowledge_point: str, difficulty: str, model: str, temperature: float):
137
  """ๆ•™ๆ็ปƒไน ็”Ÿๆˆ"""
138
  if not knowledge_point.strip():
139
  return "", ""
@@ -141,7 +161,7 @@ def textbook_exercise(knowledge_point: str, difficulty: str, model: str, tempera
141
  prompt_template = get_textbook_exercise_prompt(difficulty)
142
  prompt = prompt_template.format(mathematical_knowledge_point=knowledge_point)
143
 
144
- response = run_async(call_api(prompt, model, temperature))
145
  parsed = parse_textbook_output(response)
146
 
147
  return parsed.get("material", response), response
@@ -185,6 +205,12 @@ custom_css = """
185
  box-shadow: 0 8px 25px rgba(233, 69, 96, 0.4) !important;
186
  }
187
 
 
 
 
 
 
 
188
  footer {
189
  display: none !important;
190
  }
@@ -194,19 +220,6 @@ with gr.Blocks(title="UltraData-Math L3 Generator", css=custom_css) as demo:
194
  gr.HTML('<h1 class="main-title">๐Ÿงฎ UltraData-Math L3 Generator</h1>')
195
  gr.HTML('<p class="subtitle">LLM-based Mathematical Data Synthesis Tool</p>')
196
 
197
- with gr.Row():
198
- model_select = gr.Dropdown(
199
- choices=["GLM_ar7snd", "GLM_pq0dvd", "GLM_35a7cn", "QWEN_czrd3t", "DEEPSEEK_5jcwxs"],
200
- value="GLM_ar7snd",
201
- label="Model",
202
- scale=1,
203
- )
204
- temperature = gr.Slider(
205
- minimum=0.0, maximum=1.5, value=0.7, step=0.1,
206
- label="Temperature",
207
- scale=1,
208
- )
209
-
210
  with gr.Tabs():
211
  # Q&A Synthesis Tab
212
  with gr.TabItem("๐Ÿ“ Q&A Synthesis"):
@@ -223,15 +236,21 @@ with gr.Blocks(title="UltraData-Math L3 Generator", css=custom_css) as demo:
223
  value="high_school",
224
  label="Difficulty Level",
225
  )
226
- qa_btn = gr.Button("๐Ÿš€ Generate Q&A", variant="primary")
 
 
227
  with gr.Column():
228
  qa_problem = gr.Textbox(label="Generated Problem", lines=4)
229
  qa_solution = gr.Textbox(label="Generated Solution", lines=8)
230
  qa_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
231
 
 
 
 
 
232
  qa_btn.click(
233
  qa_synthesis,
234
- inputs=[qa_input, qa_level, model_select, temperature],
235
  outputs=[qa_problem, qa_solution, qa_raw],
236
  )
237
 
@@ -250,14 +269,20 @@ with gr.Blocks(title="UltraData-Math L3 Generator", css=custom_css) as demo:
250
  value="teacher_student",
251
  label="Conversation Style",
252
  )
253
- conv_btn = gr.Button("๐Ÿš€ Generate Conversation", variant="primary")
 
 
254
  with gr.Column():
255
  conv_output = gr.Textbox(label="Generated Conversation", lines=15)
256
  conv_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
257
 
 
 
 
 
258
  conv_btn.click(
259
  conversation_synthesis,
260
- inputs=[conv_input, conv_style, model_select, temperature],
261
  outputs=[conv_output, conv_raw],
262
  )
263
 
@@ -276,14 +301,20 @@ with gr.Blocks(title="UltraData-Math L3 Generator", css=custom_css) as demo:
276
  value="textbook",
277
  label="Rewrite Style",
278
  )
279
- rewrite_btn = gr.Button("๐Ÿš€ Rewrite", variant="primary")
 
 
280
  with gr.Column():
281
  rewrite_output = gr.Textbox(label="Rewritten Content", lines=15)
282
  rewrite_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
283
 
 
 
 
 
284
  rewrite_btn.click(
285
  rewrite_synthesis,
286
- inputs=[rewrite_input, rewrite_style, model_select, temperature],
287
  outputs=[rewrite_output, rewrite_raw],
288
  )
289
 
@@ -297,14 +328,20 @@ with gr.Blocks(title="UltraData-Math L3 Generator", css=custom_css) as demo:
297
  placeholder="Enter mathematical content here...",
298
  lines=10,
299
  )
300
- know_btn = gr.Button("๐Ÿš€ Extract Knowledge", variant="primary")
 
 
301
  with gr.Column():
302
  know_output = gr.Textbox(label="Extracted Knowledge Points", lines=15)
303
  know_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
304
 
 
 
 
 
305
  know_btn.click(
306
  knowledge_extraction,
307
- inputs=[know_input, model_select, temperature],
308
  outputs=[know_output, know_raw],
309
  )
310
 
@@ -323,14 +360,20 @@ with gr.Blocks(title="UltraData-Math L3 Generator", css=custom_css) as demo:
323
  value="easy",
324
  label="Difficulty",
325
  )
326
- textbook_btn = gr.Button("๐Ÿš€ Generate Exercise", variant="primary")
 
 
327
  with gr.Column():
328
  textbook_output = gr.Textbox(label="Generated Exercise Material", lines=15)
329
  textbook_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
330
 
 
 
 
 
331
  textbook_btn.click(
332
  textbook_exercise,
333
- inputs=[textbook_input, textbook_diff, model_select, temperature],
334
  outputs=[textbook_output, textbook_raw],
335
  )
336
 
 
5
 
6
  import os
7
  import asyncio
 
8
  import gradio as gr
9
 
10
  from openai import AsyncOpenAI
 
30
  BASE_URL = os.getenv("OPENAI_BASE_URL", "https://llm-center.ali.modelbest.cn/llm/openai/v1")
31
  DEFAULT_MODEL = "GLM_ar7snd"
32
 
33
+ # ็คบไพ‹ๆ•ฐๆฎ
34
+ 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.
35
 
36
+ The formula states that the solutions are:
37
+ x = (-b ยฑ โˆš(bยฒ - 4ac)) / (2a)
38
+
39
+ The term bยฒ - 4ac is called the discriminant. It determines the nature of the roots:
40
+ - If bยฒ - 4ac > 0, there are two distinct real roots
41
+ - If bยฒ - 4ac = 0, there is exactly one real root (a repeated root)
42
+ - If bยฒ - 4ac < 0, there are two complex conjugate roots
43
+
44
+ This formula was known to ancient mathematicians and remains one of the most important tools in solving polynomial equations."""
45
+
46
+ 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โ‚€| < ฮด.
47
+
48
+ Key Properties:
49
+ 1. The sum, difference, and product of continuous functions are continuous
50
+ 2. The composition of continuous functions is continuous
51
+ 3. A continuous function on a closed interval attains its maximum and minimum values (Extreme Value Theorem)
52
+ 4. A continuous function on a closed interval takes on every value between its minimum and maximum (Intermediate Value Theorem)"""
53
+
54
+
55
+ async def call_api(prompt: str, temperature: float = 0.7) -> str:
56
  """่ฐƒ็”จ API ็”Ÿๆˆๅ†…ๅฎน"""
57
  if not API_KEY:
58
  return "Error: API Key not configured. Please contact administrator."
 
60
  client = AsyncOpenAI(api_key=API_KEY, base_url=BASE_URL)
61
  try:
62
  response = await client.chat.completions.create(
63
+ model=DEFAULT_MODEL,
64
  messages=[{"role": "user", "content": prompt}],
65
  temperature=temperature,
66
  max_tokens=8192,
 
90
  # Task Handlers
91
  # ============================================================================
92
 
93
+ def qa_synthesis(text: str, level: str):
94
  """Q&A ้—ฎ็ญ”ๅฏนๅˆๆˆ"""
95
  if not text.strip():
96
  return "", "", ""
 
98
  prompt_template = get_qa_prompt(level)
99
  prompt = prompt_template.format(text=text)
100
 
101
+ response = run_async(call_api(prompt))
102
  parsed = parse_qa_output(response)
103
 
104
  return (
 
108
  )
109
 
110
 
111
+ def conversation_synthesis(text: str, style: str):
112
  """ๅคš่ฝฎๅฏน่ฏๅˆๆˆ"""
113
  if not text.strip():
114
  return "", ""
 
116
  prompt_template = get_conversation_prompt(style)
117
  prompt = prompt_template.format(text=text)
118
 
119
+ response = run_async(call_api(prompt))
120
  parsed = parse_conversation_output(response)
121
 
122
  return parsed.get("content", response), response
123
 
124
 
125
+ def rewrite_synthesis(text: str, style: str):
126
  """ๅคš้ฃŽๆ ผๆ”นๅ†™"""
127
  if not text.strip():
128
  return "", ""
 
130
  prompt_template = get_multistyle_prompt(style)
131
  prompt = prompt_template.format(text=text)
132
 
133
+ response = run_async(call_api(prompt))
134
  parsed = parse_rewrite_output(response)
135
 
136
  return parsed.get("rewritten", response), response
137
 
138
 
139
+ def knowledge_extraction(text: str):
140
  """็Ÿฅ่ฏ†็‚นๆๅ–"""
141
  if not text.strip():
142
  return "", ""
 
144
  prompt_template = get_knowledge_extraction_prompt()
145
  prompt = prompt_template.format(text=text)
146
 
147
+ response = run_async(call_api(prompt))
148
  parsed = parse_knowledge_output(response)
149
 
150
  knowledge_points = parsed.get("knowledge_points", [])
 
153
  return formatted, response
154
 
155
 
156
+ def textbook_exercise(knowledge_point: str, difficulty: str):
157
  """ๆ•™ๆ็ปƒไน ็”Ÿๆˆ"""
158
  if not knowledge_point.strip():
159
  return "", ""
 
161
  prompt_template = get_textbook_exercise_prompt(difficulty)
162
  prompt = prompt_template.format(mathematical_knowledge_point=knowledge_point)
163
 
164
+ response = run_async(call_api(prompt))
165
  parsed = parse_textbook_output(response)
166
 
167
  return parsed.get("material", response), response
 
205
  box-shadow: 0 8px 25px rgba(233, 69, 96, 0.4) !important;
206
  }
207
 
208
+ .gr-button-secondary {
209
+ background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%) !important;
210
+ border: none !important;
211
+ color: white !important;
212
+ }
213
+
214
  footer {
215
  display: none !important;
216
  }
 
220
  gr.HTML('<h1 class="main-title">๐Ÿงฎ UltraData-Math L3 Generator</h1>')
221
  gr.HTML('<p class="subtitle">LLM-based Mathematical Data Synthesis Tool</p>')
222
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  with gr.Tabs():
224
  # Q&A Synthesis Tab
225
  with gr.TabItem("๐Ÿ“ Q&A Synthesis"):
 
236
  value="high_school",
237
  label="Difficulty Level",
238
  )
239
+ with gr.Row():
240
+ qa_example_btn = gr.Button("๐Ÿ“‹ Load Example", variant="secondary")
241
+ qa_btn = gr.Button("๐Ÿš€ Generate Q&A", variant="primary")
242
  with gr.Column():
243
  qa_problem = gr.Textbox(label="Generated Problem", lines=4)
244
  qa_solution = gr.Textbox(label="Generated Solution", lines=8)
245
  qa_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
246
 
247
+ qa_example_btn.click(
248
+ lambda: EXAMPLE_MATH_CONTENT,
249
+ outputs=[qa_input],
250
+ )
251
  qa_btn.click(
252
  qa_synthesis,
253
+ inputs=[qa_input, qa_level],
254
  outputs=[qa_problem, qa_solution, qa_raw],
255
  )
256
 
 
269
  value="teacher_student",
270
  label="Conversation Style",
271
  )
272
+ with gr.Row():
273
+ conv_example_btn = gr.Button("๐Ÿ“‹ Load Example", variant="secondary")
274
+ conv_btn = gr.Button("๐Ÿš€ Generate Conversation", variant="primary")
275
  with gr.Column():
276
  conv_output = gr.Textbox(label="Generated Conversation", lines=15)
277
  conv_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
278
 
279
+ conv_example_btn.click(
280
+ lambda: EXAMPLE_MATH_CONTENT,
281
+ outputs=[conv_input],
282
+ )
283
  conv_btn.click(
284
  conversation_synthesis,
285
+ inputs=[conv_input, conv_style],
286
  outputs=[conv_output, conv_raw],
287
  )
288
 
 
301
  value="textbook",
302
  label="Rewrite Style",
303
  )
304
+ with gr.Row():
305
+ rewrite_example_btn = gr.Button("๐Ÿ“‹ Load Example", variant="secondary")
306
+ rewrite_btn = gr.Button("๐Ÿš€ Rewrite", variant="primary")
307
  with gr.Column():
308
  rewrite_output = gr.Textbox(label="Rewritten Content", lines=15)
309
  rewrite_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
310
 
311
+ rewrite_example_btn.click(
312
+ lambda: EXAMPLE_MATH_CONTENT,
313
+ outputs=[rewrite_input],
314
+ )
315
  rewrite_btn.click(
316
  rewrite_synthesis,
317
+ inputs=[rewrite_input, rewrite_style],
318
  outputs=[rewrite_output, rewrite_raw],
319
  )
320
 
 
328
  placeholder="Enter mathematical content here...",
329
  lines=10,
330
  )
331
+ with gr.Row():
332
+ know_example_btn = gr.Button("๐Ÿ“‹ Load Example", variant="secondary")
333
+ know_btn = gr.Button("๐Ÿš€ Extract Knowledge", variant="primary")
334
  with gr.Column():
335
  know_output = gr.Textbox(label="Extracted Knowledge Points", lines=15)
336
  know_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
337
 
338
+ know_example_btn.click(
339
+ lambda: EXAMPLE_MATH_CONTENT,
340
+ outputs=[know_input],
341
+ )
342
  know_btn.click(
343
  knowledge_extraction,
344
+ inputs=[know_input],
345
  outputs=[know_output, know_raw],
346
  )
347
 
 
360
  value="easy",
361
  label="Difficulty",
362
  )
363
+ with gr.Row():
364
+ textbook_example_btn = gr.Button("๐Ÿ“‹ Load Example", variant="secondary")
365
+ textbook_btn = gr.Button("๐Ÿš€ Generate Exercise", variant="primary")
366
  with gr.Column():
367
  textbook_output = gr.Textbox(label="Generated Exercise Material", lines=15)
368
  textbook_raw = gr.Textbox(label="Raw Response", lines=4, visible=False)
369
 
370
+ textbook_example_btn.click(
371
+ lambda: EXAMPLE_KNOWLEDGE_POINT,
372
+ outputs=[textbook_input],
373
+ )
374
  textbook_btn.click(
375
  textbook_exercise,
376
+ inputs=[textbook_input, textbook_diff],
377
  outputs=[textbook_output, textbook_raw],
378
  )
379