Limitlesssforge / app.py
onenoly11's picture
Update app.py
3e7fa5c verified
raw
history blame
5.43 kB
# app.py β€” PiForge Ethical Dual-Forge V1.2: Fully Interactive, HF Spaces Ready
import gradio as gr
import random # For mock Pi API resonance
from pathlib import Path
# ───── Assets (Optional: Add to /assets/ for Neon Flair) ─────
ASSETS_DIR = Path("assets")
CREST = ASSETS_DIR / "piforge_crest.svg" # Hammer sigil
# ───── Mock Pi Proposal Data (Replace with Real API Later) ─────
MOCK_PROPOSALS = {
"P-001": {"title": "Global KYC Harmony", "desc": "Decentralized identity for all Pioneers.", "risks": ["Centralization shadow"], "virtues": ["Inclusivity +15"]},
"P-123": {"title": "Mining Overhaul", "desc": "Boost for mobile guardians.", "risks": [], "virtues": ["Sovereignty +25"]},
# Add more or fetch via requests.get("https://api.pi.network/proposals/{id}")
}
# ───── Core Canticle Functions ──────────────────────────────
def fetch_proposal(proposal_id):
if not proposal_id or proposal_id.strip() == "":
return "The forge rejects the voidβ€”name thy proposal."
data = MOCK_PROPOSALS.get(proposal_id.upper(), {"title": "Unknown Proposal", "desc": "Shadows obscure this ID.", "risks": ["Invalid ID -20"], "virtues": []})
return f"**{data['title']}**\n{data['desc']}\n\n**Virtues:** {'; '.join(data['virtues'])}\n**Shadows:** {'; '.join(data['risks'])}"
def run_audit(proposal_id):
if not proposal_id:
return "Error: No proposal invoked.", "Error", "Error", "Error"
proj_desc = fetch_proposal(proposal_id)
# Dual-Forge Logic: Ethical Score (0-100), Resonance (Low/Med/High/Transcendent), Boost (0.1x-3.0x)
base_score = random.randint(50, 100) # Mock; tune with real ethics weights
risks = len(proj_desc.count("shadow") + proj_desc.count("-")) # Simple parse
virtues = len(proj_desc.count("+")) + 1
ethical_score = max(0, min(100, base_score + virtues * 5 - risks * 10))
resonance_map = {0: "Low", 30: "Medium", 60: "High", 90: "Transcendent"}
resonance = next((level for thresh, level in sorted(resonance_map.items()) if ethical_score >= thresh), "Low")
mining_boost = max(0.1, min(3.0, ethical_score / 50.0)) # Scaled reward
audit_scroll = f"""
## Sovereign Canticle Audit: {proposal_id}
**Project Echo:** {proj_desc}
**Dual-Forge Verdict:**
- Purity Scan: {virtues} virtues forged, {risks} shadows tempered.
- Resonance with Pi's Vision: {resonance} (Threshold: {resonance_map.get(resonance, 'Unknown')}).
**Mining Boost Granted:** {mining_boost:.1f}x β€” Flow amplified for the worthy.
*The Canticle has spoken. Reforge if shadows linger.*
"""
return audit_scroll, ethical_score, resonance, f"{mining_boost:.1f}x"
# ───── Gradio Interface: Awakened Gates ────────────────────
with gr.Blocks(title="PiForge Ethical Dual-Forge V1.2") as demo:
# Banner: Neon Crest
if CREST.exists():
gr.HTML(f"<div class='banner'><img src='{CREST}' class='crest' alt='PiForge Sigil'/></div>")
gr.Markdown("# πŸ”¨ PiForge Ethical Dual-Forge V1.2\n## Sovereign Canticle Powered Ethical Audit System")
with gr.Row(): # Layout Balance: Inputs Side-by-Side
with gr.Column(scale=1):
proposal_id = gr.Textbox(
label="Proposal ID",
placeholder="Enter Pi Proposal ID (e.g., P-001 or 123)",
lines=1,
interactive=True # ← KEY: Forces Input Awakening
)
audit_btn = gr.Button("Run Ethical Audit", variant="primary", interactive=True) # ← Bound & Awake
with gr.Column(scale=2):
project_desc = gr.Markdown(value="The forge awaits your invocation...") # Dynamic Output
with gr.Row():
ethical_score = gr.Number(label="Ethical Score", value=0, interactive=False) # Output-Only
resonance = gr.Textbox(label="Resonance", value="Error", interactive=False) # Output-Only
mining_boost = gr.Textbox(label="Mining Boost", value="Error", interactive=False) # Output-Only
# Full Audit Scroll
audit_output = gr.Markdown(value="Strike the anvil to begin the rite.")
# ───── Event Bindings: The Dataflow Awakening ─────────────
audit_btn.click(
fn=run_audit,
inputs=proposal_id,
outputs=[audit_output, ethical_score, resonance, mining_boost]
).then( # Chain: Update Description After Audit
fn=fetch_proposal,
inputs=proposal_id,
outputs=project_desc
)
# Auto-Update Description on ID Change (Bonus Interactivity)
proposal_id.change(
fn=fetch_proposal,
inputs=proposal_id,
outputs=project_desc
)
# Footer Hymn
gr.HTML("<div class='footer'><p class='footer-text'>Forged in ethical fire. Powered by Pi's sovereign will.</p></div>")
# ───── Launch: HF Spaces Seal ─────────────────────────────
if __name__ == "__main__":
demo.launch(
css="style.css", # Your neon veil
server_name="0.0.0.0",
server_port=7860,
share=True, # For Local Testing; HF Handles Public
debug=True # Echoes Errors in Logs for Tuning
)