""" ContentGuardian - AI-Powered Content Audit Agent China · Simplified Chinese · Hanyu Xinxie Style · AI-Driven Version Real AI content analysis with SVG visualization """ import gradio as gr import asyncio import aiohttp import json import os import datetime from typing import Dict, List # AI Client for content analysis class AIContentAnalyzer: def __init__(self): self.api_key = os.getenv("SILICONFLOW_API_KEY", "sk-your-api-key-here") self.base_url = "https://api.siliconflow.cn/v1/chat/completions" self.model = "Qwen/Qwen2.5-7B-Instruct" async def analyze_content(self, text: str, keywords: List[str] = None) -> Dict: """AI-powered content analysis""" keywords_str = ", ".join(keywords) if keywords else "无" prompt = f""" 你是专业的内容审核专家,请对以下文本进行全面审核分析: 文本内容:{text} 关键词:{keywords_str} 请从以下维度进行分析: 1. 错别字检测:识别文本中的拼写错误和用词不当 2. 违规内容:检测不当言论、违法违规表述 3. 虚假内容:识别可能的虚假信息和夸大宣传 4. 不当广告用语:检测违反广告法的用词 5. 关键词分析:分析指定关键词的使用情况 请以JSON格式返回分析结果: {{ "typos": [ {{"original": "错误词", "correct": "正确词", "reason": "错误原因"}} ], "violations": [ {{"content": "违规内容", "type": "违规类型", "suggestion": "修改建议"}} ], "fake_content": [ {{"content": "虚假内容", "type": "虚假类型", "suggestion": "修改建议"}} ], "inappropriate_ads": [ {{"word": "不当用词", "reason": "违规原因", "suggestion": "替代建议"}} ], "keywords_analysis": [ {{"keyword": "关键词", "frequency": 次数, "context": "使用语境", "assessment": "使用评价"}} ], "overall_assessment": {{ "risk_level": "低/中/高", "total_issues": 问题总数, "main_concerns": ["主要问题1", "主要问题2"], "recommendations": ["建议1", "建议2"] }} }} """ try: headers = { "Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json" } data = { "model": self.model, "messages": [ {"role": "system", "content": "你是专业的内容审核专家,擅长识别各类内容问题并提供专业建议。"}, {"role": "user", "content": prompt} ], "temperature": 0.1, "max_tokens": 2000 } async with aiohttp.ClientSession() as session: async with session.post(self.base_url, headers=headers, json=data) as response: if response.status == 200: result = await response.json() ai_response = result["choices"][0]["message"]["content"] # 尝试解析JSON try: # 清理AI返回的内容 if "```json" in ai_response: ai_response = ai_response.split("```json")[1].split("```")[0] elif "```" in ai_response: ai_response = ai_response.split("```")[1].split("```")[0] analysis_result = json.loads(ai_response.strip()) return analysis_result except json.JSONDecodeError: # 如果JSON解析失败,返回基础分析 return self._create_fallback_analysis(text, keywords) else: return self._create_fallback_analysis(text, keywords) except Exception as e: print(f"AI analysis error: {e}") return self._create_fallback_analysis(text, keywords) def _create_fallback_analysis(self, text: str, keywords: List[str] = None) -> Dict: """备用分析方法""" # 简单的关键词检测作为备用 inappropriate_words = ["绝对", "完全", "100%", "第一", "最好", "立即", "马上"] found_inappropriate = [word for word in inappropriate_words if word in text] return { "typos": [], "violations": [], "fake_content": [], "inappropriate_ads": [{"word": word, "reason": "可能违反广告法", "suggestion": "使用相对性表述"} for word in found_inappropriate], "keywords_analysis": [{"keyword": kw, "frequency": text.count(kw), "context": "文本中", "assessment": "正常使用"} for kw in (keywords or []) if kw in text], "overall_assessment": { "risk_level": "中" if found_inappropriate else "低", "total_issues": len(found_inappropriate), "main_concerns": ["广告用语不当"] if found_inappropriate else [], "recommendations": ["修改绝对化表述"] if found_inappropriate else ["内容表述规范"] } } # 全局AI分析器 ai_analyzer = AIContentAnalyzer() def generate_hanyu_xinxie_cards(analysis_result: Dict, original_text: str) -> str: """生成汉语新解风格的SVG卡片""" overall = analysis_result.get("overall_assessment", {}) total_issues = overall.get("total_issues", 0) risk_level = overall.get("risk_level", "低") # 莫兰迪色系配色 colors = { "primary": "#B6B5A7", # 莫兰迪灰褐色 "secondary": "#9A8F8F", # 莫兰迪灰棕色 "accent": "#C5B4A0", # 莫兰迪淡棕色 "background": "#F2EDE9", # 莫兰迪浅米色 "text": "#5B5B5B", # 莫兰迪深灰色 "light_text": "#8C8C8C", # 莫兰迪中灰色 "divider": "#D1CBC3" # 莫兰迪浅灰色 } # 根据风险等级调整颜色 if risk_level == "高": colors["secondary"] = "#B85450" # 深红色 colors["accent"] = "#D4776B" # 浅红色 elif risk_level == "中": colors["secondary"] = "#C4965A" # 橙色 colors["accent"] = "#D4A574" # 浅橙色 # 生成主要摘要卡片 main_card = f"""
AI分析过程中遇到问题:
{error_msg}
请稍后重试或检查网络连接