Skip to content

Commit

Permalink
basic api #35
Browse files Browse the repository at this point in the history
  • Loading branch information
fmikaelian committed Apr 25, 2019
1 parent 49ba859 commit 5cf9618
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ A complete worfklow is described in our [`examples`](examples) notebook.
You can deploy a `cdQA` REST API by executing:

```shell
python api.py
FLASK_APP=api.py flask run -h 0.0.0.0
```

To try it, execute:

```shell
curl http://127.0.0.1:5000/
http localhost:5000/api q=='your question here'
```

If you wish to serve a user interface, follow the instructions of [cdQA-ui](https://github.com/fmikaelian/cdQA-ui), a web interface developed for `cdQA`.
Expand Down
29 changes: 20 additions & 9 deletions api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
from flask import Flask
from flask_restful import Resource, Api
from flask import Flask, request, jsonify

import os
from ast import literal_eval
import pandas as pd

from cdqa.utils.converter import filter_paragraphs
from cdqa.reader.bertqa_sklearn import BertQA
from cdqa.pipeline.qa_pipeline import QAPipeline

app = Flask(__name__)
api = Api(app)

class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
df = pd.read_csv('../data/bnpp_newsroom_v1.0/bnpp_newsroom-v1.0.csv', converters={'paragraphs': literal_eval})
df['paragraphs'] = df['paragraphs'].apply(filter_paragraphs)
df['content'] = df['paragraphs'].apply(lambda x: ' '.join(x))

api.add_resource(HelloWorld, '/')
qa_pipe = QAPipeline(model = '../models/bert_qa_squad_vCPU/bert_qa_squad_vCPU-sklearn.joblib')
qa_pipe.fit(df)

if __name__ == '__main__':
app.run(debug=True)
@app.route('/api', methods=['GET'])
def api():
query = request.query_string
prediction = qa_pipe.answer(query)
return jsonify(query=query,
prediction=prediction)

0 comments on commit 5cf9618

Please sign in to comment.