【问题标题】:how to run stanford corenlp server on google colab?如何在 google colab 上运行 stanford corenlp 服务器?
【发布时间】:2019-09-23 20:15:53
【问题描述】:

我想使用 stanford corenlp 来获取句子的依赖解析器。为了在 python 中使用 stanford corenlp,我们需要执行以下步骤:

  1. 安装java
  2. 下载 stanford-corenlp-full-2018-10-05 并解压。
  3. 使用“cd”命令将目录更改为 stanford-corenlp-full-2018-10-05 文件夹。
  4. 在当前目录中运行此命令:

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

之后,stanford-corenlp 服务器将在 'http://localhost:9000' 运行。 最后我们可以像这样在 python 脚本中调用 CoreNLPDependencyParser():

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

现在,我想在 google colab 上运行 stanford-corenlp 服务器。我将 stanford-corenlp-full-2018-10-05 文件夹上传到谷歌驱动器并在谷歌 colab 上安装谷歌驱动器。然后我安装了具有以下功能的java:

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()

现在,我不知道如何运行上述 java 命令并获得 localhost 地址。

有什么办法吗?

【问题讨论】:

    标签: java stanford-nlp google-colaboratory


    【解决方案1】:

    要从远程机器连接到在 Google Colab 上运行的服务器,您需要使用 ngrok

    假设您的服务器在现有笔记本上运行,创建一个新笔记本并运行以下代码(我从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))
    
    

    然后用服务器正在监听的端口调用expose_port_on_colab函数,这个函数会给你一个URL,你可以用来连接到服务器

    【讨论】:

    • @SMASH1986 非常感谢您的帮助。
    • 我不确定这是否会起作用,因为 colab 现在倾向于为每个笔记本提供单独的实例/环境。
    • 这段代码为我持续运行,并没有打开端口。
    猜你喜欢
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2020-09-26
    相关资源
    最近更新 更多