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"""

✍ Tag: {tag}

💬 Label: {label}

⭐ LaTeX: {latex}

📖 Proceed To The Docs Link
""" answer = '
' 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"""

{fn}

📜 Formal Statement:
{fs}

⭐ Kind: {kind}

✍ Informal Name: {inn}

💬 Informal Statement: {ins}

✌ Similarity Ranking: {l+1}

📖 View Full Documentation
""" answer += '
' return [latex_output,answer] with gr.Blocks(theme=gr.themes.Soft(), title="Stack-Mathlib4 Search") as demo: gr.Markdown("""
Stack-Mathlib4 Search
""") 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)