使用阿里云ECS创建聊天机器人

简介:


初次发布于2018年1月


前期考虑使用了图灵机器人,有两种实现方式绑定公众号和加入群聊。图灵机器人可以直接绑定公众号,使用过程还是不错的,不用明确@,可以直接进行对话,上下文逻辑比微软小冰要好,有1000条/天的限制。第二种方案则是使用第三方插件,绑定微信号,用机器人运行微信号,缺点就是需要一直运行微信不能退出,逻辑不明显。好吧,逻辑都不是很明显。

接下来考虑了Sam Gu调用itchat的方式[1]。Sam 使用谷歌云平台集成ItChat微信聊天机器人并集成深度人工智能应用,首先安装Jupyter Notebook。

登陆服务器,可以通过控制台,或者putty, mobaxterm等工具,前者简洁后者提供文件操作界面。

mkdir anaconda #创建安装目录
cd anaconda #将进入安装目录
wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh #下载安装包
bash Anaconda3-5.0.1-Linux-x86_64.sh #安装另外卸载用rm -rf anaconda3
jupyter notebook --generate-config --allow-root 
ipython 
from notebook.auth import passwd

passwd() #设置密码会生成hashed password,复制

vi ~/.jupyter/jupyter_notebook_config.py
 

复制粘贴以下几行,然后运行。具体操作可以参翼起小飞在社区的博文,即[2]。

 

c.NotebookApp.ip='*'
c.NotebookApp.password = u'把上面的文本粘贴到这里'
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888
nohup jupyter notebook --allow-root

 

然后运行Jupyter Notebook,并在浏览器登录。Sam是在谷歌云台(第一年免费,赠送300美元,当然阿里云也有学生优惠)上创建的,基本原理是一致的,他的视频里讲的也是非常清楚,这里把这些内容搬运过来。登陆Jupyter Notebook后,创建一个文件夹存放相关文件,然后创建Python3文件,在其命令行里复制并运行以下命令,第二条运行两次。

 

!git clone https://github.com/telescopeuser/workshop_blog.git
%load workshop_blog/setup_cloud.py

 

运行lesson 1就可以开始了。当然这些命令也可以在服务器直接调用,Jupyter Notebook在这里更多的是交互界面的作用。接下来的图形识别要调用机器学习,Sam使用了Google Cloud Platform's Machine Learning APIs,直接调用就可以,复制粘贴API,然后再运行lesson 2。Lesson3 主要讲了语音的识别,翻译。Lesson4就高深一点,涉及到情感语义分析,接下来的lesson5涉及到从视频中提取信息。按照参考文献[1]的知识进行操作即可。但是,这样调用谷歌API,和之前挂用图灵机器人,最大的区别就是多了几行代码,Well,很多行代码,和我预想的还是有差距的。

然后在warmheartli的项目里面发现了些有趣的内容,不过我要准备去挤火车了……

 

更新于2018年9月17日星期一:收集聊天语料

莫名其妙的更新被删除掉,好在以前的都还留着,从新再发语料的收集不再考虑使用字幕,从微博爬取数据,作为日后的语料和机器学习用,爬虫的方法参考nghuyong,nghuyong的方法在验证登录的时候存在无法识别。因此滑块验证的方法参考LiuXingMing/WeiboSliderCode;Python3WebSpider/CrackWeiboSlide;bone_ace/article/details/71056741,当然他们的方法也并不是有效,尝试了很多方法后,最简单粗暴地是把验证图形的矩阵直接放在cookies.py里面。

 

#!/usr/bin/env python

# encoding: utf-8

import datetime

import json

import base64

from time import sleep

import os

import time

import random

import io

from PIL import Image

from math import sqrt

 

import pymongo

from selenium import webdriver

from selenium.webdriver import ActionChains

from selenium.common.exceptions import TimeoutException

from selenium.webdriver import ActionChains

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.remote.command import Command

 

WeiBoAccounts = [

{'username': '登陆账号', 'password': ‘密码’},

]

 

cookies = []

client = pymongo.MongoClient("localhost", 27017)

db = client["Sina"]

userAccount = db["userAccount"]

PIXELS = []

 

 

def getExactly(im):

    """ Precise cutting """

    imin = -1

    imax = -1

    jmin = -1

    jmax = -1

    row = im.size[0]

    col = im.size[1]

    for i in range(row):

        for j in range(col):

            if im.load()[i, j] != 255:

                imax = i

                break

        if imax == -1:

            imin = i

 

    for j in range(col):

        for i in range(row):

            if im.load()[i, j] != 255:

                jmax = j

                break

        if jmax == -1:

            jmin = j

    return (imin + 1, jmin + 1, imax + 1, jmax + 1)

 

 

def getType(browser):

    """ Identifying the graphic path """

    ttype = ''

    time.sleep(3.5)

    im0 = Image.open(io.BytesIO(browser.get_screenshot_as_png()))

    box = browser.find_element_by_id('patternCaptchaHolder')

    im = im0.crop((int(box.location['x']) + 10, int(box.location['y']) + 100, int(box.location['x']) + box.size['width'] - 10, int(box.location['y']) + box.size['height'] - 10)).convert('L')

    newBox = getExactly(im)

    im = im.crop(newBox)

    width = im.size[0]

    height = im.size[1]

    for png in ims.keys():

        isGoingOn = True

        for i in range(width):

            for j in range(height):

                if ((im.load()[i, j] >= 245 and ims[png][i][j] < 245) or (im.load()[i, j] < 245 and ims[png][i][j] >= 245)) and abs(ims[png][i][j] - im.load()[i, j]) > 10: # With 245 as the threshold, about 245 as the blank, less than 245 as the line; the difference between two pixels is about 10 to remove the error on the 245 boundary

                    isGoingOn = False

                    break

            if isGoingOn is False:

                ttype = ''

                break

            else:

                ttype = png

        else:

            break

    px0_x = box.location['x'] + 40 + newBox[0]

    px1_y = box.location['y'] + 130 + newBox[1]

    PIXELS.append((px0_x, px1_y))

    PIXELS.append((px0_x + 100, px1_y))

    PIXELS.append((px0_x, px1_y + 100))

    PIXELS.append((px0_x + 100, px1_y + 100))

    return ttype

 

 

def move(browser, coordinate, coordinate0):

    """ Move from coordinate0 to coordinate """

    time.sleep(0.05)

    length = sqrt((coordinate[0] - coordinate0[0]) ** 2 + (coordinate[1] - coordinate0[1]) ** 2)  # Two point line distance

    if length < 4:  # If the distance between two points is less than 4PX, go straight ahead.

        ActionChains(browser).move_by_offset(coordinate[0] - coordinate0[0], coordinate[1] - coordinate0[1]).perform()

        return

    else:  # Recursion, sliding to the end

        step = random.randint(3, 5)

        x = int(step * (coordinate[0] - coordinate0[0]) / length)  # In proportion

        y = int(step * (coordinate[1] - coordinate0[1]) / length)

        ActionChains(browser).move_by_offset(x, y).perform()

        move(browser, coordinate, (coordinate0[0] + x, coordinate0[1] + y))

 

 

def draw(browser, ttype):

    """ Slide """

    if len(ttype) == 4:

        px0 = PIXELS[int(ttype[0]) - 1]

        login = browser.find_element_by_id('loginAction')

        ActionChains(browser).move_to_element(login).move_by_offset(px0[0] - login.location['x'] - int(login.size['width'] / 2), px0[1] - login.location['y'] - int(login.size['height'] / 2)).perform()

        browser.execute(Command.MOUSE_DOWN, {})

 

        px1 = PIXELS[int(ttype[1]) - 1]

        move(browser, (px1[0], px1[1]), px0)

 

        px2 = PIXELS[int(ttype[2]) - 1]

        move(browser, (px2[0], px2[1]), px1)

 

        px3 = PIXELS[int(ttype[3]) - 1]

        move(browser, (px3[0], px3[1]), px2)

        browser.execute(Command.MOUSE_UP, {})

    else:

        print('Sorry! Failed! Maybe you need to update the code.')

                                    

def get_cookie_from_weibo(username, password):

    browser = webdriver.Chrome()

    browser.set_window_size(1050, 840)

    browser.get('https://weibo.cn/')

    time.sleep(1)

    assert "微博" in browser.title

    login_link = browser.find_element_by_link_text('登录')

    ActionChains(browser).move_to_element(login_link).click().perform()

    login_name = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "loginName")))

    login_password = browser.find_element_by_id("loginPassword")

    login_name.send_keys(username)

    login_password.send_keys(password)

    login_button = browser.find_element_by_id("loginAction")

    login_button.click()

    try:

        img = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'patt-shadow')))

    except TimeoutException:

        print('No verification codes')

        self.open()

    # Here you stay for 10 seconds to see if the Chrome is successfully logged in.

    ttype = getType(browser)  # Identifying the graphic path

    print('Result: %s!' % ttype)

    draw(browser, ttype)  # Slide

    time.sleep(10)

    cookie = browser.get_cookies()

    browser.close()

    return cookie

 

 

def init_cookies():

    for cookie in userAccount.find():

        cookies.append(cookie['cookie'])

 

 

if __name__ == "__main__":

    try:

        userAccount.drop()

    except Exception as e:

        pass

    for account in WeiBoAccounts:

        cookie = get_cookie_from_weibo(account["username"], account["password"])

        userAccount.insert_one({"_id": account["username"], "cookie": cookie})

 

TOBECONTINUED

 

 

 

参考文献:

[1]         Telescopeuser, workshop_blog, (n.d.). https://github.com/telescopeuser/workshop_blog.

[2]         翼起小飞, 如何在阿里ECS云端运行Jupyter Notebook进行机器/深度学习?-博客-云栖社区-阿里云, (n.d.). https://yq.aliyun.com/articles/98527 (accessed February 5, 2018).

 

相关实践学习
基于ECS搭建云上博客
本实验教您如何在ECS云服务器(Alibaba Cloud Linux 3)的环境下,通过部署LAMP环境并安装WordPress快速搭建个人博客。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
目录
相关文章
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
1147 10
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
1110 8
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。
|
存储 监控 安全
阿里云渠道商:云服务器价格有什么变动?
阿里云带宽与存储费用呈基础资源降价、增值服务差异化趋势。企业应结合业务特点,通过阶梯计价、智能分层、弹性带宽等策略优化成本,借助云监控与预算预警机制,实现高效、可控的云资源管理。
|
弹性计算 运维 安全
区别及选择指南:阿里云轻量应用服务器与ECS云服务器有什么区别?
阿里云轻量应用服务器适合个人开发者、学生搭建博客、测试环境,易用且性价比高;ECS功能更强大,适合企业级应用如大数据、高流量网站。根据需求选择:轻量入门首选,ECS专业之选。
958 2
|
弹性计算 ice
阿里云4核8G云服务器配置价格:热门ECS实例及CPU处理器型号说明
阿里云2025年4核8G服务器配置价格汇总,涵盖经济型e实例、计算型c9i等热门ECS实例,CPU含Intel Xeon及AMD EPYC系列,月费159元起,年付低至1578元,按小时计费0.45元起,实际购买享折扣优惠。
4485 1
|
存储 弹性计算 安全
阿里云渠道商:新手如何选择阿里云ECS实例?
阿里云ECS凭借弹性扩展、稳定可靠与安全防护,助力企业高效上云。本文系统解析实例规格选择关键因素:业务场景匹配、性能评估、成本优化、地域部署与扩展规划,结合计费模式与实际需求,提供科学选型建议,助您精准匹配资源,提升云上效能。(238字)
|
存储 弹性计算 安全
阿里云渠道商:新手怎么选阿里云ECS实例规格?
本文深入解析阿里云ECS的弹性、稳定与安全优势,详解实例规格选择的关键因素,涵盖应用场景、性能需求、成本预算、地理位置与扩展性,助力用户精准选型,优化业务部署与成本效益。

热门文章

最新文章