Spaces:
Sleeping
Sleeping
Download app.py from StrategyStack/README: direct link, hf CLI and curl.
- Browser
- Download file 8.72 kB
-
https://huggingface.co/spaces/StrategyStack/README/resolve/62ca4d3afbbe8bdb2aa27babc68ce85f60f29e06/app.py
- Command line
-
hf download hf://spaces/StrategyStack/README@62ca4d3afbbe8bdb2aa27babc68ce85f60f29e06/app.py
-
curl -L -o app.py https://huggingface.co/spaces/StrategyStack/README/resolve/62ca4d3afbbe8bdb2aa27babc68ce85f60f29e06/app.py
8.72 kB
| import gradio as gr | |
| import sys | |
| import os | |
| import json | |
| from typing import List, Dict, Any, Optional | |
| from pathlib import Path | |
| # --- Data Storage Configuration --- | |
| DATA_DIR = Path("/data") | |
| USER_DATA_DIR = DATA_DIR / "users" | |
| USER_DATA_DIR.mkdir(parents=True, exist_ok=True) | |
| def get_user_dir(username: str) -> Path: | |
| """Get or create user-specific data directory""" | |
| user_dir = USER_DATA_DIR / username | |
| user_dir.mkdir(parents=True, exist_ok=True) | |
| return user_dir | |
| def load_user_data(username: str, data_type: str) -> Any: | |
| """Load user-specific data from their directory""" | |
| user_dir = get_user_dir(username) | |
| data_file = user_dir / f"{data_type}.json" | |
| if data_file.exists(): | |
| with open(data_file, 'r') as f: | |
| return json.load(f) | |
| return [] | |
| def save_user_data(username: str, data_type: str, data: Any): | |
| """Save user-specific data to their directory""" | |
| user_dir = get_user_dir(username) | |
| data_file = user_dir / f"{data_type}.json" | |
| with open(data_file, 'w') as f: | |
| json.dump(data, f, indent=2) | |
| # --- Consent/Event Audit Panel --- | |
| def consent_audit_panel(consent_events: List[Dict[str, Any]], profile: Optional[gr.OAuthProfile]) -> str: | |
| """ | |
| UI Panel to show all consent events, opt-outs, and compliance flags per contact and batch. | |
| Accepts a list of consent event dicts with structure as returned from backend JSON. | |
| """ | |
| if profile is None: | |
| return "# π Authentication Required\n\nPlease sign in with your Hugging Face account to access this feature." | |
| username = profile.username | |
| # Load user-specific consent events | |
| user_events = load_user_data(username, "consent_events") | |
| if not user_events: | |
| user_events = consent_events # Use provided sample if no user data | |
| audit_md = f"# Consent/Event Audit\n\n**User:** {username}\n\nPer-contact and batch consent history." | |
| for event in user_events: | |
| audit_md += f"\n- Contact: {event.get('contact_id','?')} | Event: {event.get('event_type','?')} | Opt-out: {event.get('opt_out','?')}" | |
| return gr.Markdown(audit_md) | |
| # --- Dynamic Branching Composer for Cadence Editing --- | |
| def branching_composer(branches: List[Dict[str, Any]], profile: Optional[gr.OAuthProfile]) -> str: | |
| """ | |
| Visual sequence builder for editing cadences. | |
| Structure assumed to be a list of dict branches with JSON mirroring backend. | |
| """ | |
| if profile is None: | |
| return "# π Authentication Required\n\nPlease sign in with your Hugging Face account to access this feature." | |
| username = profile.username | |
| # Load user-specific branches | |
| user_branches = load_user_data(username, "branches") | |
| if not user_branches: | |
| user_branches = branches # Use provided sample if no user data | |
| builder_md = f"# Dynamic Cadence Branching\n\n**User:** {username}\n\n## Sequence Tree:" | |
| for b in user_branches: | |
| builder_md += f"\n- Step {b.get('step','?')}: {b.get('description','?')} (Next: {b.get('next','?')})" | |
| return gr.Markdown(builder_md) | |
| # --- Live Diagnostics Panel for Cadence Errors/Health --- | |
| def diagnostics_panel(events: List[Dict[str, Any]], profile: Optional[gr.OAuthProfile]) -> str: | |
| """ | |
| Incident/event log UI widget to show errors and health of cadence system. | |
| Format is a simple list of event dicts from backend state. | |
| """ | |
| if profile is None: | |
| return "# π Authentication Required\n\nPlease sign in with your Hugging Face account to access this feature." | |
| username = profile.username | |
| # Load user-specific diagnostic events | |
| user_events = load_user_data(username, "diagnostics") | |
| if not user_events: | |
| user_events = events # Use provided sample if no user data | |
| diag_md = f"# Live Diagnostics: Cadence Health\n\n**User:** {username}" | |
| for e in user_events: | |
| diag_md += f"\n- [{e.get('timestamp','?')}] {e.get('type','?')}: {e.get('message','?')} {'[ERROR]' if e.get('level')=='error' else ''}" | |
| return gr.Markdown(diag_md) | |
| # --- Audit Trail Export Widget --- | |
| def audit_trail_widget(profile: Optional[gr.OAuthProfile]) -> str: | |
| """ | |
| CSV export widget for compliance officer. | |
| Returns download button for current batch consent data. | |
| """ | |
| if profile is None: | |
| return "# π Authentication Required\n\nPlease sign in with your Hugging Face account to access this feature." | |
| username = profile.username | |
| export_md = f"# Audit Trail Export\n\n**User:** {username}\n\nDownload compliance reports in CSV format.\n\n*Export functionality will be implemented based on your specific requirements.*" | |
| return gr.Markdown(export_md) | |
| # Sample Data (for demo purposes when no user data exists) | |
| consent_events_sample = [ | |
| {"contact_id": "c123", "event_type": "opt_in", "opt_out": False, "timestamp": "2025-01-10"}, | |
| {"contact_id": "c456", "event_type": "opt_out", "opt_out": True, "timestamp": "2025-01-11"}, | |
| {"contact_id": "c789", "event_type": "consent_updated", "opt_out": False, "timestamp": "2025-01-12"}, | |
| ] | |
| branches_sample = [ | |
| {"step": 1, "description": "Initial outreach email", "next": 2}, | |
| {"step": 2, "description": "Follow-up call", "next": 3}, | |
| {"step": 3, "description": "Send proposal", "next": 4}, | |
| {"step": 4, "description": "Close or nurture", "next": None}, | |
| ] | |
| diagnostics_sample = [ | |
| {"timestamp": "2025-01-15 10:30", "type": "rate_limit", "message": "LinkedIn API approaching limit (85%)", "level": "warning"}, | |
| {"timestamp": "2025-01-15 11:00", "type": "success", "message": "Batch processed successfully", "level": "info"}, | |
| {"timestamp": "2025-01-15 11:30", "type": "error", "message": "Failed to send email to c999", "level": "error"}, | |
| ] | |
| audit_history_sample = [ | |
| {"batch_id": "b001", "contacts": 150, "opt_outs": 5, "timestamp": "2025-01-10"}, | |
| {"batch_id": "b002", "contacts": 200, "opt_outs": 8, "timestamp": "2025-01-12"}, | |
| ] | |
| # --- Main Application --- | |
| with gr.Blocks(title="StrategyStack UI") as demo: | |
| gr.Markdown("# π StrategyStack Recovery, Enrichment & Outreach Platform") | |
| # OAuth Login Button | |
| gr.LoginButton() | |
| gr.Markdown(""" | |
| ## Welcome to StrategyStack | |
| **Customer-Ready Multi-Tenant Platform** | |
| - β **Secure Authentication**: Sign in with your Hugging Face account | |
| - β **Data Isolation**: Your data is stored separately and securely | |
| - β **Privacy First**: We don't store your data centrally - you own it | |
| - β **Compliance Ready**: Full audit trails and consent management | |
| Please sign in above to access your personalized dashboard. | |
| """) | |
| # Create tabbed interface | |
| with gr.Tabs(): | |
| with gr.Tab("Consent/Event Audit"): | |
| consent_output = gr.Markdown() | |
| consent_refresh = gr.Button("Refresh Data") | |
| user_profile_1 = gr.State() | |
| with gr.Tab("Cadence Branching Composer"): | |
| branching_output = gr.Markdown() | |
| branching_refresh = gr.Button("Refresh Data") | |
| user_profile_2 = gr.State() | |
| with gr.Tab("Live Diagnostics"): | |
| diagnostics_output = gr.Markdown() | |
| diagnostics_refresh = gr.Button("Refresh Data") | |
| user_profile_3 = gr.State() | |
| with gr.Tab("Audit Trail Export"): | |
| audit_output = gr.Markdown() | |
| audit_refresh = gr.Button("Refresh Data") | |
| user_profile_4 = gr.State() | |
| # Load initial data | |
| def load_consent_data(profile: Optional[gr.OAuthProfile] = None): | |
| return consent_audit_panel(consent_events_sample, profile) | |
| def load_branching_data(profile: Optional[gr.OAuthProfile] = None): | |
| return branching_composer(branches_sample, profile) | |
| def load_diagnostics_data(profile: Optional[gr.OAuthProfile] = None): | |
| return diagnostics_panel(diagnostics_sample, profile) | |
| def load_audit_data(profile: Optional[gr.OAuthProfile] = None): | |
| return audit_trail_widget(profile) | |
| # Set up event handlers | |
| demo.load(load_consent_data, inputs=None, outputs=consent_output) | |
| demo.load(load_branching_data, inputs=None, outputs=branching_output) | |
| demo.load(load_diagnostics_data, inputs=None, outputs=diagnostics_output) | |
| demo.load(load_audit_data, inputs=None, outputs=audit_output) | |
| consent_refresh.click(load_consent_data, inputs=None, outputs=consent_output) | |
| branching_refresh.click(load_branching_data, inputs=None, outputs=branching_output) | |
| diagnostics_refresh.click(load_diagnostics_data, inputs=None, outputs=diagnostics_output) | |
| audit_refresh.click(load_audit_data, inputs=None, outputs=audit_output) | |
| if __name__ == "__main__": | |
| demo.launch() |