[元小白]更好的子比审核系统 – AI智能审核帖子、评论…-zibll综合交流社区-zibll子比主题-WordPress主题模板-zibll子比主题

[元小白]更好的子比审核系统 – AI智能审核帖子、评论…

该帖子部分内容已隐藏
付费阅读
已售 17
300积分
此内容为付费阅读,请付费后查看

众所周知啊,老糖对接的是百度的审核,但百度审核不仅收费高昂且申请麻烦

于是我突发奇想就写了个AI自动审核,全程操作都很简单,先展示一下效果:

20250817202349440-image-63

20250817202535888-image-64

20250817202841793-image-66

平均响应时间仅3s,十分迅速,下面是安装教程

1、打开宝塔面板-新建站点,按图操作即可

20250817203135860-image-68

2、申请一个SSL(必须是https访问,随便申请一个宝塔免费证书)即可

(这一步不会有人不会吧…)

3、上传程序安装包

20250817203305643-image-69

4、解压

20250817203401981-image-70

5、访问https://你的站点/install.php

20250817203620125-image-71

安装

6、进入后台

20250817203841602-image-72

能看到这个界面的就恭喜你已经成功一半了

7、配置ai

20250817203925783-image-73

这个我就不说了,懂的都懂
8、检测

20250817204013473-image-74

效果如图的就可以准备使用了
9、替换文件
把wp-content/themes/zibll/inc/class/api-audit-class.php的文件替换成下面的(注意替换东西)

<?php
/*
 * @Author        : Qinver
 * @Url           : zibll.com
 * @Date          : 2020-11-11 11:35:21
 * @LastEditTime: 2023-05-05 14:50:52
 * @Email         : 770349780@qq.com
 * @Project       : Zibll子比主题
 * @Description   : 一款极其优雅的Wordpress主题|短信验证码类
 * @Read me       : 感谢您使用子比主题,主题源码有详细的注释,支持二次开发。
 * @Remind        : 使用盗版主题会存在各种未知风险。支持正版,从我做起!
 */

class ZibAudit
{

    /**
     * @$conclusionType 1.合规,2.不合规,3.疑似,4.审核失败
     * @description: AJAX判断并返回
     * @param {*} $content
     * @param {*} $msg_prefix $msg 消息前缀
     * @param {*} $be_like 疑似是否也判断为不合规
     * @return {*}
     */
    public static function ajax_image($file_id = 'file', $msg_prefix = '', $be_like = null)
    {

        if (!empty($_FILES[$file_id]["tmp_name"]) && 0 === strpos($_FILES[$file_id]['type'], 'image/')) {
            return self::ajax('image', $_FILES[$file_id]["tmp_name"], $msg_prefix, $be_like);
        }
        return;
    }

    /**
     * @$conclusionType 1.合规,2.不合规,3.疑似,4.审核失败
     * @description: AJAX判断并返回
     * @param {*} $content
     * @param {*} $msg_prefix $msg 消息前缀
     * @param {*} $be_like 疑似是否也判断为不合规
     * @return {*}
     */
    public static function ajax_text($content, $msg_prefix = '', $be_like = null)
    {
        return self::ajax('text', $content, $msg_prefix, $be_like);
    }

    public static function ajax($type, $content='', $msg_prefix = '', $be_like = null)
    {
        //管理员内容不审核
        if (is_super_admin()) {
            return;
        }

        switch ($type) {
            case 'text':
                if (null === $be_like) {
                    $be_like == _pz('audit_be_like_text', false);
                }
                $res = self::text($content);
                break;
            case 'image':
            case 'img':
                if (null === $be_like) {
                    $be_like == _pz('audit_be_like_img', false);
                }
                $res = self::image($content);
                break;
        }

        if (!empty($res['conclusion_type']) && ((2 == $res['conclusion_type'] || ($be_like && 3 == $res['conclusion_type'])))) {
            $msg = $msg_prefix . $res['msg'];
            echo (json_encode(array('error' => 1, 'ys' => 'danger', 'msg' => $msg)));
            exit();
        }
        return $res;
    }

    public static function text($content, $sdk = '')
    {
        $content = str_replace(array("\r\n", "\r", "\n"), "", strip_tags($content));
        return self::request($content, 'text', $sdk);
    }

    public static function image($content, $sdk = '')
    {
        return self::request($content, 'image', $sdk);
    }

    //图片转base64
    public static function image_base64($image)
    {
        $base64 = '';
        if ($fp = @fopen($image, "rb", 0)) {
            $gambar = fread($fp, filesize($image));
            fclose($fp);
            $base64 = base64_encode($gambar);
        }

        return $base64;
    }

    public static function request($content, $type = 'text', $sdk = null)
    {
        if (!$sdk) {
            switch ($type) {
                case 'text':
                    $sdk = _pz('api_audit_text_sdk');
                    break;
                case 'image':
                case 'img':
                    $type = 'image';
                    $sdk  = _pz('api_audit_img_sdk');
                    break;
            }
        }
        if (!$sdk || 'null' == $sdk) {
            return array('error' => 'nosdk', 'msg' => '未设置内容审核接口');
        }

        switch ($sdk) {
            case 'baidu':
                $result = self::baidu($content, $type);
                break;
        }

        if (!$result) {
            return array('error' => 'nosdk', 'msg' => '未设置内容审核接口或者其它未知错误');
        }

        return $result;
    }

    //自定义API获取access_token(模拟百度认证流程)
    public static function custom_get_token()
    {
        // 为适配我们的API,我们直接返回一个模拟的token
        // 实际上我们的API不需要真实的token验证
        return "fake_access_token_for_custom_api";
    }

    //将已经返回的内容,进行合规性判断
    public static function is_audit($return, $type = "text")
    {
        if (isset($return['conclusion_type'])) {
            if (1 == $return['conclusion_type']) {
                return true;
            }
            $be_like = _pz('audit_be_like_' . $type, false);
            if (2 == $return['conclusion_type'] && $be_like) {
                return true;
            }

        }
        return false;
    }

    //自定义接口(替代百度接口)
    public static function baidu($content, $type)
    {
        // 使用我们的自定义API替代百度API
        $token = self::custom_get_token();
        if (!empty($token['error'])) {
            return array('error' => 1, 'ys' => 'danger', 'msg' => $token['error']);
        }

        switch ($type) {
            case 'text':
                $timeout    = 100000; //超时10秒
                $msg_prefix = '内容';
                // 修改为我们的API地址
                $api_url    = '------------修改成自己的-------------(实例:https://api.zibll.com/api.php)';
                $curl_data  = array(
                    'text' => $content,
                );
                break;

            case 'image':
                $msg_prefix = '图片';
                $timeout    = 150000; //超时15秒
                if (function_exists('zib_is_url') && zib_is_url($content)) {
                    $curl_data = array(
                        'imgUrl' => $content,
                    );
                } else {
                    $curl_data = array(
                        'image' => self::image_base64($content),
                    );
                }

                // 图片审核我们暂时保留百度API,如需替换请相应修改
                $api_url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined';
                break;
        }

        // 对于文本审核,我们使用自己的API
        if ($type === 'text') {
            $api_url = $api_url . '?access_token=' . $token;

            // 使用WordPress的wp_remote_post函数发送请求
            $response = wp_remote_post($api_url, array(
                'timeout' => $timeout/1000, // 转换为秒
                'body' => $curl_data,
                'headers' => array(
                    'Content-Type' => 'application/x-www-form-urlencoded'
                )
            ));

            if (is_wp_error($response)) {
                return array('error' => '1', 'msg' => '内容审核接口链接超时,请稍候再试');
            }

            $response_body = wp_remote_retrieve_body($response);
            $res = json_decode($response_body, true);

            if (!empty($res['error_msg'])) {
                return array('error' => '1', 'msg' => '内容审核失败:' . $res['error_msg']);
            }

            $msg = $msg_prefix . $res['conclusion'];

            if (isset($res['data'][0])) {
                foreach ($res['data'] as $data) {
                    if (!empty($data['msg'])) {
                        $msg .= ',' . $data['msg'];
                        if (isset($data['hits'][0])) {
                            $msg_words = '';
                            foreach ($data['hits'] as $hits) {
                                if (!empty($hits['words'][0])) {
                                    foreach ($hits['words'] as $words) {
                                        if ($words) {
                                            $msg_words .= '“' . $words . '”、';
                                        }
                                    }
                                }
                            }
                        }
                        $msg .= $msg_words ? '(' . rtrim($msg_words, '、') . ')' : '';
                    }
                }
            }

            // 汇总返回内容
            return self::_return($res['conclusionType'], $msg, $res);
        } else {
            // 图片审核仍然使用百度API
            $api_url = $api_url . '?access_token=' . $token;

            //开始请求
            $http     = new Yurun\Util\HttpRequest;
            $response = $http->timeout($timeout)->post($api_url, $curl_data);
            if (empty($response->success)) {
                return array('error' => '1', 'msg' => '百度内容审核接口链接超时,请稍候再试');
            }

            $res = $response->json(true);
            if (!empty($res['error_msg'])) {
                return array('error' => '1', 'msg' => '百度内容审核失败:' . $res['error_msg']);
            }

            $msg = $msg_prefix . $res['conclusion'];

            if (isset($res['data'][0])) {
                foreach ($res['data'] as $data) {
                    if (!empty($data['msg'])) {
                        $msg .= ',' . $data['msg'];
                        if (isset($data['hits'][0])) {
                            $msg_words = '';
                            foreach ($data['hits'] as $hits) {
                                if (!empty($hits['words'][0])) {
                                    foreach ($hits['words'] as $words) {
                                        if ($words) {
                                            $msg_words .= '“' . $words . '”、';
                                        }
                                    }
                                }
                            }
                        }
                        $msg .= $msg_words ? '(' . rtrim($msg_words, '、') . ')' : '';
                    }
                }
            }

            //汇总返回内容
            return self::_return($res['conclusionType'], $msg, $res);
        }
    }

    /**
     * @description: 输出
     * @param {*} $conclusionType 1.合规,2.不合规,3.疑似,4.审核失败
     * @param {*} $msg
     * @param {*} $data
     * @return {*}
     */
    public static function _return($conclusionType, $msg = "", $data = array())
    {
        if (!$conclusionType || 4 == $conclusionType) {
            return array('error' => '1', 'msg' => '暂无审核结果');
        }

        $conclusion_array = array(
            1 => '合规',
            2 => '不合规',
            3 => '疑似不合规',
        );

        $conclusion = isset($conclusion_array[$conclusionType]) ? $conclusion_array[$conclusionType] : '审核失败';

        return array(
            'conclusion'      => $conclusion,
            'conclusion_type' => $conclusionType,
            'msg'             => $msg,
            'data'            => $data,
        );
    }
}

10、去子比设置配置一下

20250817204638210-image-75

App Key:老糖牛逼

Secret Key:周总牛逼

 

文件下载地址:

如果你不想搭建也可以直接替换我的文件

 

 

 

请登录后发表评论