Skip to content

Exposing your application as a service.

Mario Muñoz edited this page Jun 25, 2014 · 1 revision

You have an available sample of exposing an HTTP service within the file lib/server.py. Usually, you only need to create an annotated method that wraps the call for the application you created in the previous step. You can customize the context where you want your service to be deployed using a decorator:

app = Flask(__name__)
sentiment_analyzer = SimpleSentimentAnalyzer()

@app.route("/sentiment", methods=["POST"])
def sentiment():
    input = json.loads(request.data)
    text = input.get("input", "")
    sentiment = sentiment_analyzer.calculate_sentiment(text)
    response = {"@context": "http://eurosentiment.eu/contexts/basecontext.jsonld",
                "@type": "marl:SentimentAnalysis",
                "marl:polarityValue": sentiment}
    return Response(json.dumps(response), mimetype="application/json")

if __name__ == "__main__":
    app.debug = True
    app.run()

In the given example, the final service will be exposed at http://0.0.0.0:5000/sentiment (as specified in @app.route decorator) and it will accept POST requests, returning a JSON NIF result.