Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy a model as a service using flask

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to deploy the model as a service using flask", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use flask to deploy the model as a service" article.

1. Load a saved model

For convenience, we will use a simple word segmentation model here, the related code is as follows: model.py

Import jiebaclass JiebaModel: def load_model (self): self.jieba_model = jieba.lcut def generate_result (self, text): return self.jieba_model (text, cut_all=False)

Description: load the saved model in the load_model method, whether it is sklearn, tensorflow or pytorch. The logic to get the output after processing the input is defined in the generate_result method and the result is returned.

two。 Use flask to start service

The code is as follows: test_flask.py

#-*-coding:utf-8-*-from flask import Flask, request, Response, abortfrom flask_cors import CORS# from ast import literal_evalimport timeimport sysimport jsonimport tracebackfrom model import JiebaModelapp = Flask (_ _ name__) CORS (app) # allows all domains on all routes to use CORS@app.route ("/", methods= ['POST',' GET']) def inedx (): return 'participle program is running' @ app.route ("/ split_words", methods= ['POST' ") 'GET']) def get_result (): if request.method =' POST': text = request.data.decode ("utf-8") else: text = request.args ['text'] try: start = time.time () print ("user input", text) res = jiebaModel.generate_result (text) end = time.time () print (' time spent on participle:' End-start) print ('segmentation result:', res) result = {'code':'200','msg':' response successful', 'data':res} except Exception as e: print (e) result_error = {' errcode':-1} result = json.dumps (result_error, indent=4, ensure_ascii=False) # this is used to capture more detailed exception information exc_type Exc_value, exc_traceback = sys.exc_info () lines = traceback.format_exception (exc_type, exc_value, exc_traceback) # early exit request abort (Response ("Failed!" + '.join (' + line for line in lines)) return Response (str (result)) Mimetype='application/json') if _ _ name__ = "_ _ main__": jiebaModel = JiebaModel () jiebaModel.load_model () app.run (host='0.0.0.0', port=1314, threaded=False)

Description: we define a get_result () function, and the corresponding request is ip:port/split_words. First of all, we get the data according to whether the request is a get request or an post request, then use the model to get the output result based on the input data, and return the response to the request. If an exception is encountered, it is handled accordingly and returned. In _ _ main__, we introduced the JiebaModel class of model.py, then loaded the model and called it in get_result ().

3. Send the request and get the result

The code is as follows: test_request.py

Import requestsdef get_split_word_result (text): res = requests.post ('http://{}:{}/split_words'.format(' native ip', 1314), data=str (text) .encode (' utf-8') print (res.text) get_split_word_result ("I love Beijing Tiananmen")

Note: send a post request through requests, the request data is encoded into utf-8 format, and finally get the response, and use .text to get the result.

The above is the content of this article on "how to deploy the model as a service using flask". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report