clip-as-service icon indicating copy to clipboard operation
clip-as-service copied to clipboard

Run bert-as-service on google colab

Open ctrboundary opened this issue 5 years ago • 46 comments

Prerequisites

Please fill in by replacing [ ] with [x].

System information

Some of this information can be collected via this script.

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04
  • TensorFlow installed from (source or binary): source
  • TensorFlow version: 1.14.0-rc1
  • Python version: 3.6.7
  • bert-as-service version: 1.9.1
  • GPU model and memory: Google Colab (Tesla K80)
  • CPU model and memory:

I'm trying to run bert-as-service on google colab jupyter notebook, the server runs perfectly. the only problem is client serving which need gpu ip address. is there any way to run it on google colab ?

...

ctrboundary avatar Jun 13 '19 17:06 ctrboundary

I have found a way to get it working in colab:

Install the server using this command (instead of the standard, may have to reset the runtime if the standard server is already installed) to enable http:

!pip install -U bert-serving-server[http]

Start the server with this code in the notebook (replacing the model directory with your own):

get_ipython().system_raw(
    'bert-serving-start -model_dir=./cased_L-24_H-1024_A-16 -http_port 3333 &'
)

Define this function:

import json
import requests
def get_embeddings(texts):
    headers = {
        'content-type':'application/json'
    }
    data = {
        "id":123,
        "texts":texts,
        "is_tokenized": False
    }
    data = json.dumps(data)
    r = requests.post("http://localhost:3333/encode", data=data, headers=headers).json()
    return r['result']

You can now get the embeddings like so (you may have to wait a couple of minutes for the server to start up first):

text_embeddings = get_embeddings(text_list)

geoffreyp7 avatar Jun 29 '19 11:06 geoffreyp7

Got it to work, but I had to run !pip install --upgrade ipykernel first, otherwise I couldn't reconnect to runtime (would stay on 'Busy').

rjadr avatar Sep 03 '19 15:09 rjadr

@geoffreyp7 - Can you please tell what output are you getting after, running this piece of code get_ipython().system_raw( 'bert-serving-start -model_dir=./cased_L-24_H-1024_A-16 -http_port 3333 &' )

I am getting error following your methodology. After calling , text_embeddings = get_embeddings(my_input_list), I am getting this error -

HTTPConnectionPool(host='localhost', port=3333): Max retries exceeded with url: /encode (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc2daee05c0>: Failed to establish a new connection: [Errno 111] Connection refused',))

Can you tell why it's not being connected to server. I followed exactly what you said. Please help, Thanks in advance.

ReasonablyAnon avatar Nov 07 '19 21:11 ReasonablyAnon

@AshwatKumar I got a same error. I resolve the issue by changing port number. This is what I did to get embeddings. I just followed @geoffreyp7 's procedure :)

  1. Install a module !pip install -U bert-serving-server[http]

  2. Download pre-trained model and unzip it !wget https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip !unzip uncased_L-12_H-768_A-12.zip

  3. Set port number port_num='3399'

  4. Start the server in the notebook

get_ipython().system_raw(
    'bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 -http_port ' + port_num + ' &'
)
  1. Define this function:
import json
import requests
def get_embeddings(texts):
    headers = {
        'content-type':'application/json'
    }
    data = {
        "id":123,
        "texts":texts,
        "is_tokenized": False
    }
    data = json.dumps(data)
    r = requests.post("http://localhost:" + port_num + "/encode", data=data, headers=headers).json()
    return r['result']
  1. Get embeddings
text_list1=['This is a test sentence.']
text_embeddings1 = get_embeddings(text_list1)

dstaka avatar Nov 12 '19 10:11 dstaka

Hi I get also the connection refused Error, Changing the port number has no effect. I started successfully Flask Servers with different ports before, but Bert Service fails to start

DHOFM avatar Dec 02 '19 14:12 DHOFM

Got it running using Bert Client and nohup - here is how it worked for me:

!pip install bert-serving-client !pip install -U bert-serving-server[http]

!wget https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip !unzip uncased_L-12_H-768_A-12.zip !nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 > out.file 2>&1 &

from bert_serving.client import BertClient bc = BertClient() print (bc.encode(['First do it', 'then do it right', 'then do it better']))

DHOFM avatar Dec 02 '19 15:12 DHOFM

Got it running using Bert Client and nohup - here is how it worked for me:

!pip install bert-serving-client !pip install -U bert-serving-server[http]

!wget https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip !unzip uncased_L-12_H-768_A-12.zip !nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 > out.file 2>&1 &

from bert_serving.client import BertClient bc = BertClient() print (bc.encode(['First do it', 'then do it right', 'then do it better']))

it worked for me as well , thank you!

RohanSaxena14 avatar Dec 12 '19 17:12 RohanSaxena14

DHOFM's answer also worked for me, but make sure Colab is using Tensorflow 1.x, because bert-serving-start doesn't currently work with TF 2.1 and nohup hides the output of the command failing.

karlhayek avatar Jan 23 '20 10:01 karlhayek

For me all works from DHOFM's answer before line "bc = BertClient()" In this part a block loading infinite time without warning throwing or else info. Someone in the know about this?

DaniilRoman avatar Feb 26 '20 17:02 DaniilRoman

For me all works from DHOFM's answer before line "bc = BertClient()" In this part a block loading infinite time without warning throwing or else info. Someone in the know about this?

Changing Tensorflow version to 1 worked for me

AshikSaibabu avatar Apr 24 '20 07:04 AshikSaibabu

Got it running using Bert Client and nohup - here is how it worked for me:

!pip install bert-serving-client !pip install -U bert-serving-server[HTTP]

!wget https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip !unzip uncased_L-12_H-768_A-12.zip !nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 > out.file 2>&1 &

from bert_serving.client import BertClient bc = BertClient() print (bc.encode(['First do it', 'then do it right', 'then do it better']))

@DHOFM This isn't working anymore. When I run the following code, it goes into infinite loop and does not executes. Changing to Tf version 1.x also does not work.

bc = BertClient()

iknoorjobs avatar Apr 29 '20 16:04 iknoorjobs

iknoorjobs, please look for the spaces and directory address in '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 > out.file 2>&1 &'

It was initially not running for me but it started working for me after I fixed the issues with spaces and directory path.

GaneshGS avatar May 05 '20 14:05 GaneshGS

iknoorjobs, please look for the spaces and directory address in '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 > out.file 2>&1 &'

It was initially not running for me but it started working for me after I fixed the issues with spaces and directory path.

Can you help me please , i have the same problem , i fixed the spaces but dosen't worked for me image

SlimenBouras avatar May 05 '20 15:05 SlimenBouras

I have the same problem here with bc=BertClient() image

SlimenBouras avatar May 05 '20 15:05 SlimenBouras

Hi,

Please follow the instructions.

  1. uninstall 'tensorflow-gpu==1.12.0' and install 'tensorflow==1.15.0'
  2. replace the !nohup command in your code 6 with the one below '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12

out.file2>&1&'

  1. type !ls and check the directory whether you can see unzipped 'uncased_L- 12_H-768_A-12' file in the directory or not.

Now, you run it and get back to me.

On Tue, May 5, 2020 at 11:22 AM SlimenBouras [email protected] wrote:

I have the same problem here with bc=BertClient() [image: image] https://user-images.githubusercontent.com/55366848/81083543-9fa46a80-8eec-11ea-944a-f2a07d58c7e1.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/hanxiao/bert-as-service/issues/380#issuecomment-624120929, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJX4CBCFQXLSMAQJTG3RQAVNDANCNFSM4HX4JNLQ .

-- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada

GaneshGS avatar May 05 '20 16:05 GaneshGS

Hi, Please follow the instructions. 1. uninstall 'tensorflow-gpu==1.12.0' and install 'tensorflow==1.15.0' 2. replace the !nohup command in your code 6 with the one below '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 out.file2>&1&' 3. type !ls and check the directory whether you can see unzipped 'uncased_L- 12_H-768_A-12' file in the directory or not. Now, you run it and get back to me. On Tue, May 5, 2020 at 11:22 AM SlimenBouras @.***> wrote: I have the same problem here with bc=BertClient() [image: image] https://user-images.githubusercontent.com/55366848/81083543-9fa46a80-8eec-11ea-944a-f2a07d58c7e1.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJX4CBCFQXLSMAQJTG3RQAVNDANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada

thank you for answering me, i applied your instructions, how long should i wait? image

SlimenBouras avatar May 05 '20 16:05 SlimenBouras

for first two lines, you used 'pip', change it to '!pip'

On Tue, May 5, 2020 at 12:32 PM SlimenBouras [email protected] wrote:

Hi, Please follow the instructions. 1. uninstall 'tensorflow-gpu==1.12.0' and install 'tensorflow==1.15.0' 2. replace the !nohup command in your code 6 with the one below '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 out.file2>&1&' 3. type !ls and check the directory whether you can see unzipped 'uncased_L- 12_H-768_A-12' file in the directory or not. Now, you run it and get back to me. … <#m_8545851393258997135_> On Tue, May 5, 2020 at 11:22 AM SlimenBouras @.***> wrote: I have the same problem here with bc=BertClient() [image: image] https://user-images.githubusercontent.com/55366848/81083543-9fa46a80-8eec-11ea-944a-f2a07d58c7e1.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 (comment) https://github.com/hanxiao/bert-as-service/issues/380#issuecomment-624120929>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJX4CBCFQXLSMAQJTG3RQAVNDANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada

thank you for answering me, i applied your instructions, how long should i wait? [image: image] https://user-images.githubusercontent.com/55366848/81090892-535e2800-8ef6-11ea-8cab-ed0832abb6d6.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/hanxiao/bert-as-service/issues/380#issuecomment-624160983, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJVBHQPNF2SKA336D33RQA5RXANCNFSM4HX4JNLQ .

-- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada

GaneshGS avatar May 05 '20 16:05 GaneshGS

for first two lines, you used 'pip', change it to '!pip' On Tue, May 5, 2020 at 12:32 PM SlimenBouras [email protected] wrote: Hi, Please follow the instructions. 1. uninstall 'tensorflow-gpu==1.12.0' and install 'tensorflow==1.15.0' 2. replace the !nohup command in your code 6 with the one below '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 out.file2>&1&' 3. type !ls and check the directory whether you can see unzipped 'uncased_L- 12_H-768_A-12' file in the directory or not. Now, you run it and get back to me. … <#m_8545851393258997135_> On Tue, May 5, 2020 at 11:22 AM SlimenBouras @.***> wrote: I have the same problem here with bc=BertClient() [image: image] https://user-images.githubusercontent.com/55366848/81083543-9fa46a80-8eec-11ea-944a-f2a07d58c7e1.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 (comment) <#380 (comment)>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJX4CBCFQXLSMAQJTG3RQAVNDANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada thank you for answering me, i applied your instructions, how long should i wait? [image: image] https://user-images.githubusercontent.com/55366848/81090892-535e2800-8ef6-11ea-8cab-ed0832abb6d6.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJVBHQPNF2SKA336D33RQA5RXANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada

thanks again can i have your mail ?

SlimenBouras avatar May 05 '20 17:05 SlimenBouras

for first two lines, you used 'pip', change it to '!pip' On Tue, May 5, 2020 at 12:32 PM SlimenBouras [email protected] wrote: Hi, Please follow the instructions. 1. uninstall 'tensorflow-gpu==1.12.0' and install 'tensorflow==1.15.0' 2. replace the !nohup command in your code 6 with the one below '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 out.file2>&1&' 3. type !ls and check the directory whether you can see unzipped 'uncased_L- 12_H-768_A-12' file in the directory or not. Now, you run it and get back to me. … <#m_8545851393258997135_> On Tue, May 5, 2020 at 11:22 AM SlimenBouras @.***> wrote: I have the same problem here with bc=BertClient() [image: image] https://user-images.githubusercontent.com/55366848/81083543-9fa46a80-8eec-11ea-944a-f2a07d58c7e1.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 (comment) <#380 (comment)>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJX4CBCFQXLSMAQJTG3RQAVNDANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada thank you for answering me, i applied your instructions, how long should i wait? [image: image] https://user-images.githubusercontent.com/55366848/81090892-535e2800-8ef6-11ea-8cab-ed0832abb6d6.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJVBHQPNF2SKA336D33RQA5RXANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada

thanks again can i have your mail ?

SlimenBouras, please find my e-mail: [email protected]

GaneshGS avatar May 05 '20 17:05 GaneshGS

SilmenBouras,

Is the problem solved? Is the BertClient() working???

On Tue, May 5, 2020 at 1:07 PM SlimenBouras [email protected] wrote:

for first two lines, you used 'pip', change it to '!pip' On Tue, May 5, 2020 at 12:32 PM SlimenBouras [email protected] wrote: … <#m_-5811634594184665497_> Hi, Please follow the instructions. 1. uninstall 'tensorflow-gpu==1.12.0' and install 'tensorflow==1.15.0' 2. replace the !nohup command in your code 6 with the one below '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 out.file2>&1&' 3. type !ls and check the directory whether you can see unzipped 'uncased_L- 12_H-768_A-12' file in the directory or not. Now, you run it and get back to me. … <#m_8545851393258997135_> On Tue, May 5, 2020 at 11:22 AM SlimenBouras @.***> wrote: I have the same problem here with bc=BertClient() [image: image] https://user-images.githubusercontent.com/55366848/81083543-9fa46a80-8eec-11ea-944a-f2a07d58c7e1.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 https://github.com/hanxiao/bert-as-service/issues/380 (comment) <#380 (comment) https://github.com/hanxiao/bert-as-service/issues/380#issuecomment-624120929>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJX4CBCFQXLSMAQJTG3RQAVNDANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada thank you for answering me, i applied your instructions, how long should i wait? [image: image] https://user-images.githubusercontent.com/55366848/81090892-535e2800-8ef6-11ea-8cab-ed0832abb6d6.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 (comment) https://github.com/hanxiao/bert-as-service/issues/380#issuecomment-624160983>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJVBHQPNF2SKA336D33RQA5RXANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada

thanks again can i have your mail ?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/hanxiao/bert-as-service/issues/380#issuecomment-624182650, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJVJXWGI443LIOIAPWLRQBBULANCNFSM4HX4JNLQ .

-- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada

GaneshGS avatar May 05 '20 17:05 GaneshGS

SilmenBouras, Is the problem solved? Is the BertClient() working??? On Tue, May 5, 2020 at 1:07 PM SlimenBouras [email protected] wrote: for first two lines, you used 'pip', change it to '!pip' On Tue, May 5, 2020 at 12:32 PM SlimenBouras @.*** wrote: … <#m_-5811634594184665497_> Hi, Please follow the instructions. 1. uninstall 'tensorflow-gpu==1.12.0' and install 'tensorflow==1.15.0' 2. replace the !nohup command in your code 6 with the one below '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 out.file2>&1&' 3. type !ls and check the directory whether you can see unzipped 'uncased_L- 12_H-768_A-12' file in the directory or not. Now, you run it and get back to me. … <#m_8545851393258997135_> On Tue, May 5, 2020 at 11:22 AM SlimenBouras @.***> wrote: I have the same problem here with bc=BertClient() [image: image] https://user-images.githubusercontent.com/55366848/81083543-9fa46a80-8eec-11ea-944a-f2a07d58c7e1.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 <#380> (comment) <#380 (comment) <#380 (comment)>>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJX4CBCFQXLSMAQJTG3RQAVNDANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada thank you for answering me, i applied your instructions, how long should i wait? [image: image] https://user-images.githubusercontent.com/55366848/81090892-535e2800-8ef6-11ea-8cab-ed0832abb6d6.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 (comment) <#380 (comment)>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJVBHQPNF2SKA336D33RQA5RXANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada thanks again can i have your mail ? — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#380 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHEJQJVJXWGI443LIOIAPWLRQBBULANCNFSM4HX4JNLQ . -- Dr. Ganesh Sinisetty NLP Developer and Research Scientist Prologue AI Montreal, Quebec, Canada

Still the same problem , i sent you an e-mail

SlimenBouras avatar May 05 '20 17:05 SlimenBouras

Hello, did the problem solved eventually. I have followed the steps above. Client is still not starting. Any help is appreciated.

prateek0489 avatar May 10 '20 22:05 prateek0489

same issue here bert client is not starting, the execution kept running but no results

Samery00 avatar May 11 '20 04:05 Samery00

iknoorjobs, please look for the spaces and directory address in '!nohup bert-serving-start -model_dir=./uncased_L-12_H-768_A-12 > out.file 2>&1 &'

It was initially not running for me but it started working for me after I fixed the issues with spaces and directory path.

Hi @GaneshGS @DHOFM It is still not working and I am not sure what you are trying to say. I have checked the directories and all seem to be okay. Why don't you share your colab notebook if it's working for you? It'll be helpful to all. Thanks.

iknoorjobs avatar May 14 '20 13:05 iknoorjobs

@iknoorjobs i have a lot of other stuff in that notebook so I can't share. I would need to create a clean new one. But first let me try to help you. So a few questions:

  • Does it work to you without nohup? You can't interact then in the notebook but you see the log messages

  • Are you running tensorflow 1.x?

  • Are you on a GPU instance?

Kind regads,

Dirk

DHOFM avatar May 14 '20 16:05 DHOFM

Still the same problem for me !

SlimenBouras avatar May 14 '20 22:05 SlimenBouras

@iknoorjobs i have a lot of other stuff in that notebook so I can't share. I would need to create a clean new one. But first let me try to help you. So a few questions:

  • Does it work to you without nohup? You can't interact then in the notebook but you see the log messages
  • Are you running tensorflow 1.x?
  • Are you on a GPU instance?

Kind regads,

Dirk

@DHOFM Finally got the problem. GPU instance. XD Thanks man. :)

Regards, Iknoor

iknoorjobs avatar May 14 '20 23:05 iknoorjobs

Still the same problem for me !

@SlimenBouras Make sure to change run type to GPU before running any command.

Regards, Iknoor

iknoorjobs avatar May 14 '20 23:05 iknoorjobs

Still the same problem for me !

@SlimenBouras Make sure to change run type to GPU before running any command.

Regards, Iknoor

yeah i change it look image

SlimenBouras avatar May 14 '20 23:05 SlimenBouras

Thank you everyone. I was finally able to fix this issue. I used this GPU and this command to change my tensorflow version. %tensorflow_version 1.x

prateek0489 avatar May 15 '20 21:05 prateek0489