wisdom-canvas / app.py
YSenseAI's picture
Create app.py
ca5b2b2 verified
Raw
History Blame Contribute Delete
9.17 kB
"""
YSenseAI Wisdom Canvas Demo v4.5-Beta
A demonstration of the Story-First UX for ethical AI training data collection.
"""
import gradio as gr
import hashlib
import uuid
import json
from datetime import datetime
def generate_attribution(story, author_name):
timestamp = datetime.utcnow().isoformat() + "Z"
content_hash = hashlib.sha256(story.encode('utf-8')).hexdigest()
did = f"did:ysense:{uuid.uuid4().hex[:16]}"
return {
"did": did,
"content_hash": content_hash,
"author": author_name or "Anonymous",
"timestamp": timestamp,
"version": "v4.5-beta",
"protocol": "Z-Protocol v2.0"
}
def analyze_perception_layers(story):
word_count = len(story.split())
sentences = story.split('.')
return {
"narrative": f"A personal story with {len(sentences)} key moments, exploring meaningful experiences.",
"somatic": "Physical presence and embodied experience are woven throughout the narrative.",
"attention": "The author's focus reveals what matters most in this moment of reflection.",
"synesthetic": "Sensory details paint a vivid picture of the experience.",
"temporal": f"The story unfolds across time, with {word_count} words capturing the journey."
}
def distill_essence(story, layers):
words = story.lower().split()
common = {'about', 'there', 'would', 'could', 'should', 'their', 'these', 'those', 'which', 'where', 'being'}
meaningful = [w.strip('.,!?') for w in words if len(w) > 5 and w not in common][:3]
if len(meaningful) < 3:
meaningful = ["wisdom", "growth", "journey"]
return {
"essence": " ".join(meaningful[:3]).title(),
"meaning": "These words capture the core themes of your story.",
"wisdom_type": "personal growth"
}
def calculate_quality_metrics(story, layers, essence):
word_count = len(story.split())
sentence_count = len([s for s in story.split('.') if s.strip()])
metrics = {
"authenticity": min(100, 50 + (word_count // 10)),
"emotional_depth": min(100, 60 + len(layers.get("somatic", "")) // 2),
"narrative_clarity": min(100, 70 if sentence_count > 3 else 50),
"cultural_richness": min(100, 55 + (word_count // 20)),
"wisdom_density": min(100, 65 if essence.get("wisdom_type") else 45),
"training_value": 0
}
metrics["training_value"] = sum(list(metrics.values())[:-1]) // 5
return metrics
def process_story(story, author_name, consent_research, consent_commercial, consent_derivative, consent_attribution):
if not story or len(story.strip()) < 50:
return "Please write at least 50 characters to analyze.", "", "", "", "", ""
attribution = generate_attribution(story, author_name)
attribution_display = f"""### Attribution Record
| Field | Value |
|-------|-------|
| **DID** | `{attribution['did']}` |
| **Content Hash** | `{attribution['content_hash'][:32]}...` |
| **Author** | {attribution['author']} |
| **Timestamp** | {attribution['timestamp']} |
| **Protocol** | {attribution['protocol']} |"""
layers = analyze_perception_layers(story)
layers_display = f"""### 5-Layer Perception Analysis
**Narrative Layer**: {layers.get('narrative', 'N/A')}
**Somatic Layer**: {layers.get('somatic', 'N/A')}
**Attention Layer**: {layers.get('attention', 'N/A')}
**Synesthetic Layer**: {layers.get('synesthetic', 'N/A')}
**Temporal Layer**: {layers.get('temporal', 'N/A')}"""
essence = distill_essence(story, layers)
essence_display = f"""### 3-Word Essence
# {essence.get('essence', 'Wisdom Awaits You')}
**Meaning:** {essence.get('meaning', 'Your story holds unique wisdom.')}
**Wisdom Type:** {essence.get('wisdom_type', 'personal growth').title()}"""
metrics = calculate_quality_metrics(story, layers, essence)
metrics_display = f"""### Quality Metrics
| Metric | Score |
|--------|-------|
| Authenticity | {metrics['authenticity']}% |
| Emotional Depth | {metrics['emotional_depth']}% |
| Narrative Clarity | {metrics['narrative_clarity']}% |
| Cultural Richness | {metrics['cultural_richness']}% |
| Wisdom Density | {metrics['wisdom_density']}% |
| **Training Value** | **{metrics['training_value']}%** |"""
consent_types = []
if consent_research: consent_types.append("Research Use")
if consent_commercial: consent_types.append("Commercial Training")
if consent_derivative: consent_types.append("Derivative Works")
if consent_attribution: consent_types.append("Public Attribution")
if not consent_types: consent_types.append("No consent granted")
consent_display = f"""### Z-Protocol v2.0 Consent
{chr(10).join(['- ' + c for c in consent_types])}
**Consent Hash:** `{hashlib.sha256(str(consent_types).encode()).hexdigest()[:16]}`
**Revocable:** Yes"""
export_data = {
"attribution": attribution,
"layers": layers,
"essence": essence,
"metrics": metrics,
"consent": {"research": consent_research, "commercial": consent_commercial, "derivative": consent_derivative, "attribution": consent_attribution}
}
export_display = f"""### Export Preview
```json
{json.dumps(export_data, indent=2)}
```"""
return attribution_display, layers_display, essence_display, metrics_display, consent_display, export_display
example_stories = [
["My grandmother taught me to make dumplings when I was seven. Her hands, weathered from decades of work, moved with a grace I couldn't understand then. She never measured anything - a pinch of this, a handful of that. Years later, standing in my own kitchen, I finally understood: she wasn't teaching me to cook. She was passing down a language of love that words could never capture.", "Anonymous"],
["The day I failed my first exam, my father didn't say a word. He just took me fishing. We sat by the lake for hours in silence. When the sun set, he finally spoke: 'The fish don't care about your grades. Neither do I. I care about who you become.' That silence taught me more than any lecture ever could.", "Anonymous"]
]
with gr.Blocks(title="YSenseAI Wisdom Canvas") as demo:
gr.HTML("""<div style="text-align:center;padding:20px;background:linear-gradient(135deg,#0f172a,#1e293b);border-radius:12px;margin-bottom:20px;">
<h1 style="color:#f59e0b;font-size:2.5em;margin-bottom:10px;">YSenseAI Wisdom Canvas</h1>
<p style="color:#94a3b8;font-size:1.1em;">v4.5-Beta | Share your stories. Preserve your wisdom. Shape ethical AI.</p>
<p style="margin-top:10px;"><a href="https://ysenseai.org" target="_blank" style="color:#22d3ee;margin-right:15px;">Website</a>
<a href="https://github.com/creator35lwb-web/YSense-AI-Attribution-Infrastructure" target="_blank" style="color:#22d3ee;margin-right:15px;">GitHub</a>
<a href="https://doi.org/10.5281/zenodo.17737995" target="_blank" style="color:#22d3ee;">White Paper</a></p></div>""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## Story Canvas")
story_input = gr.Textbox(label="Your Story", placeholder="Share a meaningful moment...", lines=8)
author_input = gr.Textbox(label="Author Name (optional)", placeholder="Anonymous", lines=1)
gr.Markdown("### Z-Protocol Consent")
with gr.Row():
consent_research = gr.Checkbox(label="Research Use", value=True)
consent_commercial = gr.Checkbox(label="Commercial Training", value=False)
with gr.Row():
consent_derivative = gr.Checkbox(label="Derivative Works", value=True)
consent_attribution = gr.Checkbox(label="Public Attribution", value=False)
analyze_btn = gr.Button("Analyze & Distill", variant="primary")
gr.Examples(examples=example_stories, inputs=[story_input, author_input])
with gr.Column(scale=1):
gr.Markdown("## Analysis Results")
with gr.Accordion("Attribution", open=True):
attribution_output = gr.Markdown()
with gr.Accordion("5-Layer Analysis", open=True):
layers_output = gr.Markdown()
with gr.Accordion("3-Word Essence", open=True):
essence_output = gr.Markdown()
with gr.Accordion("Quality Metrics", open=False):
metrics_output = gr.Markdown()
with gr.Accordion("Consent Record", open=False):
consent_output = gr.Markdown()
with gr.Accordion("Export Preview", open=False):
export_output = gr.Markdown()
analyze_btn.click(fn=process_story, inputs=[story_input, author_input, consent_research, consent_commercial, consent_derivative, consent_attribution], outputs=[attribution_output, layers_output, essence_output, metrics_output, consent_output, export_output])
gr.HTML("""<footer style="text-align:center;padding:20px;color:#64748b;"><p>2025 YSenseAI | MIT License | <a href="https://doi.org/10.5281/zenodo.17737995" target="_blank">DOI: 10.5281/zenodo.17737995</a></p></footer>""")
if __name__ == "__main__":
demo.launch()