Spaces:
Running
on
Zero
Running
on
Zero
initial commit
Browse files- README.md +3 -3
- app.py +194 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
title: Moonshot Math
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.36.2
|
| 8 |
app_file: app.py
|
|
|
|
| 1 |
---
|
| 2 |
title: Moonshot Math
|
| 3 |
+
emoji: ๐๐๐จ๐ปโ๐ฌ
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.36.2
|
| 8 |
app_file: app.py
|
app.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import re
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 5 |
+
import torch
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
LEAN4_DEFAULT_HEADER = (
|
| 9 |
+
"import Mathlib\n"
|
| 10 |
+
"import Aesop\n\n"
|
| 11 |
+
"set_option maxHeartbeats 0\n\n"
|
| 12 |
+
"open BigOperators Real Nat Topology Rat\n"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
title = "# ๐๐ปโโ๏ธWelcome to ๐Tonic's ๐๐๐จ๐ปโ๐ฌMoonshot Math"
|
| 16 |
+
|
| 17 |
+
description = """
|
| 18 |
+
**Kimina-Prover-72B** is a state-of-the-art large formal reasoning model for Lean 4, achieving **80%+ pass rate** on the miniF2F benchmark, outperforming all prior works.\
|
| 19 |
+
Trained with Reinforcement Learning, 72B parameters, and a 32K token context window.\
|
| 20 |
+
- [Kimina-Prover-Preview GitHub](https://github.com/MoonshotAI/Kimina-Prover-Preview)\
|
| 21 |
+
- [Hugging Face: AI-MO/Kimina-Prover-72B](https://huggingface.co/AI-MO/Kimina-Prover-72B)\
|
| 22 |
+
- [Kimina Prover blog](https://huggingface.co/blog/AI-MO/kimina-prover)\
|
| 23 |
+
- [unimath dataset](https://huggingface.co/datasets/introspector/unimath)\
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
citation = """> **Citation:**
|
| 27 |
+
> ```
|
| 28 |
+
> @article{kimina_prover_2025,
|
| 29 |
+
> title = {Kimina-Prover Preview: Towards Large Formal Reasoning Models with Reinforcement Learning},
|
| 30 |
+
> author = {Wang, Haiming and Unsal, Mert and ...},
|
| 31 |
+
> year = {2025},
|
| 32 |
+
> url = {http://arxiv.org/abs/2504.11354},
|
| 33 |
+
> }
|
| 34 |
+
> ```
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
joinus ="""
|
| 39 |
+
### Join us:
|
| 40 |
+
๐TeamTonic๐ is always making cool demos! Join our active builder's ๐ ๏ธcommunity ๐ป
|
| 41 |
+
[](https://discord.gg/qdfnvSPcqP)
|
| 42 |
+
On ๐คHuggingface: [MultiTransformer](https://huggingface.co/MultiTransformer)
|
| 43 |
+
On ๐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to๐ [Build Tonic](https://git.tonic-ai.com/contribute)
|
| 44 |
+
๐คBig thanks to Yuvi Sharma and all the folks at huggingface for the community grant ๐ค
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
# Build the initial system prompt
|
| 48 |
+
SYSTEM_PROMPT = "You are an expert in mathematics and Lean 4."
|
| 49 |
+
|
| 50 |
+
# Helper to build a Lean4 code block
|
| 51 |
+
def build_formal_block(formal_statement, informal_prefix=""):
|
| 52 |
+
return (
|
| 53 |
+
f"{LEAN4_DEFAULT_HEADER}\n"
|
| 54 |
+
f"{informal_prefix}\n"
|
| 55 |
+
f"{formal_statement}"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Helper to extract the first Lean4 code block from text
|
| 59 |
+
def extract_lean4_code(text):
|
| 60 |
+
code_block = re.search(r"```lean4(.*?)(```|$)", text, re.DOTALL)
|
| 61 |
+
if code_block:
|
| 62 |
+
code = code_block.group(1)
|
| 63 |
+
lines = [line for line in code.split('\n') if line.strip()]
|
| 64 |
+
return '\n'.join(lines)
|
| 65 |
+
return text.strip()
|
| 66 |
+
|
| 67 |
+
# Example problems
|
| 68 |
+
unimath1 = """Goal:
|
| 69 |
+
X : UU
|
| 70 |
+
Y : UU
|
| 71 |
+
P : UU
|
| 72 |
+
xp : (X โ P) โ P
|
| 73 |
+
yp : (Y โ P) โ P
|
| 74 |
+
X0 : X ร Y โ P
|
| 75 |
+
x : X
|
| 76 |
+
============================
|
| 77 |
+
(Y โ P)"""
|
| 78 |
+
|
| 79 |
+
unimath2 = """Goal:
|
| 80 |
+
R : ring M : module R
|
| 81 |
+
============================
|
| 82 |
+
(islinear (idfun M))"""
|
| 83 |
+
|
| 84 |
+
unimath3 = """Goal:
|
| 85 |
+
X : UU i : nat b : hProptoType (i < S i) x : Vector X (S i) r : i = i
|
| 86 |
+
============================
|
| 87 |
+
(pr1 lastelement = pr1 (i,, b))"""
|
| 88 |
+
|
| 89 |
+
unimath4 = """Goal:
|
| 90 |
+
X : dcpo CX : continuous_dcpo_struct X x : pr1hSet X y : pr1hSet X
|
| 91 |
+
============================
|
| 92 |
+
(x โ y โ (โ i : approximating_family CX x, approximating_family CX x i โ y))"""
|
| 93 |
+
|
| 94 |
+
additional_info_prompt = "/-Explain using mathematics-/\n"
|
| 95 |
+
|
| 96 |
+
examples = [
|
| 97 |
+
[unimath1, additional_info_prompt, 2500],
|
| 98 |
+
[unimath2, additional_info_prompt, 2500],
|
| 99 |
+
[unimath3, additional_info_prompt, 2500],
|
| 100 |
+
[unimath4, additional_info_prompt, 2500]
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
model_name = "AI-MO/Kimina-Prover-72B"
|
| 104 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 105 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)
|
| 106 |
+
|
| 107 |
+
# Set generation config
|
| 108 |
+
model.generation_config = GenerationConfig.from_pretrained(model_name)
|
| 109 |
+
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
| 110 |
+
model.generation_config.do_sample = True
|
| 111 |
+
model.generation_config.temperature = 0.6
|
| 112 |
+
model.generation_config.top_p = 0.95
|
| 113 |
+
|
| 114 |
+
# Initialize chat history with system prompt
|
| 115 |
+
def init_chat(formal_statement, informal_prefix):
|
| 116 |
+
user_prompt = (
|
| 117 |
+
"Think about and solve the following problem step by step in Lean 4.\n"
|
| 118 |
+
"# Problem: Provide a formal proof for the following statement.\n"
|
| 119 |
+
f"# Formal statement:\n```lean4\n{build_formal_block(formal_statement, informal_prefix)}\n```\n"
|
| 120 |
+
)
|
| 121 |
+
return [
|
| 122 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 123 |
+
{"role": "user", "content": user_prompt}
|
| 124 |
+
]
|
| 125 |
+
|
| 126 |
+
# Gradio chat handler
|
| 127 |
+
@spaces.GPU
|
| 128 |
+
def chat_handler(user_message, informal_prefix, max_tokens, chat_history):
|
| 129 |
+
# If chat_history is empty, initialize with system and first user message
|
| 130 |
+
if not chat_history or len(chat_history) < 2:
|
| 131 |
+
chat_history = init_chat(user_message, informal_prefix)
|
| 132 |
+
display_history = [("user", user_message)]
|
| 133 |
+
else:
|
| 134 |
+
# Append new user message
|
| 135 |
+
chat_history.append({"role": "user", "content": user_message})
|
| 136 |
+
display_history = []
|
| 137 |
+
for msg in chat_history:
|
| 138 |
+
if msg["role"] == "user":
|
| 139 |
+
display_history.append(("user", msg["content"]))
|
| 140 |
+
elif msg["role"] == "assistant":
|
| 141 |
+
display_history.append(("assistant", msg["content"]))
|
| 142 |
+
# Format prompt using chat template
|
| 143 |
+
prompt = tokenizer.apply_chat_template(chat_history, tokenize=False, add_generation_prompt=True)
|
| 144 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
|
| 145 |
+
attention_mask = torch.ones_like(input_ids)
|
| 146 |
+
outputs = model.generate(
|
| 147 |
+
input_ids,
|
| 148 |
+
attention_mask=attention_mask,
|
| 149 |
+
max_length=max_tokens + input_ids.shape[1],
|
| 150 |
+
pad_token_id=model.generation_config.pad_token_id,
|
| 151 |
+
temperature=model.generation_config.temperature,
|
| 152 |
+
top_p=model.generation_config.top_p,
|
| 153 |
+
)
|
| 154 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 155 |
+
# Extract only the new assistant message (after the prompt)
|
| 156 |
+
new_response = result[len(prompt):].strip()
|
| 157 |
+
# Add assistant message to chat history
|
| 158 |
+
chat_history.append({"role": "assistant", "content": new_response})
|
| 159 |
+
display_history.append(("assistant", new_response))
|
| 160 |
+
# Extract Lean4 code
|
| 161 |
+
code = extract_lean4_code(new_response)
|
| 162 |
+
# Prepare output
|
| 163 |
+
output_data = {
|
| 164 |
+
"model_input": prompt,
|
| 165 |
+
"model_output": result,
|
| 166 |
+
"lean4_code": code,
|
| 167 |
+
"chat_history": chat_history
|
| 168 |
+
}
|
| 169 |
+
return display_history, json.dumps(output_data, indent=2), code, chat_history
|
| 170 |
+
|
| 171 |
+
def main():
|
| 172 |
+
with gr.Blocks() as demo:
|
| 173 |
+
# Title and Model Description
|
| 174 |
+
gr.Markdown("""# ๐๐ปโโ๏ธWelcome to ๐Tonic's ๐๐๐จ๐ปโ๐ฌMoonshot Math""")
|
| 175 |
+
gr.Markdown(description)
|
| 176 |
+
gr.Markdown(joinus)
|
| 177 |
+
with gr.Row():
|
| 178 |
+
with gr.Column():
|
| 179 |
+
chat = gr.Chatbot(label="Chat History")
|
| 180 |
+
user_input = gr.Textbox(label="Your message or formal statement", lines=4)
|
| 181 |
+
informal = gr.Textbox(value=additional_info_prompt, label="Optional informal prefix")
|
| 182 |
+
max_tokens = gr.Slider(minimum=150, maximum=4096, value=2500, label="Max Tokens")
|
| 183 |
+
submit = gr.Button("Send")
|
| 184 |
+
with gr.Column():
|
| 185 |
+
json_out = gr.JSON(label="Full Output")
|
| 186 |
+
code_out = gr.Code(label="Extracted Lean4 Code", language="lean4")
|
| 187 |
+
state = gr.State([])
|
| 188 |
+
# On submit, call chat_handler
|
| 189 |
+
submit.click(chat_handler, [user_input, informal, max_tokens, state], [chat, json_out, code_out, state])
|
| 190 |
+
gr.Markdown(citation)
|
| 191 |
+
demo.launch()
|
| 192 |
+
|
| 193 |
+
if __name__ == "__main__":
|
| 194 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
accelerate
|