Skip to content
This repository was archived by the owner on Mar 29, 2021. It is now read-only.

Commit be3ed9f

Browse files
author
staticdev
committed
melhorias diversas
1 parent 9884a4e commit be3ed9f

File tree

6 files changed

+141
-133
lines changed

6 files changed

+141
-133
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:2.7.14-slim
1+
FROM python:2.7.15-slim
22

33
# set the working directory to /app
44
WORKDIR /app
@@ -25,4 +25,4 @@ RUN cd ugc_norm && sh configure.sh
2525
EXPOSE 5000
2626

2727
# run app.py when the container launches
28-
CMD ["gunicorn", "app:APP", "-b", ":5000"]
28+
CMD ["gunicorn", "__init__:create_app()", "-b", ":5000"]

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
ugcnormal-microservice
2+
======================
3+
4+
Microsserviço REST para normalização pt\_BR usando o
5+
[UGCNormal](https://github.com/avanco/UGCNormal). Ideal para aplicações
6+
que precisam de normalização online como chatbots.
7+
8+
Webservice baseado no ugcnormal\_interface
9+
[](https://github.com/thiagootuler/ugcnormal_interface).
10+
11+
Requisitos
12+
----------
13+
14+
- Instalar Docker-CE 18.03.1+
15+
- 900 Mb de espaço em disco para imagem
16+
17+
Execução
18+
--------
19+
20+
Rodar os comandos:
21+
22+
``` {.sourceCode .sh}
23+
# gerar a imagem
24+
sudo docker build -t staticdev/ugcnormal:0.1.1 .
25+
# verificar se gerou
26+
sudo docker images
27+
# instanciar imagem
28+
sudo docker run --name ugcnormal -d -p 5000:5000 --env "UGCNORMAL=./ugc_norm/speller" staticdev/ugcnormal:0.1.1
29+
# conferir processo rodando
30+
sudo docker ps -a
31+
32+
# para parar o container olhe o nome dele no docker ps -a e execute
33+
sudo docker stop ugcnormal
34+
# para remover um container (precisa parar primeiro)
35+
sudo docker rm ugcnormal
36+
```
37+
38+
Exemplos de uso
39+
---------------
40+
41+
Basta fazer um POST da mensagem a ser normalizada na url /reply passando
42+
a mensagem no campo "message" e o método no campo "method".
43+
44+
Métodos disponíveis:
45+
46+
- token: tokenizer
47+
- spell: speller
48+
- acronym: acronyms searcher
49+
- textese: untextese
50+
- proper\_noun: proper noun normalizer
51+
52+
A mensagem normalizada é retornada no campo "reply". O status da
53+
requisição no campo "status", tendo com valor padrão para sucesso "ok".
54+
55+
Exemplo curl:
56+
57+
``` {.sourceCode .sh}
58+
curl -X POST \
59+
http://localhost:5000/reply \
60+
-H 'content-type: application/json; charset=utf-8' \
61+
-d '{
62+
"message": "oi td bm?",
63+
"method": "spell"
64+
}'
65+
```
66+
67+
Exemplo python3 nativo (http.client):
68+
69+
``` {.sourceCode .python}
70+
import http.client
71+
72+
conn = http.client.HTTPConnection("localhost:5000")
73+
74+
payload = "{\"message\": \"oi td bm?\", \"method\": \"spell\"}"
75+
76+
headers = {
77+
'content-type': "application/json; charset=utf-8"
78+
}
79+
80+
conn.request("POST", "/reply", payload, headers)
81+
res = conn.getresponse()
82+
data = res.read()
83+
84+
print(data.decode("utf-8"))
85+
```

README.rst

Lines changed: 0 additions & 80 deletions
This file was deleted.

__init__.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
from flask import Flask, request, jsonify
4+
from normalizer import Normalizer
5+
6+
def create_app(test_config=None):
7+
app = Flask(__name__)
8+
app.config['JSON_AS_ASCII'] = False # retrieve UTF-8 messages
9+
10+
norm = Normalizer()
11+
12+
@app.route('/reply', methods=['POST'])
13+
def reply():
14+
params = request.json
15+
if not params:
16+
return jsonify({
17+
"status": "error",
18+
"error": "Request must be of the application/json type!",
19+
})
20+
21+
message = params.get("message")
22+
method = params.get("method")
23+
24+
# Make sure the required params are present.
25+
if message is None or method is None:
26+
return jsonify({
27+
"status": "error",
28+
"error": "message and method are required keys"
29+
})
30+
31+
methods = {'token':norm.tokenizer,
32+
'spell':norm.speller,
33+
'acronym':norm.acronym_searcher,
34+
'textese':norm.untextese,
35+
'proper_noun':norm.proper_noun_normalizer
36+
}
37+
38+
try:
39+
reply = methods[method](message)
40+
except KeyError:
41+
return jsonify({
42+
"status": "error",
43+
"error": "method not valid, try one of the following: token, spell, acronym, textese or proper_noun"
44+
})
45+
46+
# Send the response.
47+
return jsonify({
48+
"status": "ok",
49+
"reply": reply
50+
})
51+
52+
return app

app.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
numpy
22
scipy
3-
Flask==0.12.2
4-
gunicorn==19.7.1
3+
Flask==1.0.2
4+
gunicorn==19.9.0
55
multiprocessing
66
nltk
77
sklearn

0 commit comments

Comments
 (0)