From 778e217f926360d8e4d261e295e1b714ce1354da Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Tue, 25 Jan 2022 23:30:33 +0530 Subject: [PATCH 01/23] 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. --- README.md | 1 + flask_api_demo/README.md | 66 ++++++++++++++++++++++++++++++++++++++++ flask_api_demo/app.py | 27 ++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 flask_api_demo/README.md create mode 100644 flask_api_demo/app.py diff --git a/README.md b/README.md index b008ef6ca23..def7c902eaa 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ check out [NEWS.md](https://github.com/facebookresearch/ParlAI/blob/main/NEWS.md ## Interactive Tutorial For those who want to start with ParlAI now, you can try our [Colab Tutorial](https://colab.research.google.com/drive/1bRMvN0lGXaTF5fuTidgvlAl-Lb41F7AD#scrollTo=KtVz5dCUmFkN). +For those who want to try ParlAi with flask API, you can try the [flask demo](https://github.com/facebookresearch/ParlAI/tree/main/flask_api_demo). ## Installing ParlAI diff --git a/flask_api_demo/README.md b/flask_api_demo/README.md new file mode 100644 index 00000000000..c3d9cdad987 --- /dev/null +++ b/flask_api_demo/README.md @@ -0,0 +1,66 @@ +## Flask API demo + +### Parl.ai model implement on flask framework + +With this script, you can implement your fine-tune or pretrained parlai model very easily. + + +## Source Code + +```python + +#import required libraries +from flask import Flask, render_template, request +from parlai.core.agents import create_agent_from_model_file + + +# Flask constructor takes the name of +#initialize flask app +app = Flask(__name__) + +#import model from the model file can be pretrained or fine tuned +blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") + + +# The route() function of the Flask class is a decorator, +# which tells the application which URL should call +# the associated function. +@app.route("/response", methods=["GET","POST"]) # API URL +def chatbot_response(): # function name + data = request.json # Take input as json format + blender_agent.observe({'text': data["UserText"], 'episode_done': False}) #Give User inputted text to model + response = blender_agent.act() #take response from the model + return {"response" : response['text']} #return model response + + +# main driver function +if __name__ == "__main__": + app.run() + +``` + +### How to + +Just change the model path and model name inside the create_agent_from_model_file function. + +```python +blender_agent = create_agent_from_model_file("Model Name") +``` + +Now Run the script and start the flask webserver + +Pass the message format defined below: + +```json +{ + "UserText" : "Your Inputted Text" +} +``` + +It will generate the below response + +```json +{ + "response": "Model Response" +} +``` \ No newline at end of file diff --git a/flask_api_demo/app.py b/flask_api_demo/app.py new file mode 100644 index 00000000000..904b629d52b --- /dev/null +++ b/flask_api_demo/app.py @@ -0,0 +1,27 @@ +#import required libraries +from flask import Flask, render_template, request +from parlai.core.agents import create_agent_from_model_file + + +# Flask constructor takes the name of +#initialize flask app +app = Flask(__name__) + +#import model from the model file can be pretrained or fine tuned +blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") + + +# The route() function of the Flask class is a decorator, +# which tells the application which URL should call +# the associated function. +@app.route("/response", methods=["GET","POST"]) # API URL +def chatbot_response(): # function name + data = request.json # Take input as json format + blender_agent.observe({'text': data["UserText"], 'episode_done': False}) #Give User inputted text to model + response = blender_agent.act() #take response from the model + return {"response" : response['text']} #return model response + + +# main driver function +if __name__ == "__main__": + app.run() From 1a6122db7c86777b24eb3f377d0c15388d582e14 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Tue, 25 Jan 2022 23:34:28 +0530 Subject: [PATCH 02/23] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index def7c902eaa..73bef0cca57 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ check out [NEWS.md](https://github.com/facebookresearch/ParlAI/blob/main/NEWS.md ## Interactive Tutorial For those who want to start with ParlAI now, you can try our [Colab Tutorial](https://colab.research.google.com/drive/1bRMvN0lGXaTF5fuTidgvlAl-Lb41F7AD#scrollTo=KtVz5dCUmFkN). + For those who want to try ParlAi with flask API, you can try the [flask demo](https://github.com/facebookresearch/ParlAI/tree/main/flask_api_demo). ## Installing ParlAI From e2dbf7d282dd1c3c33146cbc7e3d7e7947ef1857 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Wed, 26 Jan 2022 15:22:47 +0530 Subject: [PATCH 03/23] Revert "Update README.md" This reverts commit 1a6122db7c86777b24eb3f377d0c15388d582e14. --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 73bef0cca57..def7c902eaa 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ check out [NEWS.md](https://github.com/facebookresearch/ParlAI/blob/main/NEWS.md ## Interactive Tutorial For those who want to start with ParlAI now, you can try our [Colab Tutorial](https://colab.research.google.com/drive/1bRMvN0lGXaTF5fuTidgvlAl-Lb41F7AD#scrollTo=KtVz5dCUmFkN). - For those who want to try ParlAi with flask API, you can try the [flask demo](https://github.com/facebookresearch/ParlAI/tree/main/flask_api_demo). ## Installing ParlAI From 95ae29a6a12ce802655bc0301c1b3ca302aa4912 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Wed, 26 Jan 2022 15:23:08 +0530 Subject: [PATCH 04/23] Revert "Flask Demo Added" This reverts commit 778e217f926360d8e4d261e295e1b714ce1354da. --- README.md | 1 - flask_api_demo/README.md | 66 ---------------------------------------- flask_api_demo/app.py | 27 ---------------- 3 files changed, 94 deletions(-) delete mode 100644 flask_api_demo/README.md delete mode 100644 flask_api_demo/app.py diff --git a/README.md b/README.md index def7c902eaa..b008ef6ca23 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ check out [NEWS.md](https://github.com/facebookresearch/ParlAI/blob/main/NEWS.md ## Interactive Tutorial For those who want to start with ParlAI now, you can try our [Colab Tutorial](https://colab.research.google.com/drive/1bRMvN0lGXaTF5fuTidgvlAl-Lb41F7AD#scrollTo=KtVz5dCUmFkN). -For those who want to try ParlAi with flask API, you can try the [flask demo](https://github.com/facebookresearch/ParlAI/tree/main/flask_api_demo). ## Installing ParlAI diff --git a/flask_api_demo/README.md b/flask_api_demo/README.md deleted file mode 100644 index c3d9cdad987..00000000000 --- a/flask_api_demo/README.md +++ /dev/null @@ -1,66 +0,0 @@ -## Flask API demo - -### Parl.ai model implement on flask framework - -With this script, you can implement your fine-tune or pretrained parlai model very easily. - - -## Source Code - -```python - -#import required libraries -from flask import Flask, render_template, request -from parlai.core.agents import create_agent_from_model_file - - -# Flask constructor takes the name of -#initialize flask app -app = Flask(__name__) - -#import model from the model file can be pretrained or fine tuned -blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") - - -# The route() function of the Flask class is a decorator, -# which tells the application which URL should call -# the associated function. -@app.route("/response", methods=["GET","POST"]) # API URL -def chatbot_response(): # function name - data = request.json # Take input as json format - blender_agent.observe({'text': data["UserText"], 'episode_done': False}) #Give User inputted text to model - response = blender_agent.act() #take response from the model - return {"response" : response['text']} #return model response - - -# main driver function -if __name__ == "__main__": - app.run() - -``` - -### How to - -Just change the model path and model name inside the create_agent_from_model_file function. - -```python -blender_agent = create_agent_from_model_file("Model Name") -``` - -Now Run the script and start the flask webserver - -Pass the message format defined below: - -```json -{ - "UserText" : "Your Inputted Text" -} -``` - -It will generate the below response - -```json -{ - "response": "Model Response" -} -``` \ No newline at end of file diff --git a/flask_api_demo/app.py b/flask_api_demo/app.py deleted file mode 100644 index 904b629d52b..00000000000 --- a/flask_api_demo/app.py +++ /dev/null @@ -1,27 +0,0 @@ -#import required libraries -from flask import Flask, render_template, request -from parlai.core.agents import create_agent_from_model_file - - -# Flask constructor takes the name of -#initialize flask app -app = Flask(__name__) - -#import model from the model file can be pretrained or fine tuned -blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") - - -# The route() function of the Flask class is a decorator, -# which tells the application which URL should call -# the associated function. -@app.route("/response", methods=["GET","POST"]) # API URL -def chatbot_response(): # function name - data = request.json # Take input as json format - blender_agent.observe({'text': data["UserText"], 'episode_done': False}) #Give User inputted text to model - response = blender_agent.act() #take response from the model - return {"response" : response['text']} #return model response - - -# main driver function -if __name__ == "__main__": - app.run() From a81ff9c4c76e6b93aa8bebb732d273b93e340186 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Wed, 26 Jan 2022 15:35:33 +0530 Subject: [PATCH 05/23] one the necessary changes as per suggestions --- docs/source/index.md | 1 + docs/source/tutorial_flask.md | 34 ++++++++++++++++++++++++++++++++++ parlai/scripts/flask_demo.py | 27 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 docs/source/tutorial_flask.md create mode 100644 parlai/scripts/flask_demo.py diff --git a/docs/source/index.md b/docs/source/index.md index fc4e03b646c..debc3e8286f 100644 --- a/docs/source/index.md +++ b/docs/source/index.md @@ -20,6 +20,7 @@ tutorial_chat_service tutorial_swap_components tutorial_tests tutorial_model_cards +tutorial_flask ``` ```{toctree} diff --git a/docs/source/tutorial_flask.md b/docs/source/tutorial_flask.md new file mode 100644 index 00000000000..9930d6d437c --- /dev/null +++ b/docs/source/tutorial_flask.md @@ -0,0 +1,34 @@ +## Flask API demo + +__Authors__: Khushal Jethava + + +### Parl.ai model implement on flask framework + +With this script, you can implement your fine-tune or pretrained parlai model very easily. + +### How to + +Just change the model path and model name inside the create_agent_from_model_file function. + +```python +blender_agent = create_agent_from_model_file("Model Name") +``` + +Now Run the script and start the flask webserver + +Pass the message format defined below: + +```json +{ + "UserText" : "Your Inputted Text" +} +``` + +It will generate below response + +```json +{ + "response": "Model Response" +} +``` \ No newline at end of file diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py new file mode 100644 index 00000000000..904b629d52b --- /dev/null +++ b/parlai/scripts/flask_demo.py @@ -0,0 +1,27 @@ +#import required libraries +from flask import Flask, render_template, request +from parlai.core.agents import create_agent_from_model_file + + +# Flask constructor takes the name of +#initialize flask app +app = Flask(__name__) + +#import model from the model file can be pretrained or fine tuned +blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") + + +# The route() function of the Flask class is a decorator, +# which tells the application which URL should call +# the associated function. +@app.route("/response", methods=["GET","POST"]) # API URL +def chatbot_response(): # function name + data = request.json # Take input as json format + blender_agent.observe({'text': data["UserText"], 'episode_done': False}) #Give User inputted text to model + response = blender_agent.act() #take response from the model + return {"response" : response['text']} #return model response + + +# main driver function +if __name__ == "__main__": + app.run() From 4c7ac448c69dec6f3ea5b5ced4ab422816c38313 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Wed, 26 Jan 2022 15:42:11 +0530 Subject: [PATCH 06/23] Update requirements.txt Added flask on requirements.txt file --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 638ff301f78..fcac740645f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +flask boto3==1.17.95 botocore==1.20.95 coloredlogs==14.0 From 29eff3b6f1c52fcb726f07722cd1d17efe7e26e0 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Wed, 26 Jan 2022 16:07:22 +0530 Subject: [PATCH 07/23] Update index.md --- docs/source/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/index.md b/docs/source/index.md index debc3e8286f..eec7c4008a1 100644 --- a/docs/source/index.md +++ b/docs/source/index.md @@ -17,10 +17,10 @@ tutorial_fast tutorial_mutators tutorial_crowdsourcing tutorial_chat_service +tutorial_flask tutorial_swap_components tutorial_tests tutorial_model_cards -tutorial_flask ``` ```{toctree} From 1fab18b6dd1383e9bb9aab093e6d0e699aa30a2d Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Thu, 3 Feb 2022 20:53:48 +0530 Subject: [PATCH 08/23] Revert "Update index.md" This reverts commit 29eff3b6f1c52fcb726f07722cd1d17efe7e26e0. --- docs/source/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/index.md b/docs/source/index.md index eec7c4008a1..debc3e8286f 100644 --- a/docs/source/index.md +++ b/docs/source/index.md @@ -17,10 +17,10 @@ tutorial_fast tutorial_mutators tutorial_crowdsourcing tutorial_chat_service -tutorial_flask tutorial_swap_components tutorial_tests tutorial_model_cards +tutorial_flask ``` ```{toctree} From 471f0eb61c39ed499fe63a38f297897ae3bc6ee5 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Thu, 3 Feb 2022 20:53:58 +0530 Subject: [PATCH 09/23] Revert "Update requirements.txt" This reverts commit 4c7ac448c69dec6f3ea5b5ced4ab422816c38313. --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index fcac740645f..638ff301f78 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -flask boto3==1.17.95 botocore==1.20.95 coloredlogs==14.0 From 4693d252370d3375843c28c9d305328f3aecf854 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Thu, 3 Feb 2022 21:33:07 +0530 Subject: [PATCH 10/23] Revert "one the necessary changes as per suggestions" This reverts commit a81ff9c4c76e6b93aa8bebb732d273b93e340186. --- docs/source/tutorial_flask.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/source/tutorial_flask.md b/docs/source/tutorial_flask.md index 9930d6d437c..2e04a2c8c60 100644 --- a/docs/source/tutorial_flask.md +++ b/docs/source/tutorial_flask.md @@ -7,12 +7,41 @@ __Authors__: Khushal Jethava With this script, you can implement your fine-tune or pretrained parlai model very easily. + +### Example Code + +```python + +from flask import Flask, render_template, request +from parlai.core.agents import create_agent_from_model_file + + +app = Flask(__name__) + +#import model from the model file can be pretrained or fine tuned +blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") + + +@app.route("/response", methods=["GET","POST"]) +def chatbot_response(): + data = request.json + blender_agent.observe({'text': data["UserText"], 'episode_done': False}) + response = blender_agent.act() + return {"response" : response['text']} + + +# main driver function +if __name__ == "__main__": + app.run() + +```` + ### How to Just change the model path and model name inside the create_agent_from_model_file function. ```python -blender_agent = create_agent_from_model_file("Model Name") +blender_agent = create_agent_from_model_file("path_to_model_or_zoo_name") ``` Now Run the script and start the flask webserver From 0c9b9fc11153e4f2496d292c778b5335eb57844a Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Thu, 3 Feb 2022 21:37:36 +0530 Subject: [PATCH 11/23] Deleted flask_demo.py Code has been added to doc file, so removing it for now so solve build_website error --- parlai/scripts/flask_demo.py | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 parlai/scripts/flask_demo.py diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py deleted file mode 100644 index 904b629d52b..00000000000 --- a/parlai/scripts/flask_demo.py +++ /dev/null @@ -1,27 +0,0 @@ -#import required libraries -from flask import Flask, render_template, request -from parlai.core.agents import create_agent_from_model_file - - -# Flask constructor takes the name of -#initialize flask app -app = Flask(__name__) - -#import model from the model file can be pretrained or fine tuned -blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") - - -# The route() function of the Flask class is a decorator, -# which tells the application which URL should call -# the associated function. -@app.route("/response", methods=["GET","POST"]) # API URL -def chatbot_response(): # function name - data = request.json # Take input as json format - blender_agent.observe({'text': data["UserText"], 'episode_done': False}) #Give User inputted text to model - response = blender_agent.act() #take response from the model - return {"response" : response['text']} #return model response - - -# main driver function -if __name__ == "__main__": - app.run() From 84261892185aeaf52d84da27e30ac9e9bdd29898 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Thu, 3 Feb 2022 22:00:43 +0530 Subject: [PATCH 12/23] Flask_demo added --- parlai/scripts/flask_demo.py | 21 +++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 22 insertions(+) create mode 100644 parlai/scripts/flask_demo.py diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py new file mode 100644 index 00000000000..c2a09aacd1d --- /dev/null +++ b/parlai/scripts/flask_demo.py @@ -0,0 +1,21 @@ +from flask import Flask, render_template, request +from parlai.core.agents import create_agent_from_model_file + + +app = Flask(__name__) + +#import model from the model file can be pretrained or fine tuned +blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") + + +@app.route("/response", methods=["GET","POST"]) +def chatbot_response(): + data = request.json + blender_agent.observe({'text': data["UserText"], 'episode_done': False}) + response = blender_agent.act() + return {"response" : response['text']} + + +# main driver function +if __name__ == "__main__": + app.run() diff --git a/requirements.txt b/requirements.txt index d8763a4fda0..cb221fd1c1b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ fairscale==0.3.7 docformatter==1.3.0 flake8-bugbear==19.8.0 flake8==3.7.8 +Flask==2.0.2 importlib-metadata<4.3 iopath~=0.1.8 gitdb2==2.0.5 From c987caf091523131af410c927251b770de451756 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Thu, 3 Feb 2022 22:44:18 +0530 Subject: [PATCH 13/23] Remove Model as global model --- docs/source/tutorial_flask.md | 17 ++++++++++------- parlai/scripts/flask_demo.py | 11 +++++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/docs/source/tutorial_flask.md b/docs/source/tutorial_flask.md index 2e04a2c8c60..dcbe7fb57a3 100644 --- a/docs/source/tutorial_flask.md +++ b/docs/source/tutorial_flask.md @@ -18,21 +18,24 @@ from parlai.core.agents import create_agent_from_model_file app = Flask(__name__) -#import model from the model file can be pretrained or fine tuned -blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") - +def Model_init(): + #import model from the model file can be pretrained or fine tuned + blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") + return blender_agent +#blender_agent = index() @app.route("/response", methods=["GET","POST"]) def chatbot_response(): - data = request.json - blender_agent.observe({'text': data["UserText"], 'episode_done': False}) + data = request.json + blender_agent.observe({'text': data["UserText"], 'episode_done': False}) response = blender_agent.act() - return {"response" : response['text']} + return {"response" : response['text']} # main driver function if __name__ == "__main__": - app.run() + blender_agent = Model_init() + app.run() ```` diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py index c2a09aacd1d..e638bd582e8 100644 --- a/parlai/scripts/flask_demo.py +++ b/parlai/scripts/flask_demo.py @@ -4,10 +4,12 @@ app = Flask(__name__) -#import model from the model file can be pretrained or fine tuned -blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") - +def Model_init(): + #import model from the model file can be pretrained or fine tuned + blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") + return blender_agent +#blender_agent = index() @app.route("/response", methods=["GET","POST"]) def chatbot_response(): data = request.json @@ -18,4 +20,5 @@ def chatbot_response(): # main driver function if __name__ == "__main__": - app.run() + blender_agent = Model_init() + app.run() From ec5aeed9e7877d70640e9508afd5458641f4f56b Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Thu, 3 Feb 2022 22:53:47 +0530 Subject: [PATCH 14/23] update tutorial_flask --- docs/source/tutorial_flask.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/source/tutorial_flask.md b/docs/source/tutorial_flask.md index dcbe7fb57a3..fbe319e4930 100644 --- a/docs/source/tutorial_flask.md +++ b/docs/source/tutorial_flask.md @@ -23,7 +23,6 @@ def Model_init(): blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") return blender_agent -#blender_agent = index() @app.route("/response", methods=["GET","POST"]) def chatbot_response(): data = request.json From 7116158ec95889fb0b512ae5d02c267b1c8439dd Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Sun, 6 Feb 2022 13:08:46 +0530 Subject: [PATCH 15/23] Updated tutorial_flask.md --- docs/source/tutorial_flask.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/tutorial_flask.md b/docs/source/tutorial_flask.md index fbe319e4930..c7dbb502e2b 100644 --- a/docs/source/tutorial_flask.md +++ b/docs/source/tutorial_flask.md @@ -1,9 +1,9 @@ -## Flask API demo +# Flask API demo __Authors__: Khushal Jethava -### Parl.ai model implement on flask framework +## Parl.ai model implement on flask framework With this script, you can implement your fine-tune or pretrained parlai model very easily. From 0fd475d9e1cdb0c38874353bd80b5b5ada5e1976 Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Sun, 6 Feb 2022 14:13:16 +0530 Subject: [PATCH 16/23] updated flask_demo.py --- parlai/scripts/flask_demo.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py index e638bd582e8..1ee111e0971 100644 --- a/parlai/scripts/flask_demo.py +++ b/parlai/scripts/flask_demo.py @@ -4,21 +4,16 @@ app = Flask(__name__) -def Model_init(): - #import model from the model file can be pretrained or fine tuned - blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") - return blender_agent -#blender_agent = index() @app.route("/response", methods=["GET","POST"]) def chatbot_response(): data = request.json blender_agent.observe({'text': data["UserText"], 'episode_done': False}) response = blender_agent.act() - return {"response" : response['text']} + return {"response" : response['text']} # main driver function if __name__ == "__main__": - blender_agent = Model_init() - app.run() + blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") + app.run() From 2fd90b9af5ef6cbb34d5770656c9b3403a66fe72 Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Sun, 6 Feb 2022 13:38:03 -0500 Subject: [PATCH 17/23] Lint. --- docs/source/tutorial_flask.md | 9 ++++++--- parlai/scripts/flask_demo.py | 11 +++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/source/tutorial_flask.md b/docs/source/tutorial_flask.md index c7dbb502e2b..39355fc7f9b 100644 --- a/docs/source/tutorial_flask.md +++ b/docs/source/tutorial_flask.md @@ -12,14 +12,16 @@ With this script, you can implement your fine-tune or pretrained parlai model ve ```python -from flask import Flask, render_template, request +from flask import Flask, request + from parlai.core.agents import create_agent_from_model_file app = Flask(__name__) def Model_init(): - #import model from the model file can be pretrained or fine tuned + # import model from the model file can be pretrained or fine tuned + blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") return blender_agent @@ -52,7 +54,8 @@ Pass the message format defined below: ```json { - "UserText" : "Your Inputted Text" + "UserText" : "Your Inputted Text" + } ``` diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py index 1ee111e0971..4905b7e0389 100644 --- a/parlai/scripts/flask_demo.py +++ b/parlai/scripts/flask_demo.py @@ -1,11 +1,18 @@ -from flask import Flask, render_template, request +#!/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. + +from flask import Flask, request from parlai.core.agents import create_agent_from_model_file app = Flask(__name__) -@app.route("/response", methods=["GET","POST"]) +@app.route("/response", methods=("GET", "POST")) + def chatbot_response(): data = request.json blender_agent.observe({'text': data["UserText"], 'episode_done': False}) From 4844dd06035de17b21cd0ad1cc73e45eb206e606 Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Sun, 6 Feb 2022 13:39:46 -0500 Subject: [PATCH 18/23] Update parlai/scripts/flask_demo.py lint again --- parlai/scripts/flask_demo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py index 4905b7e0389..86688b7220d 100644 --- a/parlai/scripts/flask_demo.py +++ b/parlai/scripts/flask_demo.py @@ -12,7 +12,6 @@ @app.route("/response", methods=("GET", "POST")) - def chatbot_response(): data = request.json blender_agent.observe({'text': data["UserText"], 'episode_done': False}) From 5e254d3df353e3fca007cd70d988c56c44725fcf Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Sun, 6 Feb 2022 13:41:49 -0500 Subject: [PATCH 19/23] Update parlai/scripts/flask_demo.py lol keep linting --- parlai/scripts/flask_demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py index 86688b7220d..1e3c0c82f90 100644 --- a/parlai/scripts/flask_demo.py +++ b/parlai/scripts/flask_demo.py @@ -16,7 +16,7 @@ def chatbot_response(): data = request.json blender_agent.observe({'text': data["UserText"], 'episode_done': False}) response = blender_agent.act() - return {"response" : response['text']} + return {'response': response['text']} # main driver function From 1a93fa135e62f4a3436f37ec961f87b43c9456ca Mon Sep 17 00:00:00 2001 From: Khushal Jethava Date: Wed, 23 Feb 2022 22:01:56 +0530 Subject: [PATCH 20/23] Few Changes to Improve script --- docs/source/tutorial_flask.md | 18 ++++++++---------- parlai/scripts/flask_demo.py | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/docs/source/tutorial_flask.md b/docs/source/tutorial_flask.md index 39355fc7f9b..6a96a193c69 100644 --- a/docs/source/tutorial_flask.md +++ b/docs/source/tutorial_flask.md @@ -12,31 +12,29 @@ With this script, you can implement your fine-tune or pretrained parlai model ve ```python -from flask import Flask, request +#!/usr/bin/env python3 + +from flask import Flask, request from parlai.core.agents import create_agent_from_model_file app = Flask(__name__) -def Model_init(): - # import model from the model file can be pretrained or fine tuned +blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") - blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") - return blender_agent - -@app.route("/response", methods=["GET","POST"]) +@app.route("/response", methods=("GET", "POST")) def chatbot_response(): data = request.json blender_agent.observe({'text': data["UserText"], 'episode_done': False}) response = blender_agent.act() - return {"response" : response['text']} + return {'response': response['text']} # main driver function if __name__ == "__main__": - blender_agent = Model_init() - app.run() + app.run() + ```` diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py index 1e3c0c82f90..c009e9cabb7 100644 --- a/parlai/scripts/flask_demo.py +++ b/parlai/scripts/flask_demo.py @@ -10,6 +10,7 @@ app = Flask(__name__) +blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") @app.route("/response", methods=("GET", "POST")) def chatbot_response(): @@ -21,5 +22,4 @@ def chatbot_response(): # main driver function if __name__ == "__main__": - blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") app.run() From 9df2ff5b75261e4eec32b4284ce2cf864158ab1a Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Mon, 21 Mar 2022 09:29:32 -0400 Subject: [PATCH 21/23] Wrap in a proper script. --- docs/source/tutorial_flask.md | 66 ----------------------------------- parlai/scripts/flask_demo.py | 52 ++++++++++++++++++++------- requirements.txt | 1 - 3 files changed, 40 insertions(+), 79 deletions(-) delete mode 100644 docs/source/tutorial_flask.md diff --git a/docs/source/tutorial_flask.md b/docs/source/tutorial_flask.md deleted file mode 100644 index 6a96a193c69..00000000000 --- a/docs/source/tutorial_flask.md +++ /dev/null @@ -1,66 +0,0 @@ -# Flask API demo - -__Authors__: Khushal Jethava - - -## Parl.ai model implement on flask framework - -With this script, you can implement your fine-tune or pretrained parlai model very easily. - - -### Example Code - -```python - -#!/usr/bin/env python3 - - -from flask import Flask, request -from parlai.core.agents import create_agent_from_model_file - - -app = Flask(__name__) - -blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") - -@app.route("/response", methods=("GET", "POST")) -def chatbot_response(): - data = request.json - blender_agent.observe({'text': data["UserText"], 'episode_done': False}) - response = blender_agent.act() - return {'response': response['text']} - - -# main driver function -if __name__ == "__main__": - app.run() - - -```` - -### How to - -Just change the model path and model name inside the create_agent_from_model_file function. - -```python -blender_agent = create_agent_from_model_file("path_to_model_or_zoo_name") -``` - -Now Run the script and start the flask webserver - -Pass the message format defined below: - -```json -{ - "UserText" : "Your Inputted Text" - -} -``` - -It will generate below response - -```json -{ - "response": "Model Response" -} -``` \ No newline at end of file diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask_demo.py index c009e9cabb7..e1f02c07f0f 100644 --- a/parlai/scripts/flask_demo.py +++ b/parlai/scripts/flask_demo.py @@ -4,22 +4,50 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. -from flask import Flask, request -from parlai.core.agents import create_agent_from_model_file +""" +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 +``` -app = Flask(__name__) +**Hitting the API*** +```shell +curl -k http://localhost:5000/response -H "Content-Type: application/json" -d '{"text": "foobar"}' +``` +""" -blender_agent = create_agent_from_model_file("zoo:blender/blender_90M/model") +from parlai.core.agents import create_agent +from parlai.core.params import ParlaiParser +from parlai.core.script import ParlaiScript, register_script -@app.route("/response", methods=("GET", "POST")) -def chatbot_response(): - data = request.json - blender_agent.observe({'text': data["UserText"], 'episode_done': False}) - response = blender_agent.act() - return {'response': response['text']} + +@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, request + + self.agent = create_agent(self.opt) + app = Flask("parlai_flask") + app.route("/response", methods=("GET", "POST"))(self.chatbot_response) + app.run() -# main driver function if __name__ == "__main__": - app.run() + Flask.main() diff --git a/requirements.txt b/requirements.txt index cb221fd1c1b..d8763a4fda0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,6 @@ fairscale==0.3.7 docformatter==1.3.0 flake8-bugbear==19.8.0 flake8==3.7.8 -Flask==2.0.2 importlib-metadata<4.3 iopath~=0.1.8 gitdb2==2.0.5 From 5cf1de276f1b630515d5bdd4fa0434ede0ddbaed Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Mon, 21 Mar 2022 11:27:07 -0400 Subject: [PATCH 22/23] Whoops --- docs/source/index.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/source/index.md b/docs/source/index.md index debc3e8286f..fc4e03b646c 100644 --- a/docs/source/index.md +++ b/docs/source/index.md @@ -20,7 +20,6 @@ tutorial_chat_service tutorial_swap_components tutorial_tests tutorial_model_cards -tutorial_flask ``` ```{toctree} From 75f2d441e0f985a9125488d13818052c1bcee602 Mon Sep 17 00:00:00 2001 From: Stephen Roller Date: Mon, 21 Mar 2022 11:57:02 -0400 Subject: [PATCH 23/23] Lint. --- parlai/scripts/{flask_demo.py => flask.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename parlai/scripts/{flask_demo.py => flask.py} (93%) diff --git a/parlai/scripts/flask_demo.py b/parlai/scripts/flask.py similarity index 93% rename from parlai/scripts/flask_demo.py rename to parlai/scripts/flask.py index e1f02c07f0f..fcf8c47cba0 100644 --- a/parlai/scripts/flask_demo.py +++ b/parlai/scripts/flask.py @@ -41,7 +41,7 @@ def chatbot_response(self): return {'response': response['text']} def run(self): - from flask import Flask, request + from flask import Flask self.agent = create_agent(self.opt) app = Flask("parlai_flask")