Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.wav
!bird1.wav
.vscode
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
hello:
echo "plop"

proto:
python -m grpc_tools.protoc -I protos --python_out=. --grpc_python_out=. protos/helloworld.proto

server:
python greeter_server.py

client:
python greeter_client.py
4 changes: 4 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
pylama = "*"
ipdb = "*"

[packages]
flask = "*"
Expand All @@ -15,6 +17,8 @@ librosa = "*"
keras = "*"
matplotlib = "*"
scipy = "*"
grpcio = "*"
grpcio-tools = "*"

[requires]
python_version = "3.7"
270 changes: 243 additions & 27 deletions Pipfile.lock

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# GRPC

# 1) Run the server
`pipenv run make server`

# 2) In another terminal, run the client
`pipenv run make client`

Don't forget to add an audiofile in repository for example.
And update line 30 `AUDIO_FILE_NAME = "SWIFT_20190725_080011_2_4.wav"` in `greeter_client.py`

# To update the gRPC code used by our application to use the new service definition.
`pipenv run make proto`

# To adapt client for web (react)
See (Write JS Client Code Section)[https://grpc.io/docs/tutorials/basic/web/]

### Fore more informations
See [GRPC offical documentations](https://grpc.io/)
60 changes: 60 additions & 0 deletions greeter_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: FIX ERROR
# grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
# status = StatusCode.RESOURCE_EXHAUSTED
# details = "Received message larger than max (4194305 vs. 4194304)"
# debug_error_string = "{"created":"@1574162543.663157000","description":"Error received from peer ipv6:[::1]:50051","file":"src/core/lib/surface/call.cc","file_line":1055,"grpc_message":"Received message larger than max (4194305 vs. 4194304)","grpc_status":8}
"""The Python implementation of the GRPC helloworld.Greeter client."""

from __future__ import print_function
import logging

import grpc
import os

import helloworld_pb2
import helloworld_pb2_grpc

AUDIO_FILE_NAME = "SWIFT_20190725_080011_2_4.wav"
AUDIO_PATH = os.path.join(AUDIO_FILE_NAME)


def get_audio_bytes(path=AUDIO_PATH):
with open(path, "rb") as fd:
contents = fd.read()
return contents


def run():
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
# used in circumstances in which the with statement does not fit the needs
# of the code.
with grpc.insecure_channel('localhost:50051') as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)

# Route Prediction
audio = get_audio_bytes()
print(len(audio))
prediction = stub.GetPrediction(helloworld_pb2.AudioFile(
audiofile=audio,
filename=AUDIO_FILE_NAME))
print(prediction)


if __name__ == '__main__':
logging.basicConfig()
run()
70 changes: 70 additions & 0 deletions greeter_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Python implementation of the GRPC helloworld.Greeter server."""

from concurrent import futures
import logging
import os

import grpc

import helloworld_pb2
import helloworld_pb2_grpc
from microfaune_package.microfaune.detection import RNNDetector


def save_file_in_wave(response, file_name='myfile.wav'):
if os.path.exists(file_name):
os.remove(file_name)
with open(file_name, mode='bx') as f:
f.write(response)


class Greeter(helloworld_pb2_grpc.GreeterServicer):
def __init__(self):
self.model = RNNDetector('models/model_weights-20190919_220113.h5')

def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)

def GetPrediction(self, request, context):
audiofile = request.audiofile
save_file_in_wave(audiofile, file_name=request.filename)
if os.path.exists(request.filename):
print("FILE EXISTS")

print(self.model)
pred = self.model.predict_on_wav(request.filename)

prediction_list = pred[1].tolist()
return helloworld_pb2.Prediction(prediction_list=prediction_list)


def serve():
options = [
('grpc.max_send_message_length', 50 * 1024 * 1024),
('grpc.max_receive_message_length', 50 * 1024 * 1024)
]
server = grpc.server(
futures.ThreadPoolExecutor(max_workers=10),
options=options)
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()


if __name__ == '__main__':
logging.basicConfig()
serve()
Loading