From 0f177c418907e8856b8a9b5a28922cf7fb93ff5e Mon Sep 17 00:00:00 2001 From: Olya Gupalo Date: Thu, 8 Feb 2024 17:18:55 +0100 Subject: [PATCH 1/9] Review and update Polyglot Chat README --- polyglot-chat-app/README.md | 10 +++------- polyglot-chat-app/build.gradle | 5 +---- polyglot-chat-app/settings.gradle | 2 -- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/polyglot-chat-app/README.md b/polyglot-chat-app/README.md index a322464bc..241e1fdac 100644 --- a/polyglot-chat-app/README.md +++ b/polyglot-chat-app/README.md @@ -3,13 +3,9 @@ This example demonstrates how to integrate Python on GraalVM with a Micronaut application. The application uses the Gradle build tool. -### Prerequisites -- [Native Image](https://www.graalvm.org/latest/reference-manual/native-image/) -- [Python support](https://www.graalvm.org/latest/reference-manual/python/) - ## Preparation -1. Download the latest GraalPy as described on [https://www.graalvm.org/python/](https://www.graalvm.org/python/). For example on Linux: +1. Download and install the latest GraalPy as described in the [Getting Started guide](https://www.graalvm.org/latest/reference-manual/python/#installing-graalpy). For example on Linux: ```bash wget https://github.com/oracle/graalpython/releases/download/graal-23.1.1/graalpy-23.1.1-linux-amd64.tar.gz tar xzf graalpy-23.1.1-linux-amd64.tar.gz @@ -24,14 +20,14 @@ The application uses the Gradle build tool. 3. Optional: Download and install GraalVM JDK for Java 21 or later to run Python with runtime compilation and to build a native image. The demo will work on any OpenJDK distribution, but will be much faster on GraalVM JDK. -## Building and Running the application: +## Building and Running the Application: 1. Build application with Gradle: ```bash ./gradlew run ``` -2. Navigate to http://localhost:12345/#/chat/bob +2. Navigate to [http://localhost:12345/#/chat/bob](http://localhost:12345/#/chat/bob). You can connect from multiple browsers and chat via websockets. The Python code will load a language model in the background. diff --git a/polyglot-chat-app/build.gradle b/polyglot-chat-app/build.gradle index 13ee35663..de5d031a6 100644 --- a/polyglot-chat-app/build.gradle +++ b/polyglot-chat-app/build.gradle @@ -65,7 +65,4 @@ micronaut { deduceEnvironment = true optimizeNetty = true } -} - - - +} \ No newline at end of file diff --git a/polyglot-chat-app/settings.gradle b/polyglot-chat-app/settings.gradle index 58a866b02..3c62226ef 100644 --- a/polyglot-chat-app/settings.gradle +++ b/polyglot-chat-app/settings.gradle @@ -1,3 +1 @@ - rootProject.name="websocket.chat" - From 8122afbcc3d30c5aca7ee2ee79c53b9e2ed4d13d Mon Sep 17 00:00:00 2001 From: Olya Gupalo Date: Fri, 9 Feb 2024 12:29:43 +0100 Subject: [PATCH 2/9] Update README.md --- polyglot-chat-app/README.md | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/polyglot-chat-app/README.md b/polyglot-chat-app/README.md index 241e1fdac..2794352eb 100644 --- a/polyglot-chat-app/README.md +++ b/polyglot-chat-app/README.md @@ -6,26 +6,31 @@ The application uses the Gradle build tool. ## Preparation 1. Download and install the latest GraalPy as described in the [Getting Started guide](https://www.graalvm.org/latest/reference-manual/python/#installing-graalpy). For example on Linux: - ```bash - wget https://github.com/oracle/graalpython/releases/download/graal-23.1.1/graalpy-23.1.1-linux-amd64.tar.gz - tar xzf graalpy-23.1.1-linux-amd64.tar.gz - ``` + ```bash + wget https://github.com/oracle/graalpython/releases/download/graal-23.1.1/graalpy-23.1.1-linux-amd64.tar.gz + tar xzf graalpy-23.1.1-linux-amd64.tar.gz + ``` 2. Install the required packages for this demo into the _resources_ directory: ```bash graalpy-23.1.1-linux-amd64/bin/graalpy -m venv src/main/resources/venv + ``` + ```bash src/main/resources/venv/bin/graalpy -m pip install nltk ``` + ```bash + src/main/resources/venv/bin/graalpy -c "import nltk; nltk.download('vader_lexicon')" + ``` -3. Optional: Download and install GraalVM JDK for Java 21 or later to run Python with runtime compilation and to build a native image. - The demo will work on any OpenJDK distribution, but will be much faster on GraalVM JDK. +3. (Optional) Download and install GraalVM JDK for Java 21 or later to run Python with runtime compilation and to build a native image. +The demo will work on any OpenJDK distribution, but will be much faster on GraalVM JDK. ## Building and Running the Application: 1. Build application with Gradle: - ```bash - ./gradlew run - ``` + ```bash + ./gradlew run + ``` 2. Navigate to [http://localhost:12345/#/chat/bob](http://localhost:12345/#/chat/bob). From 21a290b4dc5d80c40f624840b159d15704d56abd Mon Sep 17 00:00:00 2001 From: Olga Gupalo <31922163+olyagpl@users.noreply.github.com> Date: Fri, 9 Feb 2024 12:40:06 +0100 Subject: [PATCH 3/9] Update README.md Co-authored-by: Bernard Horan --- polyglot-chat-app/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polyglot-chat-app/README.md b/polyglot-chat-app/README.md index 2794352eb..31e0bbad2 100644 --- a/polyglot-chat-app/README.md +++ b/polyglot-chat-app/README.md @@ -11,7 +11,7 @@ The application uses the Gradle build tool. tar xzf graalpy-23.1.1-linux-amd64.tar.gz ``` -2. Install the required packages for this demo into the _resources_ directory: +2. Create a virtual environment for this demo in the _resources_ directory, and install the required packages: ```bash graalpy-23.1.1-linux-amd64/bin/graalpy -m venv src/main/resources/venv ``` From b2d7f86d4208d4fe764acf503cc9b75e7bd23865 Mon Sep 17 00:00:00 2001 From: Olya Gupalo Date: Mon, 12 Feb 2024 16:16:18 +0100 Subject: [PATCH 4/9] Move polyglo-chat-app to GraalPy demos list in top-level README --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a0d4772ac..f3a8ddc4d 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,10 @@ cd graalvm-demos graalpy-notebook-example
graalpy-notebook-example Demonstrates how to embed Python in a Java application running on GraalVM
Technologies: GraalPy + + polyglot-chat-app
polyglot-chat-app + Demonstrates how to integrate Python in a Micronaut Java application and build using Gradle
Technologies: Java, GraalPy, Micronaut + @@ -265,10 +269,6 @@ cd graalvm-demos - - polyglot-chat-app
polyglot-chat-app - Demonstrates how to build a polyglot chat application by embedding Python and R into the Java host language
Technologies: Java, GraalPy, FastR, Micronaut - polyglot-debug
polyglot-debug Demonstrates how to debug a polyglot Java and JavaScript application using GraalVM Tools for Java in VS Code
Technologies: Java, JavaScript, Maven, GraalVM Extension Pack From cc37f090168a1bf1219d885b834f78218ec59461 Mon Sep 17 00:00:00 2001 From: Olya Gupalo Date: Mon, 12 Feb 2024 16:44:39 +0100 Subject: [PATCH 5/9] Add src/main/resources/venv/pyvenv.cfg to gitignore --- polyglot-chat-app/.gitignore | 1 + polyglot-chat-app/README.md | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/polyglot-chat-app/.gitignore b/polyglot-chat-app/.gitignore index c5f0be605..9e462a00c 100644 --- a/polyglot-chat-app/.gitignore +++ b/polyglot-chat-app/.gitignore @@ -12,3 +12,4 @@ out/ .settings .classpath .factorypath +src/main/resources/venv/pyvenv.cfg \ No newline at end of file diff --git a/polyglot-chat-app/README.md b/polyglot-chat-app/README.md index 31e0bbad2..01860425a 100644 --- a/polyglot-chat-app/README.md +++ b/polyglot-chat-app/README.md @@ -3,15 +3,23 @@ This example demonstrates how to integrate Python on GraalVM with a Micronaut application. The application uses the Gradle build tool. -## Preparation +## Preparation` -1. Download and install the latest GraalPy as described in the [Getting Started guide](https://www.graalvm.org/latest/reference-manual/python/#installing-graalpy). For example on Linux: +1. Download or clone GraalVM demos repository and navigate into the `polyglot-chat-app` directory: + ```bash + git clone https://github.com/graalvm/graalvm-demos + ``` + ```bash + cd graalvm-demos/polyglot-chat-app + ``` + +2. Download and install the latest GraalPy as described in the [Getting Started guide](https://www.graalvm.org/latest/reference-manual/python/#installing-graalpy). For example on Linux: ```bash wget https://github.com/oracle/graalpython/releases/download/graal-23.1.1/graalpy-23.1.1-linux-amd64.tar.gz tar xzf graalpy-23.1.1-linux-amd64.tar.gz - ``` + `` -2. Create a virtual environment for this demo in the _resources_ directory, and install the required packages: +3. Create a virtual environment for this demo in the _resources_ directory, and install the required packages: ```bash graalpy-23.1.1-linux-amd64/bin/graalpy -m venv src/main/resources/venv ``` @@ -22,7 +30,7 @@ The application uses the Gradle build tool. src/main/resources/venv/bin/graalpy -c "import nltk; nltk.download('vader_lexicon')" ``` -3. (Optional) Download and install GraalVM JDK for Java 21 or later to run Python with runtime compilation and to build a native image. +4. (Optional) Download and install GraalVM JDK for Java 21 or later to run Python with runtime compilation and to build a native image. The demo will work on any OpenJDK distribution, but will be much faster on GraalVM JDK. ## Building and Running the Application: @@ -62,7 +70,7 @@ The Python code has to be shipped in a _resources_ directory that is kept next t 3. Run the native executable: ```bash - build/native/nativeCompile/websocket.chat + ./build/native/nativeCompile/websocket.chat ``` ### Learn More From 6b8c90625fca897c52da6a2cfcf792535b3d31c5 Mon Sep 17 00:00:00 2001 From: Olga Gupalo <31922163+olyagpl@users.noreply.github.com> Date: Wed, 14 Feb 2024 10:41:55 +0100 Subject: [PATCH 6/9] Update README.md Co-authored-by: Bernard Horan --- polyglot-chat-app/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polyglot-chat-app/README.md b/polyglot-chat-app/README.md index 01860425a..a4c62355c 100644 --- a/polyglot-chat-app/README.md +++ b/polyglot-chat-app/README.md @@ -3,7 +3,7 @@ This example demonstrates how to integrate Python on GraalVM with a Micronaut application. The application uses the Gradle build tool. -## Preparation` +## Preparation 1. Download or clone GraalVM demos repository and navigate into the `polyglot-chat-app` directory: ```bash From 256c2aa088b3980bb53c471bc8e108ed783fea06 Mon Sep 17 00:00:00 2001 From: Olga Gupalo <31922163+olyagpl@users.noreply.github.com> Date: Wed, 14 Feb 2024 10:42:16 +0100 Subject: [PATCH 7/9] Update README.md Co-authored-by: Bernard Horan --- polyglot-chat-app/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polyglot-chat-app/README.md b/polyglot-chat-app/README.md index a4c62355c..46b539cf9 100644 --- a/polyglot-chat-app/README.md +++ b/polyglot-chat-app/README.md @@ -5,7 +5,7 @@ The application uses the Gradle build tool. ## Preparation -1. Download or clone GraalVM demos repository and navigate into the `polyglot-chat-app` directory: +1. Download or clone the GraalVM demos repository and navigate into the _polyglot-chat-app_ directory: ```bash git clone https://github.com/graalvm/graalvm-demos ``` From b88663e1e53206a411191b537610e230f6735e0c Mon Sep 17 00:00:00 2001 From: Bernard Horan Date: Thu, 15 Feb 2024 11:22:09 +0000 Subject: [PATCH 8/9] Update README and build.gradle to use correct version This now produces a native executable --- polyglot-chat-app/README.md | 40 +++++++++++++++++++--------------- polyglot-chat-app/build.gradle | 6 ++--- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/polyglot-chat-app/README.md b/polyglot-chat-app/README.md index 46b539cf9..728219bb1 100644 --- a/polyglot-chat-app/README.md +++ b/polyglot-chat-app/README.md @@ -1,6 +1,7 @@ # Polyglot Chat Application This example demonstrates how to integrate Python on GraalVM with a Micronaut application. +The application uses the nltk module to analyze text sentiment. The application uses the Gradle build tool. ## Preparation @@ -15,27 +16,30 @@ The application uses the Gradle build tool. 2. Download and install the latest GraalPy as described in the [Getting Started guide](https://www.graalvm.org/latest/reference-manual/python/#installing-graalpy). For example on Linux: ```bash - wget https://github.com/oracle/graalpython/releases/download/graal-23.1.1/graalpy-23.1.1-linux-amd64.tar.gz - tar xzf graalpy-23.1.1-linux-amd64.tar.gz + pyenv install graalpy-23.1.2 + pyenv shell graalpy-23.1.2 `` -3. Create a virtual environment for this demo in the _resources_ directory, and install the required packages: +3. Create a virtual environment for the demo in the _resources_ directory, activate it, install the required package, and download a lexicon: ```bash - graalpy-23.1.1-linux-amd64/bin/graalpy -m venv src/main/resources/venv + graalpy -m venv src/main/resources/venv ``` ```bash - src/main/resources/venv/bin/graalpy -m pip install nltk + source src/main/resources/venv/bin/activate ``` ```bash - src/main/resources/venv/bin/graalpy -c "import nltk; nltk.download('vader_lexicon')" + graalpy -m pip install nltk + ``` + ```bash + graalpy -c "import nltk; nltk.download('vader_lexicon')" ``` -4. (Optional) Download and install GraalVM JDK for Java 21 or later to run Python with runtime compilation and to build a native image. -The demo will work on any OpenJDK distribution, but will be much faster on GraalVM JDK. +4. (Optional) [Download and install GraalVM JDK for Java 21](https://www.graalvm.org/downloads/) or later to run Python with runtime compilation and to build a native image. +The demo will work with any OpenJDK distribution, but will be much faster on GraalVM JDK. -## Building and Running the Application: +## Building and Running the Application -1. Build application with Gradle: +1. Build and run the application using Gradle: ```bash ./gradlew run ``` @@ -43,8 +47,8 @@ The demo will work on any OpenJDK distribution, but will be much faster on Graal 2. Navigate to [http://localhost:12345/#/chat/bob](http://localhost:12345/#/chat/bob). You can connect from multiple browsers and chat via websockets. - The Python code will load a language model in the background. - Once it is ready, it will analyse the sentiments of all messages and add an emoji to the message to indicate the feelings conveyed. + The Python code loads a language model in the background—this can take up to 5 minutes. + Once it is ready, it analyzes the sentiments of messages and add an emoji to each message to indicate the feelings conveyed. A chat may look like this (newest message at the top): ``` @@ -53,17 +57,19 @@ The demo will work on any OpenJDK distribution, but will be much faster on Graal [bob 💬] still loading the sentiment model I believe ``` -## Building a Native Image +## (Optional) Building a Native Executable + +> Note: this requires [Download and install GraalVM JDK for Java 21](https://www.graalvm.org/downloads/) or later. -The application can be AOT compiled using GraalVM Native Image. -The Python code has to be shipped in a _resources_ directory that is kept next to the native executable. +The application can be compiled ahead-of-time to a native executable using GraalVM Native Image. +The Python code must be copied to the _build/_ directory that is kept next to the native executable. -1. Build a native executable with the Micronaut AOT support: +1. Build a native executable using Micronaut AOT support: ```bash ./gradlew nativeCompile ``` -2. Copy the venv into the output _resources_ directory: +2. Copy the _venv_ directory into the output _resources_ directory: ```bash cp -R src/main/resources/venv/ build/native/nativeCompile/resources/python/ ``` diff --git a/polyglot-chat-app/build.gradle b/polyglot-chat-app/build.gradle index de5d031a6..7e7b05a9f 100644 --- a/polyglot-chat-app/build.gradle +++ b/polyglot-chat-app/build.gradle @@ -31,9 +31,9 @@ dependencies { implementation("jakarta.inject:jakarta.inject-api:2.0.1") - implementation("org.graalvm.sdk:graal-sdk:23.1.1") - implementation("org.graalvm.polyglot:python:23.1.1") - implementation("org.graalvm.sdk:nativeimage:23.1.1") + implementation("org.graalvm.sdk:graal-sdk:23.1.2") + implementation("org.graalvm.polyglot:python:23.1.2") + implementation("org.graalvm.sdk:nativeimage:23.1.2") } From 006f7df0cbec927f43fe4d571375ec6092bf3f88 Mon Sep 17 00:00:00 2001 From: Bernard Horan Date: Thu, 15 Feb 2024 12:20:57 +0000 Subject: [PATCH 9/9] Update README.md --- polyglot-chat-app/README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/polyglot-chat-app/README.md b/polyglot-chat-app/README.md index 728219bb1..635176f46 100644 --- a/polyglot-chat-app/README.md +++ b/polyglot-chat-app/README.md @@ -1,8 +1,8 @@ # Polyglot Chat Application -This example demonstrates how to integrate Python on GraalVM with a Micronaut application. -The application uses the nltk module to analyze text sentiment. -The application uses the Gradle build tool. +This example demonstrates how to integrate Python in a Micronaut Java application using the Gradle build tool. +The application uses the [Natural Language Toolkit (nltk)](https://www.nltk.org/) module to analyze the sentiment of user input. +The example also shows how to create a native executable from the application using GraalVM. ## Preparation @@ -14,11 +14,11 @@ The application uses the Gradle build tool. cd graalvm-demos/polyglot-chat-app ``` -2. Download and install the latest GraalPy as described in the [Getting Started guide](https://www.graalvm.org/latest/reference-manual/python/#installing-graalpy). For example on Linux: +2. Download and install the latest GraalPy as described in the [Getting Started guide](https://www.graalvm.org/latest/reference-manual/python/#installing-graalpy). For example: ```bash pyenv install graalpy-23.1.2 pyenv shell graalpy-23.1.2 - `` + ``` 3. Create a virtual environment for the demo in the _resources_ directory, activate it, install the required package, and download a lexicon: ```bash @@ -34,8 +34,8 @@ The application uses the Gradle build tool. graalpy -c "import nltk; nltk.download('vader_lexicon')" ``` -4. (Optional) [Download and install GraalVM JDK for Java 21](https://www.graalvm.org/downloads/) or later to run Python with runtime compilation and to build a native image. -The demo will work with any OpenJDK distribution, but will be much faster on GraalVM JDK. +4. The demo will work with any OpenJDK distribution, but will be much faster on [GraalVM JDK for Java 21](https://www.graalvm.org/downloads/). + ## Building and Running the Application @@ -54,12 +54,12 @@ The demo will work with any OpenJDK distribution, but will be much faster on Gra ``` [bob 😀] awesome, GraalVM and GraalPy rock! [bob 🫥] are we done yet? - [bob 💬] still loading the sentiment model I believe + [bob 💬] Joined! ``` ## (Optional) Building a Native Executable -> Note: this requires [Download and install GraalVM JDK for Java 21](https://www.graalvm.org/downloads/) or later. +> Note: this requires [GraalVM JDK for Java 21](https://www.graalvm.org/downloads/) or later. The application can be compiled ahead-of-time to a native executable using GraalVM Native Image. The Python code must be copied to the _build/_ directory that is kept next to the native executable.