Commit 188c3333 authored by J. Fernando Sánchez's avatar J. Fernando Sánchez
Browse files

Removed nbsphinx

It requires pandoc, which cannot be installed with pip.

We can either link to the nbfile or convert the file
manually/automatically:

```
nbconvert SenpyClientUse.ipynb --to rst
```
parent 955e17eb
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Client # Client
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The built-in senpy client allows you to query any Senpy endpoint. We will illustrate how to use it with the public demo endpoint, and then show you how to spin up your own endpoint using docker.
%% Cell type:markdown id: tags:
Demo Endpoint Demo Endpoint
------------- -------------
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Import Client and send a request To start using senpy, simply create a new Client and point it to your endpoint. In this case, the latest version of Senpy at GSI.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from senpy.client import Client from senpy.client import Client
c = Client('http://latest.senpy.cluster.gsi.dit.upm.es/api') c = Client('http://latest.senpy.cluster.gsi.dit.upm.es/api')
r = c.analyse('I like Pizza', algorithm='sentiment140')
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Print response Now, let's use that client analyse some queries:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
for entry in r.entries: r = c.analyse('I like sugar!!', algorithm='sentiment140')
print('{} -> {}'.format(entry['text'], entry['sentiments'][0]['marl:hasPolarity'])) r
``` ```
%% Output
I like Pizza -> marl:Positive
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Obtain a list of available plugins As you can see, that gave us the full JSON result. A more concise way to print it would be:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
for plugin in c.request('/plugins')['plugins']: for entry in r.entries:
print(plugin['name']) print('{} -> {}'.format(entry['text'], entry['sentiments'][0]['marl:hasPolarity']))
``` ```
%% Output
emoRand
rand
sentiment140
%% Cell type:markdown id: tags:
Local Endpoint
--------------
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Run a docker container with Senpy image and default plugins We can also obtain a list of available plugins with the client:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
!docker run -ti --name 'SenpyEndpoint' -d -p 5000:5000 gsiupm/senpy:0.8.6 --host 0.0.0.0 --default-plugins c.plugins()
``` ```
%% Output
a0157cd98057072388bfebeed78a830da7cf0a796f4f1a3fd9188f9f2e5fe562
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Import client and send a request to localhost Or, more concisely:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
c_local = Client('http://127.0.0.1:5000/api') c.plugins().keys()
r = c_local.analyse('Hello world', algorithm='sentiment140')
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Print response Local Endpoint
--------------
%% Cell type:markdown id: tags:
To run your own instance of senpy, just create a docker container with the latest Senpy image. Using `--default-plugins` you will get some extra plugins to start playing with the API.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
for entry in r.entries: !docker run -ti --name 'SenpyEndpoint' -d -p 6000:5000 gsiupm/senpy:0.8.6 --host 0.0.0.0 --default-plugins
print('{} -> {}'.format(entry['text'], entry['sentiments'][0]['marl:hasPolarity']))
``` ```
%% Output
Hello world -> marl:Neutral
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Obtain a list of available plugins deployed locally To use this endpoint:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
for plugin in c_local.request('/plugins')['plugins']: c_local = Client('http://127.0.0.1:6000/api')
print(plugin['name'])
``` ```
%% Output
rand
sentiment140
emoRand
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Stop the docker container That's all! After you are done with your analysis, stop the docker container:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
!docker stop SenpyEndpoint !docker stop SenpyEndpoint
!docker rm SenpyEndpoint !docker rm SenpyEndpoint
``` ```
%% Output
SenpyEndpoint
SenpyEndpoint
......
Client demo
===========
This video shows how to use senpy through command-line tool.
.. image:: https://asciinema.org/a/9uwef1ghkjk062cw2t4mhzpyk.png
:width: 100%
:target: https://asciinema.org/a/9uwef1ghkjk062cw2t4mhzpyk
:alt: CLI demo