簡體   English   中英

如何在谷歌colab上運行stanford corenlp服務器?

[英]how to run stanford corenlp server on google colab?

我想使用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')

現在,我想在谷歌colab上運行stanford-corenlp服務器。 我上傳stanford-corenlp-full-2018-10-05文件夾到google驅動器並在google colab上安裝google驅動器。 然后我用以下函數安裝了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地址。

有沒有辦法做到這一點?

要從遠程計算機連接到在Google Colab上運行的服務器,您需要使用ngrok

假設您的服務器在現有筆記本上運行,請創建一個新筆記本並運行以下代碼(我從這里找到):

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

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM