Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Flask Server (#4433)
Browse files Browse the repository at this point in the history
* Flask Demo Added

Added folder with name flask_api_dome and added code to implement parlai model with flask framework and readme file in the same folder. Also added URL to flask API folder on main README.md file.

* Update README.md

* Revert "Update README.md"

This reverts commit 1a6122d.

* Revert "Flask Demo Added"

This reverts commit 778e217.

* one the necessary changes as per suggestions

* Update requirements.txt

Added flask on  requirements.txt file

* Update index.md

* Revert "Update index.md"

This reverts commit 29eff3b.

* Revert "Update requirements.txt"

This reverts commit 4c7ac44.

* Revert "one the necessary changes as per suggestions"

This reverts commit a81ff9c.

* Deleted flask_demo.py

Code has been added to doc file, so removing it for now so solve build_website error

* Flask_demo added

* Remove Model as global model

* update tutorial_flask

* Updated tutorial_flask.md

* updated flask_demo.py

* Lint.

* Update parlai/scripts/flask_demo.py

lint again

* Update parlai/scripts/flask_demo.py

lol keep linting

* Few Changes to Improve script

* Wrap in a proper script.

* Whoops

* Lint.

Co-authored-by: Khushal Jethava <khushaljethwa14@gmail.com>
Co-authored-by: Khushal Jethava <khushaljethava14@outlook.com>
  • Loading branch information
3 people committed Mar 21, 2022
1 parent 74e12d1 commit c5210a6
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions parlai/scripts/flask.py
@@ -0,0 +1,53 @@
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

"""
Example Flask server which hosts a model.
## Examples
**Serving the model**
```shell
parlai flask -m repeat_query
parlai flask -mf zoo:blender/blender_90M/model
```
**Hitting the API***
```shell
curl -k http://localhost:5000/response -H "Content-Type: application/json" -d '{"text": "foobar"}'
```
"""

from parlai.core.agents import create_agent
from parlai.core.params import ParlaiParser
from parlai.core.script import ParlaiScript, register_script


@register_script('flask', hidden=True)
class Flask(ParlaiScript):
@classmethod
def setup_args(cls):
parser = ParlaiParser(True, True)
return parser

def chatbot_response(self):
from flask import request

data = request.json
self.agent.observe({'text': data["text"], 'episode_done': False})
response = self.agent.act()
return {'response': response['text']}

def run(self):
from flask import Flask

self.agent = create_agent(self.opt)
app = Flask("parlai_flask")
app.route("/response", methods=("GET", "POST"))(self.chatbot_response)
app.run()


if __name__ == "__main__":
Flask.main()

0 comments on commit c5210a6

Please sign in to comment.