diff --git a/polyglot-chat-app/README.md b/polyglot-chat-app/README.md index 46b539cf..728219bb 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 de5d031a..7e7b05a9 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") }