基于PaddleHub一键部署ernie_vilg文图生成等Web应用服务

简介: 基于PaddleHub一键部署ernie_vilg文图生成等Web应用服务

一、基于PaddleHub一键部署ernie_vilg文图生成等Web应用服务


image.png


1.基本介绍


之前写过基于PaddleHub的Web项目,然而晃晃悠悠2年过去了,许多hub升级了,接口改了,经常被问到报错了怎么办,这下和ernie_vilg部署一同作了。基本上只要会html基础、js基础、python基础就可以动手搭建了,属于有手就能干的入门项目。


2.基本功能


  • 抠图
  • 换背景证件照
  • 美颜
  • OCR
  • ernie_vilg文图生成


3.用到的几个包


  • paddlepaddle、paddlehub包
  • flask_bootstrap,提供bootstrap支持
  • blueprint,通过Blueprint来组织URL以及处理请求


二、hub serving服务启动


1.json文件编写


主要有以下部分:

  • 模型名称
  • 模型版本
  • 模型参数(常见的batch size等,但不一定都是)
  • 端口
  • 多进程

hub serving start --config config.json

{
  "modules_info": {
    "face_landmark_localization": {
      "init_args": {
        "version": "1.0.3"
      },
      "predict_args": {
        "batch_size": 1
      }
    },
    "deeplabv3p_xception65_humanseg": {
      "init_args": {
        "version": "1.1.2"
      },
      "predict_args": {
        "batch_size": 1
      }
    },
    "ernie_vilg": {
      "init_args": {
        "version": "1.0.0"
      },
      "predict_args": {
      }
    }
  },
  "port": 8866,
  "use_multiprocess": false,
  "workers": 2
}


2.启动服务


hub serving start --config config.json
(p2) PS C:\Users\livingbody\Desktop\ernie_vilg\flaskautocut> hub serving start --config config.json
[2022-08-31 09:22:38,624] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object
[2022-08-31 09:22:38,683] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object
[2022-08-31 09:22:38,894] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object
 * Serving Flask app 'paddlehub.serving.app_compat' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
INFO 2022-08-31 09:23:02,049 _internal.py:224]  * Running on all addresses (0.0.0.0)
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://127.0.0.1:8866
 * Running on http://192.168.8.8:8866 (Press CTRL+C to quit)

如上图,在8866端口启动成功


3.其他


目前,hub会自动判断并选择显卡,所以无需特别设置。


三、页面编写


1.提交页面


主要完成2项输入

  • 图片描述
  • 风格选择
<label class="col-sm-2 control-label">输入描述</label>
                <div class="col-sm-3">
                    <input type="text" class="form-control" name="describe" id="describe" placeholder="Text"
                           value="悠然见南山">
                </div>
                <label class="col-sm-2 control-label">背景色选择</label>
                <div class="col-sm-2">
                    <select class="form-control" name="selected_color">
                        <option value="油画">油画</option>
                        <option value="水彩">水彩</option>
                        <option value="粉笔画">粉笔画</option>
                        <option value="卡通">卡通</option>
                        <option value="儿童画">儿童画</option>
                        <option value="蜡笔画">蜡笔画</option>
                    </select>


2.展示页面


主要完成生成展示,具体是对生成的图片列表进行遍历展示。

{% if file_list %}
        {% for filename in file_list %}
        <a href='{{ filename }}' download="mypic.jpg">
            <img class="col-md-6 img-responsive  img-thumbnail" src="{{ filename }}"
                 alt="你的图片被外星人劫持了~~" width="50%"/>
        </a>
        {% endfor %}
        {% else %}
        <h1>file_list is none</h1>
 {% endif %}


四、后端调用


1.接收页面form提交


  • 收取页面提交的图片描述、风格选择参数
  • 调用ernie_vilg_gen函数,生成图片
  • 将文件列表绑定到展示页面
# 接收页面传递参数
@index_ernie_vilg.route('/ernie_vilg', methods=['POST', 'GET'])  # 添加路由
def ernie_vilg():
    if request.method == 'POST':
        try:
            ernie_vilg_text = request.form.get('describe')
            selected_style = request.form.get('selected_style')
            # print(ernie_vilg_text)           
            file_list = ernie_vilg_gen(ernie_vilg_text, selected_style)
            # file_list = [f'static/images/target/{fff}' for fff in file_list]
            for filename in file_list:
                print(filename)
            return render_template('ernie_vilg_ok.html', file_list=file_list)
        except Exception as e:
            print(e)
            # return render_template('404.html')
    return render_template('ernie_vilg.html')


2.serving调用


调用serving的api,生成图片

# 生成
def ernie_vilg_gen(ernie_vilg_text, selected_style):
    # 发送HTTP请求
    data = {'text_prompts': ernie_vilg_text, 'style': "油画"}
    headers = {"Content-type": "application/json"}
    url = "http://127.0.0.1:8866/predict/ernie_vilg"
    r = requests.post(url=url, headers=headers, data=json.dumps(data))
    # print(r.json())
    # 获取返回结果
    file_list = []
    for i, result in enumerate(r.json()["results"]):
        image = Image.open(BytesIO(base64.b64decode(result)))
        image.save('static/images/target/result_{}.png'.format(i))
        file_list.append('static/images/target/result_{}.png'.format(i))
        print('result_{}.png'.format(i))
    return file_list


五、启动项目


  • flask bootstrap支持
pip install flask_bootstrap
  • flask blueprint
pip install flask-blueprint
  • run下
set CUDA_VISIBLE_DEVICES=0
hub serving start --config config.json
python app.py
  • 代码 代码上传至项目根目录了【ernievilg_web.zip】,可以自行下载,有问题可以浏览一起解决


目录
相关文章
|
24天前
|
网络协议 Java Nacos
nacos常见问题之在web界面 上下线服务时报错 400如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
29 0
|
24天前
|
监控 Serverless 测试技术
Serverless 应用引擎常见问题之做的web服务计费如何解决
Serverless 应用引擎(Serverless Application Engine, SAE)是一种完全托管的应用平台,它允许开发者无需管理服务器即可构建和部署应用。以下是Serverless 应用引擎使用过程中的一些常见问题及其答案的汇总:
169 3
|
30天前
|
JSON API 数据库
解释如何在 Python 中实现 Web 服务(RESTful API)。
解释如何在 Python 中实现 Web 服务(RESTful API)。
23 0
|
1月前
|
存储 资源调度 应用服务中间件
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
24 0
|
1月前
|
负载均衡 Java 中间件
使用Go语言构建高性能Web服务
Go语言作为一种快速、高效的编程语言,其在构建高性能Web服务方面具有独特优势。本文将探讨如何利用Go语言开发和优化Web服务,以实现更高的性能和可伸缩性。
|
2月前
|
Arthas 监控 NoSQL
web服务性能监控方案
web服务性能监控方案
|
2天前
|
Web App开发 Java 应用服务中间件
【Java Web】在 IDEA 中部署 Tomcat
【Java Web】在 IDEA 中部署 Tomcat
8 0
|
15天前
|
数据采集 Java API
python并发编程: Python使用线程池在Web服务中实现加速
python并发编程: Python使用线程池在Web服务中实现加速
17 3
python并发编程: Python使用线程池在Web服务中实现加速
|
28天前
javaWeb服务详解(含源代码,测试通过,注释) ——web.xml
javaWeb服务详解(含源代码,测试通过,注释) ——web.xml
7 0
|
30天前
|
XML JSON API
通过Flask框架创建灵活的、可扩展的Web Restful API服务
通过Flask框架创建灵活的、可扩展的Web Restful API服务