Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ AI-assisted research tool for machine-verified theorems.
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import json
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
import gradio as gr
|
| 10 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
@@ -12,6 +13,14 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
| 12 |
import matplotlib.pyplot as plt
|
| 13 |
import matplotlib
|
| 14 |
matplotlib.use('Agg')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Load knowledge base
|
| 17 |
with open('theorems_kb.json', 'r') as f:
|
|
@@ -52,8 +61,8 @@ def find_relevant_theorems(hypothesis, top_k=5):
|
|
| 52 |
|
| 53 |
return results
|
| 54 |
|
| 55 |
-
def
|
| 56 |
-
"""Generate Lean 4 proof scaffold
|
| 57 |
|
| 58 |
scaffold = f"""-- Hypothesis: {hypothesis}
|
| 59 |
-- Upstream dependencies:
|
|
@@ -77,8 +86,63 @@ theorem my_hypothesis : <replace_with_formal_statement> := by
|
|
| 77 |
|
| 78 |
return scaffold
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def hypothesis_prover_tab(hypothesis):
|
| 81 |
-
"""Tab 1: Hypothesis Prover."""
|
| 82 |
if not hypothesis.strip():
|
| 83 |
return "Please enter a hypothesis.", ""
|
| 84 |
|
|
@@ -88,23 +152,23 @@ def hypothesis_prover_tab(hypothesis):
|
|
| 88 |
return "No relevant theorems found. Try different keywords.", ""
|
| 89 |
|
| 90 |
# Format relevant theorems
|
| 91 |
-
|
| 92 |
|
| 93 |
for i, item in enumerate(relevant, 1):
|
| 94 |
thm = item['theorem']
|
| 95 |
score = item['score']
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
if thm['docstring']:
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
|
| 104 |
-
# Generate
|
| 105 |
-
|
| 106 |
|
| 107 |
-
return
|
| 108 |
|
| 109 |
def search_theorems(module_filter, search_query):
|
| 110 |
"""Tab 2: Theorem Explorer."""
|
|
@@ -269,7 +333,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Eigenverse Theorem Prover") as app
|
|
| 269 |
|
| 270 |
# Tab 1: Hypothesis Prover
|
| 271 |
with gr.Tab("🔍 Hypothesis Prover"):
|
| 272 |
-
gr.Markdown("### Describe your hypothesis and get relevant theorems + proof
|
|
|
|
| 273 |
|
| 274 |
hypothesis_input = gr.Textbox(
|
| 275 |
label="Hypothesis",
|
|
@@ -277,13 +342,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Eigenverse Theorem Prover") as app
|
|
| 277 |
lines=3
|
| 278 |
)
|
| 279 |
|
| 280 |
-
prove_button = gr.Button("Find Theorems & Generate
|
| 281 |
|
| 282 |
with gr.Row():
|
| 283 |
with gr.Column():
|
| 284 |
relevant_output = gr.Markdown(label="Relevant Theorems")
|
| 285 |
with gr.Column():
|
| 286 |
-
scaffold_output = gr.
|
| 287 |
|
| 288 |
prove_button.click(
|
| 289 |
hypothesis_prover_tab,
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import json
|
| 8 |
+
import os
|
| 9 |
import numpy as np
|
| 10 |
import gradio as gr
|
| 11 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
|
|
| 13 |
import matplotlib.pyplot as plt
|
| 14 |
import matplotlib
|
| 15 |
matplotlib.use('Agg')
|
| 16 |
+
from huggingface_hub import InferenceClient
|
| 17 |
+
|
| 18 |
+
# HuggingFace Inference API setup
|
| 19 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None) # Set as Space secret
|
| 20 |
+
client = InferenceClient(
|
| 21 |
+
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 22 |
+
token=HF_TOKEN
|
| 23 |
+
)
|
| 24 |
|
| 25 |
# Load knowledge base
|
| 26 |
with open('theorems_kb.json', 'r') as f:
|
|
|
|
| 61 |
|
| 62 |
return results
|
| 63 |
|
| 64 |
+
def generate_template_scaffold(hypothesis, relevant_theorems):
|
| 65 |
+
"""Generate basic Lean 4 proof scaffold template (fallback)."""
|
| 66 |
|
| 67 |
scaffold = f"""-- Hypothesis: {hypothesis}
|
| 68 |
-- Upstream dependencies:
|
|
|
|
| 86 |
|
| 87 |
return scaffold
|
| 88 |
|
| 89 |
+
def generate_llm_hypothesis(user_hypothesis: str, relevant_theorems: list) -> str:
|
| 90 |
+
"""Generate LLM-powered hypothesis analysis and Lean 4 draft."""
|
| 91 |
+
|
| 92 |
+
theorem_context = "\n".join([
|
| 93 |
+
f"- {item['theorem']['name']} ({item['theorem']['module']}): {item['theorem']['statement'][:200]}"
|
| 94 |
+
for item in relevant_theorems[:5]
|
| 95 |
+
])
|
| 96 |
+
|
| 97 |
+
prompt = f"""You are a Lean 4 theorem proving assistant for the Eigenverse mathematical framework.
|
| 98 |
+
|
| 99 |
+
The Eigenverse framework is built around the critical eigenvalue μ = exp(i·3π/4) at the 135° phase, with coherence function C(r) = 2r/(1+r²). It has 688 machine-verified theorems across physics, chemistry, and cosmology.
|
| 100 |
+
|
| 101 |
+
User hypothesis: {user_hypothesis}
|
| 102 |
+
|
| 103 |
+
Relevant existing theorems found:
|
| 104 |
+
{theorem_context}
|
| 105 |
+
|
| 106 |
+
Your task:
|
| 107 |
+
1. Analyze how the hypothesis relates to these theorems
|
| 108 |
+
2. Identify what mathematical structures are needed
|
| 109 |
+
3. Draft a Lean 4 theorem statement and proof skeleton
|
| 110 |
+
|
| 111 |
+
Output format:
|
| 112 |
+
## Analysis
|
| 113 |
+
<2-3 sentences connecting hypothesis to existing theorems>
|
| 114 |
+
|
| 115 |
+
## Lean 4 Draft
|
| 116 |
+
```lean4
|
| 117 |
+
-- Hypothesis: {user_hypothesis}
|
| 118 |
+
-- Dependencies: list the relevant theorems
|
| 119 |
+
|
| 120 |
+
theorem hypothesis_name : <formal_statement> := by
|
| 121 |
+
-- Step 1: <what to do>
|
| 122 |
+
have h1 := relevant_theorem_1
|
| 123 |
+
-- Step 2: <what to do>
|
| 124 |
+
sorry -- replace with proof
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
## Next Steps
|
| 128 |
+
<what needs to be proven to complete this>"""
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
response = client.text_generation(
|
| 132 |
+
prompt,
|
| 133 |
+
max_new_tokens=600,
|
| 134 |
+
temperature=0.3,
|
| 135 |
+
do_sample=True,
|
| 136 |
+
)
|
| 137 |
+
return response
|
| 138 |
+
except Exception as e:
|
| 139 |
+
# Fallback to template if LLM fails
|
| 140 |
+
fallback = f"⚠️ LLM generation failed ({str(e)}). Showing template scaffold.\n\n"
|
| 141 |
+
fallback += generate_template_scaffold(user_hypothesis, relevant_theorems)
|
| 142 |
+
return fallback
|
| 143 |
+
|
| 144 |
def hypothesis_prover_tab(hypothesis):
|
| 145 |
+
"""Tab 1: Hypothesis Prover - now with LLM-powered analysis."""
|
| 146 |
if not hypothesis.strip():
|
| 147 |
return "Please enter a hypothesis.", ""
|
| 148 |
|
|
|
|
| 152 |
return "No relevant theorems found. Try different keywords.", ""
|
| 153 |
|
| 154 |
# Format relevant theorems
|
| 155 |
+
theorems_output = f"## Relevant Theorems (Top {len(relevant)})\n\n"
|
| 156 |
|
| 157 |
for i, item in enumerate(relevant, 1):
|
| 158 |
thm = item['theorem']
|
| 159 |
score = item['score']
|
| 160 |
+
theorems_output += f"### {i}. {thm['name']} (relevance: {score:.3f})\n"
|
| 161 |
+
theorems_output += f"**Module:** {thm['module']}\n\n"
|
| 162 |
+
theorems_output += f"**Kind:** {thm['kind']}\n\n"
|
| 163 |
if thm['docstring']:
|
| 164 |
+
theorems_output += f"**Description:** {thm['docstring']}\n\n"
|
| 165 |
+
theorems_output += f"```lean\n{thm['statement']}\n```\n\n"
|
| 166 |
+
theorems_output += "---\n\n"
|
| 167 |
|
| 168 |
+
# Generate LLM-powered hypothesis analysis
|
| 169 |
+
llm_output = generate_llm_hypothesis(hypothesis, relevant)
|
| 170 |
|
| 171 |
+
return theorems_output, llm_output
|
| 172 |
|
| 173 |
def search_theorems(module_filter, search_query):
|
| 174 |
"""Tab 2: Theorem Explorer."""
|
|
|
|
| 333 |
|
| 334 |
# Tab 1: Hypothesis Prover
|
| 335 |
with gr.Tab("🔍 Hypothesis Prover"):
|
| 336 |
+
gr.Markdown("### Describe your hypothesis and get relevant theorems + LLM-powered proof draft")
|
| 337 |
+
gr.Markdown("*Powered by Mistral-7B via HuggingFace Inference API. Set `HF_TOKEN` as a Space secret for better rate limits.*")
|
| 338 |
|
| 339 |
hypothesis_input = gr.Textbox(
|
| 340 |
label="Hypothesis",
|
|
|
|
| 342 |
lines=3
|
| 343 |
)
|
| 344 |
|
| 345 |
+
prove_button = gr.Button("Find Theorems & Generate Proof Draft", variant="primary")
|
| 346 |
|
| 347 |
with gr.Row():
|
| 348 |
with gr.Column():
|
| 349 |
relevant_output = gr.Markdown(label="Relevant Theorems")
|
| 350 |
with gr.Column():
|
| 351 |
+
scaffold_output = gr.Markdown(label="LLM Proof Draft")
|
| 352 |
|
| 353 |
prove_button.click(
|
| 354 |
hypothesis_prover_tab,
|