Skip to content

Commit

Permalink
Update README and build.gradle to use correct version
Browse files Browse the repository at this point in the history
This now produces a native executable
  • Loading branch information
Bernard Horan committed Feb 15, 2024
1 parent 256c2aa commit b88663e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
40 changes: 23 additions & 17 deletions polyglot-chat-app/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,36 +16,39 @@ 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
```
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):
```
Expand All @@ -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/
```
Expand Down
6 changes: 3 additions & 3 deletions polyglot-chat-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}


Expand Down

0 comments on commit b88663e

Please sign in to comment.