Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gradio demo for Structural Isomorphism Search Engine.
|
| 3 |
+
|
| 4 |
+
Usage:
|
| 5 |
+
pip install gradio
|
| 6 |
+
python demo/app.py
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import sys
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
# Add project root to path
|
| 13 |
+
sys.path.insert(0, str(Path(__file__).parent.parent))
|
| 14 |
+
|
| 15 |
+
import gradio as gr
|
| 16 |
+
from structural_isomorphism import StructuralSearch
|
| 17 |
+
|
| 18 |
+
# Global search engine instance (loaded once)
|
| 19 |
+
search = None
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def initialize():
|
| 23 |
+
"""Load model and knowledge base."""
|
| 24 |
+
global search
|
| 25 |
+
if search is None:
|
| 26 |
+
search = StructuralSearch()
|
| 27 |
+
return search
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def do_search(query: str, top_k: int) -> str:
|
| 31 |
+
"""Run structural search and format results as HTML."""
|
| 32 |
+
engine = initialize()
|
| 33 |
+
results = engine.query(query, top_k=int(top_k))
|
| 34 |
+
|
| 35 |
+
if not results:
|
| 36 |
+
return "<p style='color: #888;'>No results found. Check that knowledge base files exist in data/.</p>"
|
| 37 |
+
|
| 38 |
+
html_parts = []
|
| 39 |
+
for i, r in enumerate(results, 1):
|
| 40 |
+
score_color = "#22c55e" if r["score"] > 0.7 else "#eab308" if r["score"] > 0.4 else "#ef4444"
|
| 41 |
+
html_parts.append(f"""
|
| 42 |
+
<div style="border: 1px solid #e5e7eb; border-radius: 8px; padding: 16px; margin-bottom: 12px;">
|
| 43 |
+
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;">
|
| 44 |
+
<div>
|
| 45 |
+
<span style="font-weight: 700; font-size: 1.1em;">#{i} {r['name']}</span>
|
| 46 |
+
<span style="background: #f3f4f6; padding: 2px 8px; border-radius: 4px; margin-left: 8px; font-size: 0.9em;">{r['domain']}</span>
|
| 47 |
+
<span style="background: #f3f4f6; padding: 2px 8px; border-radius: 4px; margin-left: 4px; font-size: 0.85em; color: #6b7280;">Type {r['type_id']}</span>
|
| 48 |
+
</div>
|
| 49 |
+
<span style="color: {score_color}; font-weight: 700; font-size: 1.1em;">{r['score']:.3f}</span>
|
| 50 |
+
</div>
|
| 51 |
+
<p style="color: #374151; margin: 0; line-height: 1.6;">{r['description']}</p>
|
| 52 |
+
</div>
|
| 53 |
+
""")
|
| 54 |
+
|
| 55 |
+
return "\n".join(html_parts)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
EXAMPLES = [
|
| 59 |
+
["两个市场参与者互相等待对方先行动,导致谁也不动"],
|
| 60 |
+
["一个系统在受到小扰动后能自动回到原来的状态"],
|
| 61 |
+
["产品刚上市时增长缓慢,然后突然爆发式增长,最后趋于饱和"],
|
| 62 |
+
["每个人都做出对自己最优的选择,但合起来的结果对所有人都不好"],
|
| 63 |
+
["温度只需要微小变化,整个系统就突然从一种状态变成另一种状态"],
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
DESCRIPTION = """
|
| 67 |
+
# Structural Isomorphism Search Engine
|
| 68 |
+
|
| 69 |
+
Discover hidden cross-domain structural connections. Describe any phenomenon in natural language,
|
| 70 |
+
and the engine will find structurally similar phenomena from completely different domains.
|
| 71 |
+
|
| 72 |
+
The model recognizes **structural patterns** (feedback loops, phase transitions, cascade effects, etc.)
|
| 73 |
+
rather than surface-level keyword matches.
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
with gr.Blocks(
|
| 77 |
+
title="Structural Isomorphism Search",
|
| 78 |
+
theme=gr.themes.Soft(),
|
| 79 |
+
) as demo:
|
| 80 |
+
gr.Markdown(DESCRIPTION)
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
with gr.Column(scale=3):
|
| 84 |
+
query_input = gr.Textbox(
|
| 85 |
+
label="Describe a phenomenon",
|
| 86 |
+
placeholder="e.g., A thermostat detects temperature below setpoint, turns on heating...",
|
| 87 |
+
lines=3,
|
| 88 |
+
)
|
| 89 |
+
with gr.Column(scale=1):
|
| 90 |
+
top_k_slider = gr.Slider(
|
| 91 |
+
minimum=1, maximum=20, value=10, step=1,
|
| 92 |
+
label="Number of results",
|
| 93 |
+
)
|
| 94 |
+
search_btn = gr.Button("Search", variant="primary", size="lg")
|
| 95 |
+
|
| 96 |
+
gr.Examples(
|
| 97 |
+
examples=EXAMPLES,
|
| 98 |
+
inputs=query_input,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
results_output = gr.HTML(label="Results")
|
| 102 |
+
|
| 103 |
+
search_btn.click(
|
| 104 |
+
fn=do_search,
|
| 105 |
+
inputs=[query_input, top_k_slider],
|
| 106 |
+
outputs=results_output,
|
| 107 |
+
)
|
| 108 |
+
query_input.submit(
|
| 109 |
+
fn=do_search,
|
| 110 |
+
inputs=[query_input, top_k_slider],
|
| 111 |
+
outputs=results_output,
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
demo.launch(share=False)
|