# { "Depends": "py-genlayer:1jb45aa8ynh2a9c9xn3b7qqh8sm5q93hwfp7jqmwsfhh8jpz09h6" }
from genlayer import *
import typing
import json
class AISmartModerator(gl.Contract):
# Storage: mapping from content_id to analysis result
analyses: TreeMap[str, str] # content_id -> JSON result
def __init__(self):
pass
@gl.public.write
def analyze_content(self, content_id: str, text: str) -> str:
"""
AI-Powered Analysis using LLM
"""
def run_ai_analysis() -> str:
prompt = f"""
Analyze the following text and return a valid JSON object with this exact structure:
{{
"sentiment": "positive" | "negative" | "neutral",
"toxicity_score": 0.0 to 1.0,
"category": "news" | "spam" | "complaint" | "praise" | "question" | "other",
"summary": "One short sentence summarizing the content",
"should_flag": true | false
}}
Text to analyze:
{text}
"""
# Call LLM (non-deterministic)
raw_result = gl.nondet.exec_prompt(prompt, response_format="json")
# Ensure it's valid JSON string for storage
return json.dumps(raw_result, sort_keys=True)
# Use GenLayer's equivalence principle for consensus
result_json = gl.eq_principle.strict_eq(run_ai_analysis)
# Store on-chain
self.analyses[content_id] = result_json
print(f"✅ Analysis completed for {content_id}")
return result_json
@gl.public.view
def get_analysis(self, content_id: str) -> typing.Optional[dict]:
"""Retrieve previous AI analysis"""
if content_id in self.analyses:
return json.loads(self.analyses[content_id])
return None
@gl.public.view
def get_all_analyzed_ids(self) -> list:
"""List all content that has been analyzed"""
return list(self.analyses.keys())