Upload folder using huggingface_hub
Browse files- README.md +2 -8
- herald_gui.py +167 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: green
|
| 5 |
-
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.9.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: herald
|
| 3 |
+
app_file: herald_gui.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 5.9.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
herald_gui.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import traceback
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from validator import validate
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
|
| 7 |
+
# CSS for styling
|
| 8 |
+
css = """
|
| 9 |
+
#search_button {background-color: #FC7B4C; /* Default color for light mode */}
|
| 10 |
+
@media (prefers-color-scheme: dark) {#search_button {background-color: #C45127; /* dark mode */}}
|
| 11 |
+
|
| 12 |
+
.footer {
|
| 13 |
+
position: fixed;
|
| 14 |
+
bottom: 0;
|
| 15 |
+
left: 0;
|
| 16 |
+
width: 100vw;
|
| 17 |
+
box-sizing: border-box;
|
| 18 |
+
text-align: center;
|
| 19 |
+
background-color: white;
|
| 20 |
+
color: black;
|
| 21 |
+
font-size: 12px;
|
| 22 |
+
padding: 10px;
|
| 23 |
+
border-top: 1px solid #ccc;
|
| 24 |
+
z-index: 1000;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
@media (prefers-color-scheme: dark) {
|
| 28 |
+
.footer {
|
| 29 |
+
background-color: #333;
|
| 30 |
+
color: #ccc;
|
| 31 |
+
border-top: 1px solid #444;
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
"""
|
| 35 |
+
footer_html = """
|
| 36 |
+
<div class='footer'>
|
| 37 |
+
AI4M Team, BICMR@PKU
|
| 38 |
+
</div>
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
def translate(informal_statement, num_passes):
|
| 42 |
+
name='textbook_exercise'
|
| 43 |
+
max_tokens=1024
|
| 44 |
+
temperature=0.99
|
| 45 |
+
|
| 46 |
+
client = OpenAI(
|
| 47 |
+
base_url="https://console.siflow.cn/siflow/draco/ai4math/ytwang/herald-1/v1",
|
| 48 |
+
api_key="herald-cool"
|
| 49 |
+
)
|
| 50 |
+
template = """Please translate the natural language statement to Lean4 code with the header.
|
| 51 |
+
**Name**
|
| 52 |
+
{name}
|
| 53 |
+
**Informal statement**
|
| 54 |
+
{informal_statement}
|
| 55 |
+
"""
|
| 56 |
+
messages = [
|
| 57 |
+
{'role': 'system', 'content': 'You are an expert at Lean 4 and Mathematics.'},
|
| 58 |
+
{'role': 'user', 'content': template.format(
|
| 59 |
+
name=name,
|
| 60 |
+
informal_statement=informal_statement)}
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
completion = client.chat.completions.create(
|
| 64 |
+
messages=messages,
|
| 65 |
+
model="/AI4M/users/ytwang/LLaMA-Factory/saves/deepseek_7b_flash_math_formal5_resume/full/sft/",
|
| 66 |
+
max_tokens=max_tokens,
|
| 67 |
+
temperature=temperature,
|
| 68 |
+
n=num_passes,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
generated = [output.message.content for output in completion.choices]
|
| 72 |
+
for gen in generated:
|
| 73 |
+
print(gen)
|
| 74 |
+
return generated
|
| 75 |
+
|
| 76 |
+
def translate_inv(formal_statement, num_passes):
|
| 77 |
+
max_tokens=1024
|
| 78 |
+
temperature=0
|
| 79 |
+
client = OpenAI(
|
| 80 |
+
base_url="https://console.siflow.cn/siflow/draco/ai4math/ytwang/herald-1/v1",
|
| 81 |
+
api_key="herald-cool"
|
| 82 |
+
)
|
| 83 |
+
template = """Please translate the Lean4 code back to natural language.
|
| 84 |
+
**Formal statement**
|
| 85 |
+
{formal_statement}
|
| 86 |
+
"""
|
| 87 |
+
messages = [
|
| 88 |
+
{'role': 'system', 'content': 'You are an expert at Lean 4 and Mathematics.'},
|
| 89 |
+
{'role': 'user', 'content': template.format(
|
| 90 |
+
formal_statement=formal_statement)}
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
completion = client.chat.completions.create(
|
| 94 |
+
messages=messages,
|
| 95 |
+
model="/AI4M/users/ytwang/LLaMA-Factory/saves/deepseek_7b_flash_math_formal5_resume/full/sft/",
|
| 96 |
+
max_tokens=max_tokens,
|
| 97 |
+
temperature=temperature,
|
| 98 |
+
n=num_passes,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
generated = [output.message.content for output in completion.choices]
|
| 102 |
+
for gen in generated:
|
| 103 |
+
print(gen)
|
| 104 |
+
return generated
|
| 105 |
+
|
| 106 |
+
def translate_for_gradio(informal_statement, num_passes):
|
| 107 |
+
import pandas as pd
|
| 108 |
+
generated = translate(informal_statement, num_passes)
|
| 109 |
+
generated = [f"```lean\n{gen}\n```" for gen in generated]
|
| 110 |
+
df = pd.DataFrame(generated, columns=['Translated Formal Statement'])
|
| 111 |
+
df.insert(0, 'Rank', range(1, len(df) + 1))
|
| 112 |
+
return df
|
| 113 |
+
|
| 114 |
+
def translate_inv_for_gradio(formal_statement, num_passes):
|
| 115 |
+
import pandas as pd
|
| 116 |
+
generated = translate_inv(formal_statement, num_passes)
|
| 117 |
+
generated = [f"```lean\n{gen}\n```" for gen in generated]
|
| 118 |
+
df = pd.DataFrame(generated, columns=['Translated Natural Language'])
|
| 119 |
+
df.insert(0, 'Rank', range(1, len(df) + 1))
|
| 120 |
+
return df
|
| 121 |
+
|
| 122 |
+
def grmain():
|
| 123 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Lean 4 Translator", css=css) as demo:
|
| 124 |
+
gr.Markdown("""
|
| 125 |
+
<center>
|
| 126 |
+
<font size=8> Herald Translator </font><br>
|
| 127 |
+
<font size=4> Autoformalize your statement using our latest model</font>
|
| 128 |
+
</center>
|
| 129 |
+
""")
|
| 130 |
+
with gr.Tab("Formalize"):
|
| 131 |
+
input_text = gr.Textbox(placeholder="Example: The infinite sum of the sequence `1/n` is not convergent.", label="Query", info="Enter a natural language query")
|
| 132 |
+
num_passes = gr.Slider(
|
| 133 |
+
minimum=1, maximum=128, value=16, step=16, label="Number of passes", info=f"Choose between 1 and 128")
|
| 134 |
+
#num_results = gr.Slider(
|
| 135 |
+
# minimum=1, maximum=20, value=10, step=1, label="Number of results", info=f"Choose between 1 and {20}")
|
| 136 |
+
with gr.Row():
|
| 137 |
+
clear_button = gr.Button("Clear", elem_id="clear")
|
| 138 |
+
translate_button = gr.Button("Translate", elem_id="translate_button")
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
with gr.Tab("Informalize"):
|
| 142 |
+
input_text_inv = gr.Textbox(placeholder="Example: theorem not_summable_one_div_natCast : ¬Summable (fun n => 1 / n : ℕ → ℝ) := sorry", label="Formal Statement", info="Enter a formal statement")
|
| 143 |
+
num_passes_inv = gr.Slider(
|
| 144 |
+
minimum=1, maximum=128, value=8, step=8, label="Number of passes", info=f"Choose between 1 and 128")
|
| 145 |
+
with gr.Row():
|
| 146 |
+
clear_button_inv = gr.Button("Clear", elem_id="clear_inv")
|
| 147 |
+
translate_button_inv = gr.Button("Translate", elem_id="translate_button_inv")
|
| 148 |
+
|
| 149 |
+
output = gr.DataFrame(datatype="markdown")
|
| 150 |
+
gr.HTML(footer_html)
|
| 151 |
+
clear_button.click(lambda: [None]*2, outputs=[input_text, output])
|
| 152 |
+
translate_button.click(fn=translate_for_gradio, inputs=[input_text, num_passes], outputs=output)
|
| 153 |
+
translate_button_inv.click(fn=translate_inv_for_gradio, inputs=[input_text_inv, num_passes_inv], outputs=output)
|
| 154 |
+
# input_text.submit(fn=translate_for_gradio, inputs=[input_text, num_passes], outputs=output)
|
| 155 |
+
|
| 156 |
+
demo.launch(server_name="0.0.0.0", server_port=9062, share = True)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
+
informal_statement_easy = "The infinite sum of the sequence `1/n` is not convergent."
|
| 161 |
+
informal_statement_medium = "Prove that if $A$ and $B$ are subsets of group $G$ with $A \subset B$ then $C_G(B)$ is a subgroup of $C_G(A)$."
|
| 162 |
+
informal_statement_hard = "Let $R$ be a subring of the commutative ring $S$ with $1 \in R$ and let $s \in S$. $s$ is integral over $R$ if and only if $R[s]$ is a finitely generated $R$-module (where $R[s]$ is the ring of all $R$-linear combinations of powers of $s$)."
|
| 163 |
+
informal_statement_extreme = "Let \(alpha \in R, K=\mathbb{Q}[alpha]\), and let \(f\) be any monic polynomial over \(\mathbb{Z}\) such that \(f(alpha)=0\). If \(p\) is a prime such that \(p nmid \mathrm{N}_{K} f^{\prime}(alpha)\), then \(p\) is unramified in K."
|
| 164 |
+
# translate(informal_statement_easy, num_passes=16)
|
| 165 |
+
# # print(informal_statement_extreme)
|
| 166 |
+
# main(informal_statement_easy, num_passes=32)
|
| 167 |
+
grmain()
|