Skip to content

Commit f184604

Browse files
committed
Update samples with last changes
Signed-off-by: andreadimaio <[email protected]>
1 parent 2fb62e4 commit f184604

File tree

6 files changed

+129
-2
lines changed

6 files changed

+129
-2
lines changed

samples/detection/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Watsonx Detection Example
2+
3+
This is a simple example that demonstrates how to use IBM watsonx.ai to detect hate and profanity (HAP) and personally identifiable information (PII).
4+
5+
## Prerequisites
6+
7+
Before running the application, set the following environment variables or create a new `.env` file in the project's root folder:
8+
9+
- `WATSONX_API_KEY` – Your watsonx.ai API key
10+
- `WATSONX_URL` – The base URL for the watsonx.ai service
11+
- `WATSONX_PROJECT_ID` – Your watsonx.ai project id
12+
13+
Example (Linux/macOS):
14+
```bash
15+
export WATSONX_API_KEY=api-key
16+
export WATSONX_URL=https://watsonx-url
17+
export WATSONX_PROJECT_ID=project-id
18+
```
19+
20+
Example (Windows CMD):
21+
```cmd
22+
set WATSONX_API_KEY=api-key
23+
set WATSONX_URL=https://watsonx-url
24+
set WATSONX_PROJECT_ID=project-id
25+
```
26+
## How to Run
27+
Use Maven to run the application.
28+
```bash
29+
mvn package exec:java
30+
```

samples/detection/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.ibm.watsonx</groupId>
7+
<artifactId>watsonx-ai-sample-detection</artifactId>
8+
<version>0.12.0</version>
9+
<name>Detection Sample</name>
10+
11+
<parent>
12+
<groupId>com.ibm.watsonx</groupId>
13+
<artifactId>watsonx-ai-sample-parent</artifactId>
14+
<version>0.12.0</version>
15+
<relativePath>../</relativePath>
16+
</parent>
17+
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>org.codehaus.mojo</groupId>
22+
<artifactId>exec-maven-plugin</artifactId>
23+
<version>3.5.1</version>
24+
<configuration>
25+
<mainClass>com.ibm.detection.App</mainClass>
26+
</configuration>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright IBM Corp. 2025 - 2025
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package com.ibm.detection;
6+
7+
import java.net.URI;
8+
import java.time.Duration;
9+
import java.util.List;
10+
import org.eclipse.microprofile.config.Config;
11+
import org.eclipse.microprofile.config.ConfigProvider;
12+
import com.ibm.watsonx.ai.detection.DetectionService;
13+
import com.ibm.watsonx.ai.detection.DetectionTextRequest;
14+
import com.ibm.watsonx.ai.detection.DetectionTextResponse;
15+
import com.ibm.watsonx.ai.detection.detector.Hap;
16+
import com.ibm.watsonx.ai.detection.detector.Pii;
17+
18+
public class App {
19+
20+
private static final Config config = ConfigProvider.getConfig();
21+
22+
public static void main(String[] args) throws Exception {
23+
24+
try {
25+
26+
var url = URI.create(config.getValue("WATSONX_URL", String.class));
27+
var apiKey = config.getValue("WATSONX_API_KEY", String.class);
28+
var projectId = config.getValue("WATSONX_PROJECT_ID", String.class);
29+
30+
DetectionService detectionService = DetectionService.builder()
31+
.apiKey(apiKey)
32+
.projectId(projectId)
33+
.timeout(Duration.ofSeconds(60))
34+
.baseUrl(url)
35+
.build();
36+
37+
DetectionTextRequest request = DetectionTextRequest.builder()
38+
.input("I kill you with my phone number 1234567890")
39+
.detectors(Pii.create(), Hap.builder().threshold(0.3f).build())
40+
.build();
41+
42+
List<DetectionTextResponse> response = detectionService.detect(request).detections();
43+
44+
System.out.println(response);
45+
System.exit(0);
46+
47+
} catch (Exception e) {
48+
e.printStackTrace();
49+
} finally {
50+
System.exit(0);
51+
}
52+
}
53+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="[%p] %d{yyyy-MM-dd HH:mm:ss} %msg%n" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="info">
10+
<AppenderRef ref="Console" />
11+
</Root>
12+
</Loggers>
13+
</Configuration>

samples/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<module>time-series</module>
2525
<module>utility-tools</module>
2626
<module>deployment</module>
27+
<module>detection</module>
2728
</modules>
2829

2930
<licenses>
@@ -41,7 +42,7 @@
4142
<dependency>
4243
<groupId>com.ibm.watsonx</groupId>
4344
<artifactId>watsonx-ai</artifactId>
44-
<version>0.12.0</version>
45+
<version>999-SNAPSHOT</version>
4546
</dependency>
4647

4748
<dependency>

samples/text-classification/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Watsonx Embedding Example
1+
# Watsonx Text Classification Example
22

33
This example demonstrates how to use IBM watsonx.ai to perform text classification from documents stored in IBM Cloud Object Storage (COS).
44

0 commit comments

Comments
 (0)