如何在 google colab 上 运行 stanford corenlp 服务器?
how to run stanford corenlp server on google colab?
我想使用 stanford corenlp 获取句子的依赖解析器。为了在 python 中使用 stanford corenlp,我们需要执行以下步骤:
- 安装java
- 下载 stanford-corenlp-full-2018-10-05 并解压。
- 使用 "cd" 命令将目录更改为 stanford-corenlp-full-2018-10-05 文件夹。
- 运行当前目录下这条命令:
"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 文件夹上传到 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 命令并获得本地主机地址。
有什么办法吗?
要在 Google Colab 上从远程计算机连接到服务器 运行ning,您需要使用 ngrok。
假设您的服务器 运行ning 在现有笔记本上,请创建一个新笔记本并 运行 以下代码(我从 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,你可以用它来连接到服务器
我想使用 stanford corenlp 获取句子的依赖解析器。为了在 python 中使用 stanford corenlp,我们需要执行以下步骤:
- 安装java
- 下载 stanford-corenlp-full-2018-10-05 并解压。
- 使用 "cd" 命令将目录更改为 stanford-corenlp-full-2018-10-05 文件夹。
- 运行当前目录下这条命令:
"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 文件夹上传到 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 命令并获得本地主机地址。
有什么办法吗?
要在 Google Colab 上从远程计算机连接到服务器 运行ning,您需要使用 ngrok。
假设您的服务器 运行ning 在现有笔记本上,请创建一个新笔记本并 运行 以下代码(我从 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,你可以用它来连接到服务器