5分钟构建无服务器敏感词过滤后端系统(基于FunctionGraph)

利用函数工作流构建无服务器架构的敏感词过滤系统,无需关注服务器运维,仅需编写核心业务逻辑,实现文本敏感词检测及过滤。系统弹性伸缩、按需计费,大幅降低开发与运维成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘要开发者通过函数工作流,无需配置和管理服务器,以无服务器的方式构建应用,便能开发出一个弹性高可用的后端系统。托管函数具备以毫秒级弹性伸缩、免运维、高可靠的方式运行,极大地提高了开发和运维效率,减小了运作成本。

函数工作流(FunctionGraph,FGS)是一项基于事件驱动的函数托管计算服务,托管函数具备以毫秒级弹性伸缩、免运维、高可靠的方式运行。通过函数工作流,开发者无需配置和管理服务器,只需关注业务逻辑,编写函数代码,以无服务器的方式构建应用,便能开发出一个弹性高可用的后端系统,并按实际运行消耗的资源计费。极大地提高了开发和运维效率,减小了运作成本。

相比于传统的架构,函数工作流构建的无服务器架构具有如下优点:

1. 无需关注任何服务器,只需关注核心业务逻辑,提高开发和运维效率;

2. 函数运行随业务量弹性伸缩,按需付费,执行才计费,对于访问量波峰波谷非常明显的场景可以减少大量成本;

3. 通过简单的配置即可连通函数工作流和其它各云服务,甚至云服务和云服务;

为了进一步让大家感受函数工作流的优势,我们将介绍如何通过函数工作流快速构建一个无服务器的敏感词过滤系统,本文我们主要关注后端系统,前端的表现形式很多,大家可以自行构建。如下图,该系统会识别用户上传的文本内容是否包含敏感信息(如色 情、政 治等),并对这些词语进行过滤。

试想,如果我们通过传统的模式开发此应用,需要如何开发?

即使是基于现在的云平台,我们也仍需要购买云服务器,关注其规格、镜像、网络等各指标的选型和运维,然后在开发过程中可能还需要考虑与其他云服务的集成使用问题,使代码中耦合大量非业务代码,并且服务器等资源也并非是按需的,特别是对于访问量波峰波谷非常明显的场景,会造成大量多余的费用。

现在我们可以通过函数工作流服务来快速构建这个系统,并且完全无需关注服务器,且弹性伸缩运行、按需计费,如图:

创建函数,在函数中调用华为云内容检测服务提供的文本检测接口,实现文本的敏感词检测,并为该函数配置一个APIG触发器,对外提供敏感词过滤的API,从而构建出一个完整的敏感词过滤的无服务器系统。客户端调用API,他会自动触发函数执行,而开发者编写的函数只需实现接收到文本之后如何处理文本的逻辑(调用内容检测服务服务)即可,最后将结果返回给客户端。至此,我们就构建了一个完整的无服务器敏感词过滤系统。

现在,我们将介绍如何端到端地将此无服务器系统构建出来。

1. 准备工作

进入华为云内容检测服务,申请开通文本内容检测,成功申请后边可以调用内容检测服务提供的文本检测接口了。

2. 创建函数

进入函数工作流服务页面,创建函数,实现文本检测的接口调用和敏感词过滤,代码如下(Python):

# -*- coding:utf-8 -*-

import json

import base64

import urllib

import urllib2

import ssl

import sys

 

reload(sys)

sys.setdefaultencoding('utf-8')

 

def do_filter(msg,str_list):

    result = ''

    try:

        if len(str_list) <=0:

            return msg

        for str in str_list:

            str_tmp = msg.replace(str,'')

            msg = str_tmp

        result = msg

    except:

        print("_do_filter catch an exception!")

    return result

 

def filter(context, msg):

    result = ''

    try:

        ssl._create_default_https_context = ssl._create_unverified_context

 

        token = context.getToken();

        headers = {'Content-Type':'application/json;charset=utf8','X-Auth-Token':token}

 

        url = "https://ais.cn-north-1.myhwclouds.com/v1.0/moderation/text"

 

        values = {}

        values['categories'] = ['porn','ad','politics','abuse','contraband']

        #msg = base64.b64encode(msg)

        item = {'type':'content','text':msg}

        values['items'] = [item]

 

        data = json.dumps(values)

        print("data: %s"%data)

 

        request = urllib2.Request(url,data,headers)

        rsp = urllib2.urlopen(request)

        http_rsp = rsp.read()

        print("http response: %s" %http_rsp)

 

        json_rsp = json.loads(http_rsp)

        result = json_rsp['result']

 

        suggestion = result['suggestion']

 

        if suggestion == 'pass':

            print("input msg have passed the checking!")

            result = msg

        else:

            detail = result['detail']

 

            if detail.has_key('porn'):

                list_porn = detail['porn']

                msg = do_filter(msg,list_porn)

            if detail.has_key('ad'):

                list_ad = detail['ad']

                msg = do_filter(msg,list_ad)

            if detail.has_key('politics'):

                list_politics = detail['politics']

                msg = do_filter(msg,list_politics)

            if detail.has_key('abuse'):

                list_abuse = detail['abuse']

                msg = do_filter(msg,list_abuse)

            if detail.has_key('contraband'):

                list_contraband = detail['contraband']

                msg = do_filter(msg,list_contraband)

            result = msg

    except Exception, e:

        print e

        print("filter catch an exception!")

    return result

 

def handler (event, context):

    print("message filter begin!")

    result = ""

    response = {}

    http_method = event.get('httpMethod')

 

    if http_method == 'OPTIONS':

        response = {

            'statusCode': 200,

            'isBase64Encoded': True,

            'headers': {

                "Content-Type": "application/json; charset=utf-8",

                "Access-Control-Allow-Origin": "*",

                "Access-Control-Allow-Headers": "Content-Type,Accept",

                "Access-Control-Allow-Methods": "GET,POST,PUT,DELETE"

            },

            'body': base64.b64encode('{"result":'+ '"' + result +'"}'),

        }

        return response

    body = event.get('body')

    body_decode = base64.b64decode(body)

    json_object = json.loads(body_decode)

    msg = json_object['msg']

 

    print('msg : %s'%msg)

 

    try:

        result = filter(context, msg)

        response = {

            'statusCode': 200,

            'isBase64Encoded': True,

            'headers': {

                "Content-Type": "application/json; charset=utf-8",

                "Access-Control-Allow-Origin": "*",

                "Access-Control-Allow-Headers": "Content-Type,Accept",

                "Access-Control-Allow-Methods": "GET,POST,PUT,DELETE"

            },

            'body': base64.b64encode('{"result":'+ '"' + result +'"}'),

        }

    except:

        print("function catch an exception!")

 

    return response

函数创建完成之后,为其配置具有IAM访问权限的委托,因为本函数代码中获取用户的ak、sk需要拥有访问IAM的权限。

3. 创建APIG触发器

为函数配置一个APIG触发器,这样便得到一个调用该函数的HTTP(S) API,供外部调用。

4. 测试

使用postman等工具向上一步中创建的APIG触发器的接口发送post请求,body体为:{“msg”: “过滤检测的文本”},查看返回信息。

比如发送 {"msg": "just fuck ..."},返回体为 {"result": "just  ..."}

自此,我们就完整地实现了一个无服务器的敏感词过滤后端系统。

 

华为云内容检测:http://t.cn/ReRP5UI

华为云函数工作流:http://t.cn/ReRvs3R

/ -- 在线客服聊天 --> 认证失败,重新设置聊天系统的用户密码 // 聊天窗口右侧默认展示最近订单,如果想要展示商品、订单、店铺则需要在当前页面中设置隐藏域,name必须为 chat_goods_id, // chat_order_id, chat_supp_id /* ------------------------------------------------------ */ function action_authfail () { $user_id = $_SESSION['user_id']; $sql = "select user_name, password, email from " . $GLOBALS['ecs']->table('users') . " where user_id = '$user_id'"; $row = $db->getRow($sql); $success = create_of_user($user_id, $row['password'], $row['user_name'], $row['email'], 10, - 1); if($success) { $result = array( 'error' => 1,'message' => '可能由于网络原因,发生错误!请点击 <a href="chat.php?act=chat"><strong>重试</strong></a> ,重新连接...','content' => '' ); } else { $result = array( 'error' => 1,'message' => '由于网络原因,发生错误!请点击 <a href="chat.php?act=chat"><strong>重试</strong></a> ,重新连接...','content' => '' ); } $result = json_encode($result); exit($result); } /** * 用户离线 */ function action_off_line() { // 用户超过5分钟未发言则视为自动离线,更新客服状态 } function is_telephone ($phone) { $chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/"; if(preg_match($chars, $phone)) { return true; } } /** * 获取db对象 * * @return unknown */ function get_database () { return $GLOBALS['db']; } /** * 获取smarty对象 * * @return unknown */ function get_smarty () { return $GLOBALS[smarty]; } /** * 获取ecs对象 * * @return unknown */ function get_ecs () { return $GLOBALS['ecs']; } /* * 获取商品所对应店铺的店铺基本信息 * @param int $suppid 店铺id * @param int $suppinfo 入驻商的信息 */ function get_dianpu_baseinfo ($suppid = 0) { if(intval($suppid) <= 0) { return; } $sql_supplier = "SELECT s.supplier_id,s.supplier_name,s.add_time,sr.rank_name FROM " . $GLOBALS['ecs']->table("supplier") . " as s left join " . $GLOBALS['ecs']->table("supplier_rank") . " as sr ON s.rank_id=sr.rank_id WHERE s.supplier_id=" . $suppid . " AND s.status=1"; $supp_info = $GLOBALS['db']->getRow($sql_supplier); $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('supplier_shop_config') . " WHERE supplier_id = " . $suppid; $shopinfo = $GLOBALS['db']->getAll($sql); $config = array(); foreach($shopinfo as $value) { $config[$value['code']] = $value['value']; } $shop_info = array(); $shop_info['ghs_css_path'] = 'themes/' . $config['template'] . '/images/ghs/css/ghs_style.css'; // 入驻商所选模板样式路径 $shoplogo = empty($config['shop_logo']) ? 'themes/' . $config['template'] . '/images/dianpu.jpg' : $config['shop_logo']; $shop_info['shoplogo'] = $shoplogo; // 商家logo $shop_info['shopname'] = htmlspecialchars($config['shop_name']); // 店铺名称 $shop_info['suppid'] = $suppid; // 商家名称 $shop_info['suppliername'] = htmlspecialchars($supp_info['supplier_name']); // 商家名称 $shop_info['userrank'] = htmlspecialchars($supp_info['rank_name']); // 商家等级 $shop_info['region'] = get_province_city($config['shop_province'], $config['shop_city']); $shop_info['address'] = $config['shop_address']; $shop_info['serviceqq'] = $config['qq']; $shop_info['serviceww'] = $config['ww']; $shop_info['serviceemail'] = $config['service_email']; $shop_info['servicephone'] = $config['service_phone']; $shop_info['createtime'] = gmdate('Y-m-d', $config['add_time']); // 商家创建时间 $suppid = (intval($suppid) > 0) ? intval($suppid) : intval($_GET['suppId']); return $shop_info; } 构建多商户人工客服系统
最新发布
07-16
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值