This is an automated email from the ASF dual-hosted git repository.

mawiesne pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opennlp-sandbox.git


The following commit(s) were added to refs/heads/main by this push:
     new 0460b3b5 OPENNLP-1830: Add Python gRPC sentence detection example 
(#465)
0460b3b5 is described below

commit 0460b3b5d1a48259e3822f0f2c14943d38d95055
Author: (jobin sabu) <[email protected]>
AuthorDate: Tue Jun 2 14:38:21 2026 +0530

    OPENNLP-1830: Add Python gRPC sentence detection example (#465)
---
 .gitignore                                         |  2 +
 opennlp-grpc/README.md                             | 17 ++++++
 opennlp-grpc/examples/README.md                    | 29 +++++++---
 opennlp-grpc/examples/python-client/README.md      | 53 ++++++++++++++++--
 opennlp-grpc/examples/python-client/pyproject.toml | 27 +++++++++
 .../examples/python-client/sentdetect_example.py   | 65 ++++++++++++++++++++++
 6 files changed, 181 insertions(+), 12 deletions(-)

diff --git a/.gitignore b/.gitignore
index 70f2c082..91d90c9a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,5 +12,7 @@ nb-configuration.xml
 vocab.txt
 extlib
 *.jar
+__pycache__/
+*.pyc
 
 .python-version
\ No newline at end of file
diff --git a/opennlp-grpc/README.md b/opennlp-grpc/README.md
index 3eae2bd4..1ae966e1 100644
--- a/opennlp-grpc/README.md
+++ b/opennlp-grpc/README.md
@@ -1,3 +1,20 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
 # OpenNLP gRPC - Proof of Concept
 
 This project demonstrates a proof of concept for creating a backend powered by 
Apache OpenNLP using gRPC. It comprises of three main modules : 
diff --git a/opennlp-grpc/examples/README.md b/opennlp-grpc/examples/README.md
index a969256c..6996df1c 100644
--- a/opennlp-grpc/examples/README.md
+++ b/opennlp-grpc/examples/README.md
@@ -1,20 +1,33 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
 # Apache OpenNLP gRPC - Examples
 
 This repository contains examples for the Apache OpenNLP gRPC project.
 
-For other languages, you need to generate the code stubs yourself, as shown 
here for Python
+For other languages, generate the code stubs yourself, as shown here for 
Python:
 
-```
-python3 -m pip install grpcio-tools
-mkdir python
+```bash
 python3 -m grpc_tools.protoc -I. --python_out=python --grpc_python_out=python 
opennlp.proto
 ```
 
-# Documentation generation
+## Documentation generation
 
 ```powershell
-docker run --rm -v ${PWD}:/out -v ${PWD}:/protos pseudomuto/protoc-gen-doc 
--doc_opt=markdown,opennlp.md 
+docker run --rm -v ${PWD}:/out -v ${PWD}:/protos pseudomuto/protoc-gen-doc 
--doc_opt=markdown,opennlp.md
 ```
-
 The current version of the documentation can be found [here](opennlp)
-
diff --git a/opennlp-grpc/examples/python-client/README.md 
b/opennlp-grpc/examples/python-client/README.md
index 0c5a5ff6..f7ffd577 100644
--- a/opennlp-grpc/examples/python-client/README.md
+++ b/opennlp-grpc/examples/python-client/README.md
@@ -1,9 +1,54 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
 # Apache OpenNLP gRPC - Python Client
 
-This client was generated using the gRPC tools and the schema provided in the 
opennlp-grpc-api module.
+This client was generated using the gRPC tools and the schema provided in
+the `opennlp-grpc-api` module.
+
+## Setup
+
+Install dependencies:
+
+```bash
+uv sync
+```
+
+Fallback without `uv`:
+
+```bash
+python3 -m pip install grpcio grpcio-tools
+```
+
+## Run examples
+
+Run POS tagging:
+
+```bash
+uv run python main.py
+```
+
+Run sentence detection:
 
+```bash
+uv run python sentdetect_example.py
 ```
-python3 -m pip install grpcio-tools
-mkdir python
-python3 -m grpc_tools.protoc -I. --python_out=python --grpc_python_out=python 
opennlp.proto
+
+## Regenerating gRPC stubs
+
+```bash
+uv run python -m grpc_tools.protoc -I../../opennlp-grpc-api --python_out=. 
--grpc_python_out=. ../../opennlp-grpc-api/opennlp.proto
 ```
\ No newline at end of file
diff --git a/opennlp-grpc/examples/python-client/pyproject.toml 
b/opennlp-grpc/examples/python-client/pyproject.toml
new file mode 100644
index 00000000..ee99ea8c
--- /dev/null
+++ b/opennlp-grpc/examples/python-client/pyproject.toml
@@ -0,0 +1,27 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+[project]
+name = "opennlp-python-examples"
+version = "0.1.0"
+requires-python = ">=3.10"
+
+dependencies = [
+  "grpcio>=1.60.0",
+  "grpcio-tools>=1.60.0"
+]
+description = "Apache OpenNLP Python gRPC examples"
+license = "Apache-2.0"
+
diff --git a/opennlp-grpc/examples/python-client/sentdetect_example.py 
b/opennlp-grpc/examples/python-client/sentdetect_example.py
new file mode 100644
index 00000000..b720ce80
--- /dev/null
+++ b/opennlp-grpc/examples/python-client/sentdetect_example.py
@@ -0,0 +1,65 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import grpc
+import opennlp_pb2
+import opennlp_pb2_grpc
+
+
+def run():
+    with grpc.insecure_channel("localhost:7071") as channel:
+        stub = opennlp_pb2_grpc.SentenceDetectorServiceStub(channel)
+
+        print("Connecting to OpenNLP gRPC server...")
+
+        try:
+            response = stub.GetAvailableModels(
+                opennlp_pb2.Empty(),
+                timeout=5
+            )
+            models = list(response.models)
+        except grpc.RpcError as e:
+            print(f"Server error: {e.code()} - {e.details()}")
+            return
+
+        if not models:
+            print("No models available on server.")
+            return
+
+        # Pick first registered sentence detector model
+        model = models[0]
+        print(f"Using model: {model.name} ({model.hash})")
+
+        text = "The Apache OpenNLP project is great. It is now connected to 
Python!"
+
+        request = opennlp_pb2.SentDetectRequest(
+            sentence=text,
+            model_hash=model.hash
+        )
+
+        try:
+            result = stub.sentDetect(request, timeout=5)
+        except grpc.RpcError as e:
+            print(f"Sentence detection failed: {e.code()} - {e.details()}")
+            return
+
+        print("\nSentence Detection Result:")
+        for i, sentence in enumerate(result.values, 1):
+            print(f"{i}. {sentence}")
+
+
+if __name__ == "__main__":
+    run()
\ No newline at end of file

Reply via email to