Spaces:
Sleeping
Sleeping
Update app.py
Browse filesgit add . && git commit -m "Awaken Dual-Forge: Bind events & interactivity" && git pus
app.py
CHANGED
|
@@ -1,96 +1,115 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
score = len(project_description) % 1000 # Mock score
|
| 9 |
-
resonance = 691 if score > 500 else 500 # Mock resonance
|
| 10 |
-
mining_boost = 15 if resonance >= 691 else 0
|
| 11 |
-
return score, resonance, mining_boost
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Prepare the results for the Pi App
|
| 21 |
-
results = {
|
| 22 |
-
"ethical_score": score,
|
| 23 |
-
"resonance": resonance,
|
| 24 |
-
"mining_boost": mining_boost,
|
| 25 |
-
"proposal_id": proposal_id
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
# In a real implementation, we would use JavaScript to postMessage
|
| 29 |
-
# Since Gradio doesn't directly support postMessage, we might use custom JS
|
| 30 |
-
# For now, we return the results to the frontend and let the frontend handle the postMessage
|
| 31 |
-
return results
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
resonance_output = gr.Number(label="Resonance")
|
| 45 |
-
mining_boost_output = gr.Number(label="Mining Boost")
|
| 46 |
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
results_output = gr.JSON(visible=False)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
fn=audit_and_communicate,
|
| 55 |
-
inputs=project_input,
|
| 56 |
-
outputs=[score_output, resonance_output, mining_boost_output, proposal_id_output, results_output]
|
| 57 |
-
)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
// We can also use the specific element ID, but Gradio doesn't guarantee it.
|
| 70 |
-
// Alternatively, we can use the Gradio JS events.
|
| 71 |
-
// But for simplicity, we'll use the following:
|
| 72 |
-
|
| 73 |
-
// We'll create a function to send the message when the results are updated.
|
| 74 |
-
function sendMessage() {
|
| 75 |
-
const results = document.querySelector('#results-output .json'); // Adjust the selector based on the actual structure
|
| 76 |
-
if (results) {
|
| 77 |
-
const data = JSON.parse(results.textContent);
|
| 78 |
-
window.parent.postMessage({
|
| 79 |
-
type: 'ethical_audit_complete',
|
| 80 |
-
results: data
|
| 81 |
-
}, "https://app.minepi.com");
|
| 82 |
-
}
|
| 83 |
-
}
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
)
|
|
|
|
|
|
|
|
|
|
| 94 |
|
|
|
|
| 95 |
if __name__ == "__main__":
|
| 96 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py β PiForge Ethical Dual-Forge V1.2: Fully Interactive, HF Spaces Ready
|
| 2 |
import gradio as gr
|
| 3 |
+
import random # For mock Pi API resonance
|
| 4 |
+
from pathlib import Path
|
| 5 |
|
| 6 |
+
# βββββ Assets (Optional: Add to /assets/ for Neon Flair) βββββ
|
| 7 |
+
ASSETS_DIR = Path("assets")
|
| 8 |
+
CREST = ASSETS_DIR / "piforge_crest.svg" # Hammer sigil
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# βββββ Mock Pi Proposal Data (Replace with Real API Later) βββββ
|
| 11 |
+
MOCK_PROPOSALS = {
|
| 12 |
+
"P-001": {"title": "Global KYC Harmony", "desc": "Decentralized identity for all Pioneers.", "risks": ["Centralization shadow"], "virtues": ["Inclusivity +15"]},
|
| 13 |
+
"P-123": {"title": "Mining Overhaul", "desc": "Boost for mobile guardians.", "risks": [], "virtues": ["Sovereignty +25"]},
|
| 14 |
+
# Add more or fetch via requests.get("https://api.pi.network/proposals/{id}")
|
| 15 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# βββββ Core Canticle Functions ββββββββββββββββββββββββββββββ
|
| 18 |
+
def fetch_proposal(proposal_id):
|
| 19 |
+
if not proposal_id or proposal_id.strip() == "":
|
| 20 |
+
return "The forge rejects the voidβname thy proposal."
|
| 21 |
+
data = MOCK_PROPOSALS.get(proposal_id.upper(), {"title": "Unknown Proposal", "desc": "Shadows obscure this ID.", "risks": ["Invalid ID -20"], "virtues": []})
|
| 22 |
+
return f"**{data['title']}**\n{data['desc']}\n\n**Virtues:** {'; '.join(data['virtues'])}\n**Shadows:** {'; '.join(data['risks'])}"
|
| 23 |
+
|
| 24 |
+
def run_audit(proposal_id):
|
| 25 |
+
if not proposal_id:
|
| 26 |
+
return "Error: No proposal invoked.", "Error", "Error", "Error"
|
| 27 |
|
| 28 |
+
proj_desc = fetch_proposal(proposal_id)
|
| 29 |
+
# Dual-Forge Logic: Ethical Score (0-100), Resonance (Low/Med/High/Transcendent), Boost (0.1x-3.0x)
|
| 30 |
+
base_score = random.randint(50, 100) # Mock; tune with real ethics weights
|
| 31 |
+
risks = len(proj_desc.count("shadow") + proj_desc.count("-")) # Simple parse
|
| 32 |
+
virtues = len(proj_desc.count("+")) + 1
|
| 33 |
+
ethical_score = max(0, min(100, base_score + virtues * 5 - risks * 10))
|
| 34 |
|
| 35 |
+
resonance_map = {0: "Low", 30: "Medium", 60: "High", 90: "Transcendent"}
|
| 36 |
+
resonance = next((level for thresh, level in sorted(resonance_map.items()) if ethical_score >= thresh), "Low")
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
mining_boost = max(0.1, min(3.0, ethical_score / 50.0)) # Scaled reward
|
| 39 |
|
| 40 |
+
audit_scroll = f"""
|
| 41 |
+
## Sovereign Canticle Audit: {proposal_id}
|
|
|
|
| 42 |
|
| 43 |
+
**Project Echo:** {proj_desc}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
**Dual-Forge Verdict:**
|
| 46 |
+
- Purity Scan: {virtues} virtues forged, {risks} shadows tempered.
|
| 47 |
+
- Resonance with Pi's Vision: {resonance} (Threshold: {resonance_map.get(resonance, 'Unknown')}).
|
| 48 |
+
|
| 49 |
+
**Mining Boost Granted:** {mining_boost:.1f}x β Flow amplified for the worthy.
|
| 50 |
+
|
| 51 |
+
*The Canticle has spoken. Reforge if shadows linger.*
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
return audit_scroll, ethical_score, resonance, f"{mining_boost:.1f}x"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# βββββ Gradio Interface: Awakened Gates ββββββββββββββββββββ
|
| 57 |
+
with gr.Blocks(title="PiForge Ethical Dual-Forge V1.2") as demo:
|
| 58 |
+
|
| 59 |
+
# Banner: Neon Crest
|
| 60 |
+
if CREST.exists():
|
| 61 |
+
gr.HTML(f"<div class='banner'><img src='{CREST}' class='crest' alt='PiForge Sigil'/></div>")
|
| 62 |
+
|
| 63 |
+
gr.Markdown("# π¨ PiForge Ethical Dual-Forge V1.2\n## Sovereign Canticle Powered Ethical Audit System")
|
| 64 |
+
|
| 65 |
+
with gr.Row(): # Layout Balance: Inputs Side-by-Side
|
| 66 |
+
with gr.Column(scale=1):
|
| 67 |
+
proposal_id = gr.Textbox(
|
| 68 |
+
label="Proposal ID",
|
| 69 |
+
placeholder="Enter Pi Proposal ID (e.g., P-001 or 123)",
|
| 70 |
+
lines=1,
|
| 71 |
+
interactive=True # β KEY: Forces Input Awakening
|
| 72 |
+
)
|
| 73 |
+
audit_btn = gr.Button("Run Ethical Audit", variant="primary", interactive=True) # β Bound & Awake
|
| 74 |
+
|
| 75 |
+
with gr.Column(scale=2):
|
| 76 |
+
project_desc = gr.Markdown(value="The forge awaits your invocation...") # Dynamic Output
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
ethical_score = gr.Number(label="Ethical Score", value=0, interactive=False) # Output-Only
|
| 80 |
+
resonance = gr.Textbox(label="Resonance", value="Error", interactive=False) # Output-Only
|
| 81 |
+
mining_boost = gr.Textbox(label="Mining Boost", value="Error", interactive=False) # Output-Only
|
| 82 |
+
|
| 83 |
+
# Full Audit Scroll
|
| 84 |
+
audit_output = gr.Markdown(value="Strike the anvil to begin the rite.")
|
| 85 |
+
|
| 86 |
+
# βββββ Event Bindings: The Dataflow Awakening βββββββββββββ
|
| 87 |
+
audit_btn.click(
|
| 88 |
+
fn=run_audit,
|
| 89 |
+
inputs=proposal_id,
|
| 90 |
+
outputs=[audit_output, ethical_score, resonance, mining_boost]
|
| 91 |
+
).then( # Chain: Update Description After Audit
|
| 92 |
+
fn=fetch_proposal,
|
| 93 |
+
inputs=proposal_id,
|
| 94 |
+
outputs=project_desc
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Auto-Update Description on ID Change (Bonus Interactivity)
|
| 98 |
+
proposal_id.change(
|
| 99 |
+
fn=fetch_proposal,
|
| 100 |
+
inputs=proposal_id,
|
| 101 |
+
outputs=project_desc
|
| 102 |
)
|
| 103 |
+
|
| 104 |
+
# Footer Hymn
|
| 105 |
+
gr.HTML("<div class='footer'><p class='footer-text'>Forged in ethical fire. Powered by Pi's sovereign will.</p></div>")
|
| 106 |
|
| 107 |
+
# βββββ Launch: HF Spaces Seal βββββββββββββββββββββββββββββ
|
| 108 |
if __name__ == "__main__":
|
| 109 |
+
demo.launch(
|
| 110 |
+
css="style.css", # Your neon veil
|
| 111 |
+
server_name="0.0.0.0",
|
| 112 |
+
server_port=7860,
|
| 113 |
+
share=True, # For Local Testing; HF Handles Public
|
| 114 |
+
debug=True # Echoes Errors in Logs for Tuning
|
| 115 |
+
)
|