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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py - Ai Forge: Ethical Audit + AI App Builder (Streamlit Version)
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import hashlib
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import numpy as np
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# Page config
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Ai Forge: Ethical Audit + AI App Builder",
|
| 11 |
+
page_icon="π€",
|
| 12 |
+
layout="wide"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# π PIFORGE DUAL-PLATFORM: AUDIT + APP BUILDER
|
| 16 |
+
PIFORGE_CSS = """
|
| 17 |
+
<style>
|
| 18 |
+
.piforge-premium-header {
|
| 19 |
+
background: linear-gradient(135deg, #FF6B6B 0%, #FFE66D 50%, #1A535C 100%);
|
| 20 |
+
padding: 30px;
|
| 21 |
+
border-radius: 20px;
|
| 22 |
+
color: white;
|
| 23 |
+
text-align: center;
|
| 24 |
+
margin-bottom: 30px;
|
| 25 |
+
border: 4px solid #4ECDC4;
|
| 26 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
|
| 27 |
+
}
|
| 28 |
+
.audit-badge {
|
| 29 |
+
background: linear-gradient(45deg, #667eea, #764ba2);
|
| 30 |
+
color: white;
|
| 31 |
+
padding: 8px 16px;
|
| 32 |
+
border-radius: 20px;
|
| 33 |
+
font-size: 16px;
|
| 34 |
+
font-weight: bold;
|
| 35 |
+
}
|
| 36 |
+
.builder-badge {
|
| 37 |
+
background: linear-gradient(45deg, #FFD700, #FF6B00);
|
| 38 |
+
color: #1A535C;
|
| 39 |
+
padding: 8px 16px;
|
| 40 |
+
border-radius: 20px;
|
| 41 |
+
font-size: 16px;
|
| 42 |
+
font-weight: bold;
|
| 43 |
+
}
|
| 44 |
+
</style>
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
class PiForgeQualiaOracle:
|
| 48 |
+
def get_qualia_score(self, impact_text: str) -> int:
|
| 49 |
+
# POWERFUL ETHICAL AUDIT ENGINE
|
| 50 |
+
if not impact_text: return 500
|
| 51 |
+
text_lower = impact_text.lower()
|
| 52 |
+
score = 500
|
| 53 |
+
ethical_dimensions = {
|
| 54 |
+
"community": 80, "inclusion": 90, "transparent": 85, "fair": 80,
|
| 55 |
+
"privacy": 75, "education": 75, "help": 70, "empower": 85,
|
| 56 |
+
"decentralized": 65, "open": 70, "accessible": 75
|
| 57 |
+
}
|
| 58 |
+
for principle, boost in ethical_dimensions.items():
|
| 59 |
+
if principle in text_lower: score += boost
|
| 60 |
+
score += min(200, len(impact_text) // 2)
|
| 61 |
+
return min(1000, max(0, score))
|
| 62 |
+
|
| 63 |
+
qualia_oracle = PiForgeQualiaOracle()
|
| 64 |
+
|
| 65 |
+
# π― CORE AUDIT FUNCTIONS (Your Original System)
|
| 66 |
+
def velvet_verdict(a, b):
|
| 67 |
+
a, b = int(a), int(b)
|
| 68 |
+
if a == 0 or b == 0: return 0
|
| 69 |
+
return (2 * a * b) // (a + b)
|
| 70 |
+
|
| 71 |
+
def resonance_narrative(r):
|
| 72 |
+
if r >= 800: return "π Resonance blooms: Sovereign sway achieved"
|
| 73 |
+
if r >= 650: return "π Synthesis stirs: Tender truth tempers the tide"
|
| 74 |
+
if r >= 500: return "π« Echo invites: A gentle balance"
|
| 75 |
+
return "π± Refine the reactive, reflect the reflection"
|
| 76 |
+
|
| 77 |
+
def plot_triad(veracity, qualia, resonance):
|
| 78 |
+
# AUDIT VISUALIZATION
|
| 79 |
+
labels = ['Veracity', 'Qualia', 'Resonance']
|
| 80 |
+
scores = [veracity/1000, qualia/1000, resonance/1000]
|
| 81 |
+
angles = np.linspace(0, 2 * np.pi, len(labels), endpoint=False).tolist()
|
| 82 |
+
scores += scores[:1]
|
| 83 |
+
angles += angles[:1]
|
| 84 |
+
|
| 85 |
+
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))
|
| 86 |
+
ax.plot(angles, scores, color='magenta', linewidth=2)
|
| 87 |
+
ax.fill(angles, scores, color='cyan', alpha=0.25)
|
| 88 |
+
ax.set_xticks(angles[:-1])
|
| 89 |
+
ax.set_xticklabels(labels)
|
| 90 |
+
ax.set_yticklabels([])
|
| 91 |
+
ax.set_facecolor('#1a1a1a')
|
| 92 |
+
fig.patch.set_facecolor('#1a1a1a')
|
| 93 |
+
plt.title('Ethical Triad Harmony', size=14, color='white', y=1.1)
|
| 94 |
+
return fig
|
| 95 |
+
|
| 96 |
+
# π ETHICAL AUDIT SYSTEM
|
| 97 |
+
def simple_ethics_check(project_name, description, impact):
|
| 98 |
+
# ORIGINAL AUDIT FUNCTION
|
| 99 |
+
if not description:
|
| 100 |
+
return 0, "π Please describe your project", "Waiting for your vision..."
|
| 101 |
+
|
| 102 |
+
qualia_score = qualia_oracle.get_qualia_score(impact + " " + description)
|
| 103 |
+
efficiency_score = min(800, len(description) * 2 + 400)
|
| 104 |
+
resonance = velvet_verdict(efficiency_score, qualia_score)
|
| 105 |
+
|
| 106 |
+
if resonance >= 850:
|
| 107 |
+
verdict = "π― EXCELLENT - Strong ethical alignment!"
|
| 108 |
+
confidence = f"Ethical Score: {resonance}/1000 - Community-ready!"
|
| 109 |
+
elif resonance >= 700:
|
| 110 |
+
verdict = "β
VERY GOOD - Positive impact potential"
|
| 111 |
+
confidence = f"Ethical Score: {resonance}/1000 - Solid foundation"
|
| 112 |
+
elif resonance >= 550:
|
| 113 |
+
verdict = "π‘ GOOD - Some refinement opportunities"
|
| 114 |
+
confidence = f"Ethical Score: {resonance}/1000 - Consider community feedback"
|
| 115 |
+
else:
|
| 116 |
+
verdict = "π NEEDS WORK - Rethink ethical approach"
|
| 117 |
+
confidence = f"Ethical Score: {resonance}/1000 - Focus on user benefits"
|
| 118 |
+
|
| 119 |
+
return resonance, verdict, confidence
|
| 120 |
+
|
| 121 |
+
def traverse_triad(values, v_weight, impact_txt):
|
| 122 |
+
# EXPERT AUDIT PHASE II (Fixed: using values as proxy for reactive, v_weight as reactive_val)
|
| 123 |
+
reactive_val = int(v_weight) # Use v_weight as reactive for demo
|
| 124 |
+
qualia_val = qualia_oracle.get_qualia_score(impact_txt)
|
| 125 |
+
resonance_val = velvet_verdict(reactive_val, qualia_val)
|
| 126 |
+
narrative = resonance_narrative(resonance_val)
|
| 127 |
+
plot = plot_triad(reactive_val, qualia_val, resonance_val)
|
| 128 |
+
|
| 129 |
+
return (f"Reactive: {reactive_val}/1000",
|
| 130 |
+
f"Qualia: {qualia_val}/1000",
|
| 131 |
+
f"Resonance: {resonance_val}/1000",
|
| 132 |
+
narrative, plot)
|
| 133 |
+
|
| 134 |
+
# π€ APP BUILDER FUNCTIONS (New Addition)
|
| 135 |
+
class PiForgeAppBuilder:
|
| 136 |
+
def generate_testnet_app(self, app_idea, app_type, features):
|
| 137 |
+
base_boost = 5
|
| 138 |
+
feature_boosts = {"community_governance": 3, "pi_rewards": 2, "transparent_audit": 2}
|
| 139 |
+
total_boost = base_boost + sum(feature_boosts.get(f, 0) for f in features)
|
| 140 |
+
return {
|
| 141 |
+
"testnet_mining_boost": min(15, total_boost),
|
| 142 |
+
"app_id": f"testnet_{hashlib.sha256(f'{app_idea}{datetime.now()}'.encode()).hexdigest()[:12]}"
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
app_builder = PiForgeAppBuilder()
|
| 146 |
+
|
| 147 |
+
def build_testnet_app(app_idea, app_type, selected_features):
|
| 148 |
+
# NEW APP BUILDER FUNCTION
|
| 149 |
+
if not app_idea:
|
| 150 |
+
return "β Please describe your app idea", "", 0, "", ""
|
| 151 |
+
|
| 152 |
+
app_data = app_builder.generate_testnet_app(app_idea, app_type, selected_features)
|
| 153 |
+
ethical_score = qualia_oracle.get_qualia_score(f"{app_idea} with {selected_features}")
|
| 154 |
+
|
| 155 |
+
app_blueprint = f"""
|
| 156 |
+
π **Testnet App Blueprint**
|
| 157 |
+
π― **Ethical Score:** {ethical_score}/1000
|
| 158 |
+
β‘ **Mining Boost:** +{app_data['testnet_mining_boost']}%
|
| 159 |
+
π± **App ID:** {app_data['app_id']}
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
return ("β
Testnet App Blueprint Generated!", app_blueprint,
|
| 163 |
+
app_data['testnet_mining_boost'], app_data['app_id'], "Ready for Testnet!")
|
| 164 |
+
|
| 165 |
+
# ποΈ COMPLETE DUAL-PLATFORM INTERFACE
|
| 166 |
+
st.markdown(PIFORGE_CSS, unsafe_allow_html=True)
|
| 167 |
+
|
| 168 |
+
# Header
|
| 169 |
+
st.markdown("""
|
| 170 |
+
<div class="piforge-premium-header">
|
| 171 |
+
<h1>π¨ Ο Ai Forge Dual-Platform</h1>
|
| 172 |
+
<h3>Ethical Audit System + AI App Builder</h3>
|
| 173 |
+
<p><span class='audit-badge'>π ETHICAL AUDIT</span> <span class='builder-badge'>π€ APP BUILDER</span></p>
|
| 174 |
+
</div>
|
| 175 |
+
""", unsafe_allow_html=True)
|
| 176 |
+
|
| 177 |
+
# Mode selector (though using tabs, keep for future)
|
| 178 |
+
mode = st.radio(
|
| 179 |
+
"Choose Ai Forge Platform",
|
| 180 |
+
options=["π Ethical Audit", "π€ AI App Builder", "π Portfolio"],
|
| 181 |
+
index=0
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
# Tabs for main sections
|
| 185 |
+
tab1, tab2 = st.tabs(["π Ethical Audit", "π€ AI App Builder"])
|
| 186 |
+
|
| 187 |
+
with tab1:
|
| 188 |
+
st.markdown("### π Ethical Audit System")
|
| 189 |
+
st.markdown("*Your original powerful ethical assessment platform*")
|
| 190 |
+
|
| 191 |
+
# Simple Audit
|
| 192 |
+
with st.expander("π Simple Audit", expanded=True):
|
| 193 |
+
col1, col2 = st.columns(2)
|
| 194 |
+
with col1:
|
| 195 |
+
project_name = st.text_input("Project Name", value="Community Marketplace")
|
| 196 |
+
description = st.text_area("Description", height=100)
|
| 197 |
+
impact = st.text_area("Impact", height=70)
|
| 198 |
+
if st.button("π Run Ethical Audit", type="primary"):
|
| 199 |
+
score, verdict, analysis = simple_ethics_check(project_name, description, impact)
|
| 200 |
+
st.session_state.simple_score = score
|
| 201 |
+
st.session_state.simple_verdict = verdict
|
| 202 |
+
st.session_state.simple_analysis = analysis
|
| 203 |
+
|
| 204 |
+
with col2:
|
| 205 |
+
if 'simple_score' in st.session_state:
|
| 206 |
+
st.metric("Ethical Score", st.session_state.simple_score, delta=None)
|
| 207 |
+
st.text_area("Verdict", value=st.session_state.simple_verdict, height=70)
|
| 208 |
+
st.text_area("Analysis", value=st.session_state.simple_analysis, height=100)
|
| 209 |
+
|
| 210 |
+
# Expert Audit
|
| 211 |
+
with st.expander("π¬ Expert Audit"):
|
| 212 |
+
col1, col2 = st.columns(2)
|
| 213 |
+
with col1:
|
| 214 |
+
values = st.text_input("Core Values")
|
| 215 |
+
assumptions = st.text_input("Assumptions")
|
| 216 |
+
impact_text = st.text_area("Impact Narrative", height=100)
|
| 217 |
+
v_weight = st.slider("Veracity Weight", 0, 1000, 600)
|
| 218 |
+
q_weight = st.slider("Qualia Weight", 0, 1000, 400)
|
| 219 |
+
if st.button("π Traverse Ethical Triad", type="primary"):
|
| 220 |
+
verity_out, qualia_out, resonance_out, narrative_out, plot_fig = traverse_triad(values, v_weight, impact_text)
|
| 221 |
+
st.session_state.expert_verity = verity_out
|
| 222 |
+
st.session_state.expert_qualia = qualia_out
|
| 223 |
+
st.session_state.expert_resonance = resonance_out
|
| 224 |
+
st.session_state.expert_narrative = narrative_out
|
| 225 |
+
st.session_state.expert_plot = plot_fig
|
| 226 |
+
|
| 227 |
+
with col2:
|
| 228 |
+
if 'expert_verity' in st.session_state:
|
| 229 |
+
st.text_area("Reactive Echo", value=st.session_state.expert_verity, height=50)
|
| 230 |
+
st.text_area("Tender Reflection", value=st.session_state.expert_qualia, height=50)
|
| 231 |
+
st.text_area("Resonance Value", value=st.session_state.expert_resonance, height=50)
|
| 232 |
+
st.text_area("Velvet Verdict", value=st.session_state.expert_narrative, height=70)
|
| 233 |
+
st.pyplot(st.session_state.expert_plot)
|
| 234 |
+
|
| 235 |
+
with tab2:
|
| 236 |
+
st.markdown("### π Testnet AI App Builder")
|
| 237 |
+
st.markdown("*Build Pi Testnet apps with ethical scoring*")
|
| 238 |
+
|
| 239 |
+
col1, col2 = st.columns(2)
|
| 240 |
+
with col1:
|
| 241 |
+
app_idea = st.text_area("App Idea", height=100)
|
| 242 |
+
app_type = st.selectbox("App Type", options=["marketplace", "education", "gaming"])
|
| 243 |
+
features = st.multiselect("Select Features", options=["community_governance", "pi_rewards", "transparent_audit"])
|
| 244 |
+
if st.button("π¨ Build Testnet App", type="primary"):
|
| 245 |
+
build_status, blueprint, boost, app_id, status_msg = build_testnet_app(app_idea, app_type, features)
|
| 246 |
+
st.session_state.build_status = build_status
|
| 247 |
+
st.session_state.blueprint = blueprint
|
| 248 |
+
st.session_state.boost = boost
|
| 249 |
+
st.session_state.app_id = app_id
|
| 250 |
+
st.session_state.status_msg = status_msg
|
| 251 |
+
|
| 252 |
+
with col2:
|
| 253 |
+
if 'build_status' in st.session_state:
|
| 254 |
+
st.text_area("Build Status", value=st.session_state.build_status, height=50)
|
| 255 |
+
st.text_area("App Blueprint", value=st.session_state.blueprint, height=200)
|
| 256 |
+
st.metric("Mining Boost %", st.session_state.boost, delta=None)
|
| 257 |
+
st.text_input("App ID", value=st.session_state.app_id, disabled=True)
|
| 258 |
+
st.caption(st.session_state.status_msg)
|