File size: 6,512 Bytes
4f5b091
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83cedb8
4f5b091
 
 
 
 
83cedb8
 
 
4f5b091
83cedb8
4f5b091
 
 
 
 
 
83cedb8
 
4f5b091
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import chromadb
import gradio as gr
import matplotlib.pyplot as plt
import io
from PIL import Image
import re

# 初始化集合
client = chromadb.PersistentClient(path="chroma_db2")
output_collection = client.get_collection("data_collection")

def remove1(latex_str):
    # 定义需要移除的命令的正则表达式
    unsupported_commands = [
        r'\\ref\{([^}]+)\}',  # 匹配 \ref{...},保留括号内容
        r'\\label\{([^}]+)\}',  # 匹配 \label{...},保留括号内容
        r'\\Ob\(([^)]+)\)',
    ]
    latex_str=latex_str.replace("\\\\","\\")
    # 逐个移除不支持的命令
    for pattern in unsupported_commands:
        latex_str = re.sub(pattern, r'\1', latex_str)

    return latex_str

plt.rcParams.update({
    "text.usetex": True,
    "font.family": "serif",
    "axes.spines.right": False,
    "axes.spines.top": False,
    "text.latex.preamble": r"\usepackage{amsmath,amssymb}"
})

def search(query,enable_latex):
    output_results = output_collection.get(
        where={"$or": [{"tag": query}, {"label": query}]},
        limit=1
    )
    
    if not output_results["ids"]:
        return ["No matching results found",None,'']
    
    # 获取匹配的output信息
    tag= output_results["metadatas"][0]["tag"]
    label= output_results["metadatas"][0]["label"]
    latex= output_results["documents"][0]
    latex_output = f"""
    <div style="margin-bottom: 25px; padding: 15px; border: 1px solid #e0e0e0; border-radius: 8px;">
        <div style="margin: 15px 0;">
                <p style="font-size: 1.1em; margin: 8px 0;">
                    <strong>✍ Tag:</strong> {tag}
                </p>
                <p style="font-size: 1.1em; margin: 8px 0;">
                    <strong>💬 Label:</strong> {label}
                </p>
                <p style="font-size: 1.1em; margin: 8px 0; color: #e67e22;">
                    <strong>⭐ LaTeX:</strong> {latex}
                </p>
        </div>
        <div style="text-align: center; margin-top: 20px;">
            <a href="https://stacks.math.columbia.edu/tag/{tag}" 
                target="_blank" 
                style="display: inline-block;
                    background: #3498db;
                    color: white !important;
                    padding: 12px 25px;
                    border-radius: 5px;
                    text-decoration: none;
                    font-size: 1.1em;
                    transition: all 0.3s ease;
                    box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
                📖 Proceed To The Docs Link
            </a>
        </div>
    </div>
    """
    answer = '<div style="padding: 10px;">'
    for l in range(10):
        kind = output_results["metadatas"][0][str(l)+'kind'].strip()
        fn = output_results["metadatas"][0][str(l)+'formal name'].strip("'")
        fs = output_results["metadatas"][0][str(l)+'formal statement']
        inn = output_results["metadatas"][0][str(l)+'informal name'].strip("'")
        ins = output_results["metadatas"][0][str(l)+'informal statement']
    
        answer += f"""
        <div style="margin-bottom: 25px; padding: 15px; border: 1px solid #e0e0e0; border-radius: 8px;">
            <!-- 放大并居中的formal name -->
            <div style="text-align: center; margin: 15px 0;">
                <h2 style="font-size: 1.8em; color: #2c3e50; 
                         margin: 0;
                          padding-bottom: 10px;
                          border-bottom: 3px solid #3498db;
                          display: inline-block;">
                    {fn}
                </h2>
            </div>

            <!-- 强调的formal statement -->
            <div style="background: #f8f9fa;
                       padding: 15px;
                       border-radius: 6px;
                       margin: 15px 0;
                       border-left: 4px solid #3498db;">
                <p style="font-size: 1.2em; line-height: 1.6; color: #2c3e50; margin: 0;font-family: 'Courier New', Consolas, Monaco, monospace;">
                    <strong style="font-size: 1.1em; color: #3498db;">📜 Formal Statement:</strong><br>
                    {fs}
                </p>
            </div>

            <!-- 其他信息 -->
            <div style="margin: 15px 0;">
                <p style="font-size: 1.1em; margin: 8px 0;">
                    <strong>⭐ Kind:</strong> {kind}
                </p>
                <p style="font-size: 1.1em; margin: 8px 0;">
                    <strong>✍ Informal Name:</strong> {inn}
                </p>
                <p style="font-size: 1.1em; margin: 8px 0;">
                    <strong>💬 Informal Statement:</strong> {ins}
                </p>
                <p style="font-size: 1.1em; margin: 8px 0; color: #e67e22;">
                    <strong>✌ Similarity Ranking:</strong> {l+1}
                </p>
            </div>

            <!-- 文档链接 -->
            <div style="text-align: center; margin-top: 20px;">
                <a href="https://leanprover-community.github.io/mathlib4_docs/find/?pattern={fn}#doc" 
                   target="_blank" 
                   style="display: inline-block;
                         background: #3498db;
                          color: white !important;
                          padding: 12px 25px;
                          border-radius: 5px;
                          text-decoration: none;
                          font-size: 1.1em;
                          transition: all 0.3s ease;
                          box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
                    📖 View Full Documentation
                </a>
            </div>
        </div>
        """
    answer += '</div>'
    
    return [latex_output,answer]

with gr.Blocks(theme=gr.themes.Soft(), title="Stack-Mathlib4 Search") as demo:
    gr.Markdown("""<center><font size=8> Stack-Mathlib4 Search</font></center>""")
        
    with gr.Tab("Theorem Search"):
        label_input = gr.Textbox(
            placeholder="", 
            label="Please enter a label or a tag"
        )
        search_button = gr.Button("Search", variant="primary")
        
        information = gr.HTML(label="Information of the Theorem", elem_classes="result-box")
        answer = gr.HTML(label="Top Matching Theorems", elem_classes="result-box")

        search_button.click(
            fn=search,
            inputs=[label_input],
            outputs=[information, answer]
        )

demo.launch(share=True)