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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chromadb
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
# 初始化集合
|
| 9 |
+
client = chromadb.PersistentClient(path="chroma_db2")
|
| 10 |
+
output_collection = client.get_collection("data_collection")
|
| 11 |
+
|
| 12 |
+
def remove1(latex_str):
|
| 13 |
+
# 定义需要移除的命令的正则表达式
|
| 14 |
+
unsupported_commands = [
|
| 15 |
+
r'\\ref\{([^}]+)\}', # 匹配 \ref{...},保留括号内容
|
| 16 |
+
r'\\label\{([^}]+)\}', # 匹配 \label{...},保留括号内容
|
| 17 |
+
r'\\Ob\(([^)]+)\)',
|
| 18 |
+
]
|
| 19 |
+
latex_str=latex_str.replace("\\\\","\\")
|
| 20 |
+
# 逐个移除不支持的命令
|
| 21 |
+
for pattern in unsupported_commands:
|
| 22 |
+
latex_str = re.sub(pattern, r'\1', latex_str)
|
| 23 |
+
|
| 24 |
+
return latex_str
|
| 25 |
+
|
| 26 |
+
plt.rcParams.update({
|
| 27 |
+
"text.usetex": True,
|
| 28 |
+
"font.family": "serif",
|
| 29 |
+
"axes.spines.right": False,
|
| 30 |
+
"axes.spines.top": False,
|
| 31 |
+
"text.latex.preamble": r"\usepackage{amsmath,amssymb}"
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
def show_latex(formula):
|
| 35 |
+
try:
|
| 36 |
+
# 创建图像
|
| 37 |
+
fig = plt.figure()
|
| 38 |
+
plt.axis("off")
|
| 39 |
+
plt.text(0.5, 0.5, "\n \n"+f"{remove1(formula)}",
|
| 40 |
+
ha="center", va="center",
|
| 41 |
+
fontsize=24)
|
| 42 |
+
|
| 43 |
+
# 保存到内存缓冲区
|
| 44 |
+
buf = io.BytesIO()
|
| 45 |
+
plt.savefig(buf, format="png", bbox_inches="tight", dpi=150)
|
| 46 |
+
plt.close(fig)
|
| 47 |
+
buf.seek(0)
|
| 48 |
+
return Image.open(buf)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return show_latex("Sorry, due to the incompatibility of some LaTeX expressions, rendering errors occurred.")
|
| 51 |
+
|
| 52 |
+
def search(query,enable_latex):
|
| 53 |
+
output_results = output_collection.get(
|
| 54 |
+
where={"$or": [{"tag": query}, {"label": query}]},
|
| 55 |
+
limit=1
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if not output_results["ids"]:
|
| 59 |
+
return ["No matching results found",None,'']
|
| 60 |
+
|
| 61 |
+
# 获取匹配的output信息
|
| 62 |
+
tag= output_results["metadatas"][0]["tag"]
|
| 63 |
+
label= output_results["metadatas"][0]["label"]
|
| 64 |
+
latex= output_results["documents"][0]
|
| 65 |
+
latex_output = f"""
|
| 66 |
+
<div style="margin-bottom: 25px; padding: 15px; border: 1px solid #e0e0e0; border-radius: 8px;">
|
| 67 |
+
<div style="margin: 15px 0;">
|
| 68 |
+
<p style="font-size: 1.1em; margin: 8px 0;">
|
| 69 |
+
<strong>✍ Tag:</strong> {tag}
|
| 70 |
+
</p>
|
| 71 |
+
<p style="font-size: 1.1em; margin: 8px 0;">
|
| 72 |
+
<strong>💬 Label:</strong> {label}
|
| 73 |
+
</p>
|
| 74 |
+
<p style="font-size: 1.1em; margin: 8px 0; color: #e67e22;">
|
| 75 |
+
<strong>⭐ LaTeX:</strong> {latex}
|
| 76 |
+
</p>
|
| 77 |
+
</div>
|
| 78 |
+
<div style="text-align: center; margin-top: 20px;">
|
| 79 |
+
<a href="https://stacks.math.columbia.edu/tag/{tag}"
|
| 80 |
+
target="_blank"
|
| 81 |
+
style="display: inline-block;
|
| 82 |
+
background: #3498db;
|
| 83 |
+
color: white !important;
|
| 84 |
+
padding: 12px 25px;
|
| 85 |
+
border-radius: 5px;
|
| 86 |
+
text-decoration: none;
|
| 87 |
+
font-size: 1.1em;
|
| 88 |
+
transition: all 0.3s ease;
|
| 89 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
|
| 90 |
+
📖 Proceed To The Docs Link
|
| 91 |
+
</a>
|
| 92 |
+
</div>
|
| 93 |
+
</div>
|
| 94 |
+
"""
|
| 95 |
+
latex_image = show_latex("\n \n"+remove1(latex)) if enable_latex else None
|
| 96 |
+
answer = '<div style="padding: 10px;">'
|
| 97 |
+
for l in range(10):
|
| 98 |
+
kind = output_results["metadatas"][0][str(l)+'kind'].strip()
|
| 99 |
+
fn = output_results["metadatas"][0][str(l)+'formal name'].strip("'")
|
| 100 |
+
fs = output_results["metadatas"][0][str(l)+'formal statement']
|
| 101 |
+
inn = output_results["metadatas"][0][str(l)+'informal name'].strip("'")
|
| 102 |
+
ins = output_results["metadatas"][0][str(l)+'informal statement']
|
| 103 |
+
|
| 104 |
+
answer += f"""
|
| 105 |
+
<div style="margin-bottom: 25px; padding: 15px; border: 1px solid #e0e0e0; border-radius: 8px;">
|
| 106 |
+
<!-- 放大并居中的formal name -->
|
| 107 |
+
<div style="text-align: center; margin: 15px 0;">
|
| 108 |
+
<h2 style="font-size: 1.8em; color: #2c3e50;
|
| 109 |
+
margin: 0;
|
| 110 |
+
padding-bottom: 10px;
|
| 111 |
+
border-bottom: 3px solid #3498db;
|
| 112 |
+
display: inline-block;">
|
| 113 |
+
{fn}
|
| 114 |
+
</h2>
|
| 115 |
+
</div>
|
| 116 |
+
|
| 117 |
+
<!-- 强调的formal statement -->
|
| 118 |
+
<div style="background: #f8f9fa;
|
| 119 |
+
padding: 15px;
|
| 120 |
+
border-radius: 6px;
|
| 121 |
+
margin: 15px 0;
|
| 122 |
+
border-left: 4px solid #3498db;">
|
| 123 |
+
<p style="font-size: 1.2em; line-height: 1.6; color: #2c3e50; margin: 0;font-family: 'Courier New', Consolas, Monaco, monospace;">
|
| 124 |
+
<strong style="font-size: 1.1em; color: #3498db;">📜 Formal Statement:</strong><br>
|
| 125 |
+
{fs}
|
| 126 |
+
</p>
|
| 127 |
+
</div>
|
| 128 |
+
|
| 129 |
+
<!-- 其他信息 -->
|
| 130 |
+
<div style="margin: 15px 0;">
|
| 131 |
+
<p style="font-size: 1.1em; margin: 8px 0;">
|
| 132 |
+
<strong>⭐ Kind:</strong> {kind}
|
| 133 |
+
</p>
|
| 134 |
+
<p style="font-size: 1.1em; margin: 8px 0;">
|
| 135 |
+
<strong>✍ Informal Name:</strong> {inn}
|
| 136 |
+
</p>
|
| 137 |
+
<p style="font-size: 1.1em; margin: 8px 0;">
|
| 138 |
+
<strong>💬 Informal Statement:</strong> {ins}
|
| 139 |
+
</p>
|
| 140 |
+
<p style="font-size: 1.1em; margin: 8px 0; color: #e67e22;">
|
| 141 |
+
<strong>✌ Similarity Ranking:</strong> {l+1}
|
| 142 |
+
</p>
|
| 143 |
+
</div>
|
| 144 |
+
|
| 145 |
+
<!-- 文档链接 -->
|
| 146 |
+
<div style="text-align: center; margin-top: 20px;">
|
| 147 |
+
<a href="https://leanprover-community.github.io/mathlib4_docs/find/?pattern={fn}#doc"
|
| 148 |
+
target="_blank"
|
| 149 |
+
style="display: inline-block;
|
| 150 |
+
background: #3498db;
|
| 151 |
+
color: white !important;
|
| 152 |
+
padding: 12px 25px;
|
| 153 |
+
border-radius: 5px;
|
| 154 |
+
text-decoration: none;
|
| 155 |
+
font-size: 1.1em;
|
| 156 |
+
transition: all 0.3s ease;
|
| 157 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
|
| 158 |
+
📖 View Full Documentation
|
| 159 |
+
</a>
|
| 160 |
+
</div>
|
| 161 |
+
</div>
|
| 162 |
+
"""
|
| 163 |
+
answer += '</div>'
|
| 164 |
+
|
| 165 |
+
return [latex_output,latex_image,answer]
|
| 166 |
+
|
| 167 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Stack-Mathlib4 Search") as demo:
|
| 168 |
+
gr.Markdown("""<center><font size=8> Stack-Mathlib4 Search</font></center>""")
|
| 169 |
+
|
| 170 |
+
with gr.Tab("Theorem Search"):
|
| 171 |
+
with gr.Row():
|
| 172 |
+
label_input = gr.Textbox(
|
| 173 |
+
placeholder="",
|
| 174 |
+
label="Please enter a label or a tag"
|
| 175 |
+
)
|
| 176 |
+
search_button = gr.Button("Search", variant="primary")
|
| 177 |
+
|
| 178 |
+
enable_switch = gr.Checkbox(
|
| 179 |
+
label="LaTeX Rendering",
|
| 180 |
+
value=True,
|
| 181 |
+
info="Toggle LaTeX formula display"
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
information = gr.HTML(label="Information of the Theorem", elem_classes="result-box")
|
| 185 |
+
latex_2 = gr.Image(label="LaTeX Rendering", interactive=False)
|
| 186 |
+
answer = gr.HTML(label="Top Matching Theorems", elem_classes="result-box")
|
| 187 |
+
|
| 188 |
+
search_button.click(
|
| 189 |
+
fn=search,
|
| 190 |
+
inputs=[label_input, enable_switch],
|
| 191 |
+
outputs=[information, latex_2, answer]
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
demo.launch(share=True)
|