how to run stanford corenlp server on google colab

2020-06-30 00:14发布

问题:

I want to use stanford corenlp for obtaining dependency parser of sentences. In order to using stanford corenlp in python, we need to do the below steps:

  1. Install java
  2. Download stanford-corenlp-full-2018-10-05 and extract it.
  3. Change directory to stanford-corenlp-full-2018-10-05 folder with "cd" command.
  4. Run this command in the current directory:

    "java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 75000" .

After that, stanford-corenlp server will run at 'http://localhost:9000' . Finally we can call CoreNLPDependencyParser() in python script like this:

dependency_parser = CoreNLPDependencyParser(url='http://localhost:9000')

Now , i want to run stanford-corenlp server on google colab. I uplaoded stanford-corenlp-full-2018-10-05 folder to google drive and mount google drive on google colab. Then I installed java with below function :

import os       
def install_java():
  !apt-get install -y openjdk-8-jdk-headless -qq > /dev/null     
  os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"     
  !java -version    
install_java()

Now, i don't know how run aforementioned java command and gain localhost address.

Is there any way to do that?

回答1:

To connect from a remote machine to a server running on Google Colab, you need to use ngrok.

Assuming your server is running on an existing notebook, create a new notebook and run the following code (which I found from here):

import os
import subprocess
import json
import time
import requests


def _get_ngrok_tunnel():
    while True:
        try:
            tunnels_json = requests.get("http://localhost:4040/api/tunnels").content
            public_url = json.loads(tunnels_json)['tunnels'][0]['public_url']
            return public_url
        except Exception:
            print("Can't get public url, retrying...")
            time.sleep(2)


def _warmup_ngrok_tunnel(public_url):
    while requests.get(public_url).status_code >= 500:
        print("Tunnel is not ready, retrying...")
        time.sleep(2)


def expose_port_on_colab(port):
    os.system("apt-get install net-tools")
    # check that port is open
    while not (":{} ".format(port) in str(subprocess.check_output("netstat -vatn", shell=True))):
        print("Port {} is closed, retrying...".format(port))
        time.sleep(2)

    # run ngrok
    os.system("wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip")
    os.system("unzip ngrok-stable-linux-amd64.zip")
    os.system("./ngrok http {0} &".format(port))
    public_url = _get_ngrok_tunnel()
    _warmup_ngrok_tunnel(public_url)

    print("Open {0} to access your {1} port".format(public_url, port))

Then invoke expose_port_on_colab function with the port that the server is listening to, this function will give you a URL that you can use to connect to the server