Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use StanfordCoreNLPServer to load scenegraph library #1346

Closed
chaojiewang94 opened this issue Mar 9, 2023 · 27 comments
Closed

How to use StanfordCoreNLPServer to load scenegraph library #1346

chaojiewang94 opened this issue Mar 9, 2023 · 27 comments

Comments

@chaojiewang94
Copy link

I wanna to construct a StanfordCoreNLPServer, which has preloaded scenegraph, how to achice this?

I have tried the command "
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer
-preload scenegraph
-status_port 9000 -port 9000 -timeout 15000 &
"
but it seems to be failed

@chaojiewang94 chaojiewang94 changed the title How use StanfordCoreNLPServer to load scenegraph library How to use StanfordCoreNLPServer to load scenegraph library Mar 9, 2023
@AngledLuffa
Copy link
Contributor

It's the same problem as on the stanza side. The code to run scenegraph in a server simply doesn't exist.

I'm sure you've found this already, but there are instructions on how to run it here:

https://nlp.stanford.edu/software/scenegraph-parser.shtml

That's the only thing that exists currently. We could theoretically include it in a server, but until you came along, there was never any request for that. What are you looking for that the CLI and the API don't provide?

@chaojiewang94
Copy link
Author

chaojiewang94 commented Mar 9, 2023

I wonder if you can include the following two APIs in NLPserver
``
import edu.stanford.nlp.scenegraph.RuleBasedParser;
import edu.stanford.nlp.scenegraph.SceneGraph;
''
so that I can preload them as the same as other APIs like tokenize,ssplit,pos,lemma,ner in https://github.com/nltk/nltk/wiki/Stanford-CoreNLP-API-in-NLTK

then I use the serve to transfer a sentence into scene graph in Python, just like this

``
from nltk.parse import CoreNLPParser

sentence = "A brown fox chases a white rabbit."

parser = CoreNLPParser(url='http://localhost:9000')
sg = parser.scenegraph_parse(sentence)

print(sg.toReadableString())
''

It will be helpful for us to cite your work in https://nlp.stanford.edu/software/scenegraph-parser.shtml
thanks

@AngledLuffa

@chaojiewang94
Copy link
Author

Sorry for misunderstaing, I just wanna to parse a sentence into a scene graph with Python @AngledLuffa

@AngledLuffa
Copy link
Contributor

can do, but it will be at least a week until we make that available

@chaojiewang94
Copy link
Author

chaojiewang94 commented Mar 10, 2023

thanks for your help, looking forward to your reply @AngledLuffa

@AngledLuffa
Copy link
Contributor

Sorry for the long delay.  Spring quarter was quite busy (we did submit a couple drafts out of work we had done, so that was good).  I have time to pick this up again, and after looking over scenegraph, I think I can put something together.
There's a text output format and a json output format which would both be easy to make available in python.  There are a couple different ways of doing it:

  • standalone java program with a corresponding python interface. easier to run, but can only be run locally
  • add it to server itself.  a little more complicated, allows you to run the server in a separate process or on a separate machine

Which of these would be more useful, assuming you even still need this upgrade?

AngledLuffa added a commit that referenced this issue Jul 10, 2023
Processes requests using the scenegraph package:
https://nlp.stanford.edu/software/scenegraph-parser.shtml

Output is in either the text or json format from the scenegraph package.

Requested in #1346
AngledLuffa added a commit to stanfordnlp/stanza that referenced this issue Jul 10, 2023
@chaojiewang94
Copy link
Author

Thanks for your reply, I wonder if you can include the scene graph parser into stanza? so that I can directly use it after intalling stanza?

@AngledLuffa
Copy link
Contributor

One way or another, it'll require having the Java implementation of SceneGraph, as there really isn't any impetus to update the project for python & deep learning in the group

@chaojiewang94
Copy link
Author

I wonder if you can include the following two APIs in NLPserver `` import edu.stanford.nlp.scenegraph.RuleBasedParser; import edu.stanford.nlp.scenegraph.SceneGraph; '' so that I can preload them as the same as other APIs like tokenize,ssplit,pos,lemma,ner in https://github.com/nltk/nltk/wiki/Stanford-CoreNLP-API-in-NLTK

then I use the serve to transfer a sentence into scene graph in Python, just like this

`` from nltk.parse import CoreNLPParser

sentence = "A brown fox chases a white rabbit."

parser = CoreNLPParser(url='http://localhost:9000') sg = parser.scenegraph_parse(sentence)

print(sg.toReadableString()) ''

It will be helpful for us to cite your work in https://nlp.stanford.edu/software/scenegraph-parser.shtml thanks

@AngledLuffa

can u implement like this, maybe the second way?

@AngledLuffa
Copy link
Contributor

You can see what I did in the two changes listed above. If you have a CoreNLP server created, or if you create one using the Stanza API, you can then call client.scenegraph(text) and it will get a result.

It occurs to me that I don't actually know if the SceneGraph parser is threadsafe. 🤷

@AngledLuffa
Copy link
Contributor

It might be a little time before the next official release, but I can make some sort of temporary package available if you'd use it in the short term

@chaojiewang94
Copy link
Author

Thanks, I will have a try in the following week

AngledLuffa added a commit that referenced this issue Jul 15, 2023
Processes requests using the scenegraph package:
https://nlp.stanford.edu/software/scenegraph-parser.shtml

Output is in either the text or json format from the scenegraph package.

Requested in #1346

Leave a note about not having tested the scenegraph parser for thread safety
AngledLuffa added a commit to stanfordnlp/stanza that referenced this issue Jul 18, 2023
@AngledLuffa
Copy link
Contributor

I haven't made an official CoreNLP release with the updates yet, but you can use this for the server changes:

https://nlp.stanford.edu/software/stanford-corenlp-4.5.4.zip

For the Stanza interface, you'll have to install the dev branch, such as described here

https://stackoverflow.com/questions/20101834/pip-install-from-git-repo-branch

AngledLuffa added a commit that referenced this issue Sep 6, 2023
Processes requests using the scenegraph package:
https://nlp.stanford.edu/software/scenegraph-parser.shtml

Output is in either the text or json format from the scenegraph package.

Requested in #1346

Leave a note about not having tested the scenegraph parser for thread safety
@chaojiewang94
Copy link
Author

You can see what I did in the two changes listed above. If you have a CoreNLP server created, or if you create one using the Stanza API, you can then call client.scenegraph(text) and it will get a result.

It occurs to me that I don't actually know if the SceneGraph parser is threadsafe. 🤷

sorry, I still have trouble in building client with Stanza API

set up the client

with CoreNLPClient(annotators=['tokenize','ssplit','pos','lemma','ner','parse','depparse','coref'], timeout=60000, memory='16G') as client:
# submit the request to the server
client.scenegraph(text)

did I miss something?

@chaojiewang94
Copy link
Author

chaojiewang94 commented Sep 11, 2023

from stanza.server import CoreNLPClient

print('---')
print('input text')
print('')

text = "Chris Manning is a nice person. Chris wrote a simple sentence. He also gives oranges to people."

print(text)

print('---')
print('starting up Java Stanford CoreNLP Server...')

with CoreNLPClient(timeout=60000, memory='16G') as client:
# submit the request to the server
client.scenegraph(text)

@chaojiewang94
Copy link
Author

bug here:


input text
Chris Manning is a nice person. Chris wrote a simple sentence. He also gives oranges to people.

starting up Java Stanford CoreNLP Server...
2023-09-11 14:58:40 INFO: Writing properties to tmp file: corenlp_server-6f8436e1da2e4dc6.props
2023-09-11 14:58:40 INFO: Starting server with command: java -Xmx16G -cp /home/chaojiewang/stanza_corenlp/* edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 60000 -threads 5 -maxCharLength 100000 -quiet False -serverProperties corenlp_server-6f8436e1da2e4dc6.props -preload -outputFormat serialized
Traceback (most recent call last):
File "/home/chaojiewang/anaconda3/envs/diffusion/lib/python3.8/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/home/chaojiewang/anaconda3/envs/diffusion/lib/python3.8/site-packages/urllib3/util/connection.py", line 95, in create_connection
raise err
File "/home/chaojiewang/anaconda3/envs/diffusion/lib/python3.8/site-packages/urllib3/util/connection.py", line 85, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:

@chaojiewang94
Copy link
Author

@AngledLuffa can u provide your demo code to use client.secengraph, thanks

@AngledLuffa
Copy link
Contributor

what failure are you getting?

this works for me with the latest CoreNLP (4.5.5) and the latest stanza (1.5.1)

stanfordnlp/stanza@b63fdfe

@AngledLuffa
Copy link
Contributor

getting a connection error makes me wonder if there's a permissions problem binding that port

@chaojiewang94
Copy link
Author

chaojiewang94 commented Sep 13, 2023

getting a connection error makes me wonder if there's a permissions problem binding that port

@AngledLuffa

but the demo for CoreNLP works for me

from stanza.server import CoreNLPClient

print('---')
print('input text')
print('')

text = "Chris Manning is a nice person. Chris wrote a simple sentence. He also gives oranges to people."

print(text)

print('---')
print('starting up Java Stanford CoreNLP Server...')

with CoreNLPClient(annotators=['tokenize','ssplit','pos','lemma','ner','parse','depparse','coref'], timeout=60000, memory='16G') as client:
# submit the request to the server
ann = client.annotate(text)

@AngledLuffa
Copy link
Contributor

I have found that it is behaving a bit different between having the server already started and starting the server inside a script. Let me track that down

@chaojiewang94
Copy link
Author

chaojiewang94 commented Sep 13, 2023

this works for me after debugging, but I donot know why @AngledLuffa

from stanza.server import CoreNLPClient

text = "Chris Manning is a nice person. Chris wrote a simple sentence. He also gives oranges to people."

with CoreNLPClient(annotators=['tokenize'], timeout=60000, memory='16G') as client:

ann = client.annotate(text)

result = client.scenegraph("Jennifer's antennae are on her head.")
print(result)

@chaojiewang94
Copy link
Author

seems there are some procedures to intial the client in client.annotate()

@AngledLuffa
Copy link
Contributor

ok, figured it out, there needs to be a

    client.ensure_alive()

It was supposed to be in the client itself, so that's on me. I'll fix it on the dev branch. For now you can just add this line, so the code to start & then run it looks like this

with CoreNLPClient(preload=False) as client:
    client.ensure_alive()
    result = client.scenegraph("Jennifer's antennae are on her head.")
    print(result)

@chaojiewang94
Copy link
Author

@AngledLuffa great! works for me now, thanks for your help!

@AngledLuffa
Copy link
Contributor

👍

i will add that to the client so you won't need to do it for future versions

@chaojiewang94
Copy link
Author

chaojiewang94 commented Sep 13, 2023

@AngledLuffa got it!

AngledLuffa added a commit to stanfordnlp/stanza that referenced this issue Sep 13, 2023
…ise, a request attempt might happen while the server is still loading and therefore fail. See discussion at stanfordnlp/CoreNLP#1346
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants