Skip to content

Commit 0bef65a

Browse files
committed
init
1 parent 45a3ae5 commit 0bef65a

9 files changed

+282
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
MISSING
99

1010
https://paulparkinson.github.io/developer/oracle-ai-for-sustainable-dev/workshops/freetier/index.html?lab=oml-healthcare
11+
https://sdgs.un.org/goals
12+
https://huggingface.co/DeepFloyd/IF-I-XL-v1.0
1113

1214
## Getting Started
1315
MISSING

build_and_run.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
#!/bin/bash
22

33
## Add exports here... For example...
4-
#export OPENAI_KEY="mykeyvalue"
54
#export COHERE_KEY="mykeyvalue"
5+
#export OPENAI_KEY="mykeyvalue"
66
#export OCICONFIG_FILE=~/.oci/config
77
#export OCICONFIG_PROFILE=DEFAULT
88
#export COMPARTMENT_ID="ocid1.compartment.oc1..mycompartmentvalue"
99
#export OBJECTSTORAGE_NAMESPACE="myobjectstorenamespacename"
1010
#export OBJECTSTORAGE_BUCKETNAME="myobjectstorebucketname"
11-
#export SPRING_DATASOURCE_USERNAME="myuser"
12-
#export SPRING_DATASOURCE_URL="jdbc:oracle:thin:@myservice_high?TNS_ADMIN=/.../mywalletdir"
13-
#export SPRING_DATASOURCE_PASSWORD="mypw"
11+
#export ORDS_ENDPOINT_ANALYZE_IMAGE_OBJECTSTORE="myordsendpoint"
12+
#export ORDS_ENDPOINT_ANALYZE_IMAGE_INLINE="myordsendpoint"
13+
#export OCI_VISION_SERVICE_ENDPOINT="https://vision.aiservice.us-ashburn-1.oci.oraclecloud.com"
14+
1415
## The following are only applicable when using Kubernetes...
1516
#export KUBECONFIG=~/.kube/config-healthai
1617
#export DOCKER_REGISTRY=us-ashburn-1.ocir.io/oradbclouducm/gd74087885
1718

18-
#The following is temporary until release is avaiable in maven and only required to be called once...
19+
#The following is temporary until release is available in maven and only required to be called once...
1920
#mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=oci-java-sdk-generativeai-3.25.1-preview1-20230906.204234-1.jar
2021
mvn clean package ; java -Djava.security.debug="access,failure" -jar target/oracleai-0.0.1-SNAPSHOT.jar
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
set serveroutput on
3+
declare
4+
resp DBMS_CLOUD_TYPES.resp;
5+
endpoint varchar2(64) := 'https://vision.aiservice.us-ashburn-1.oci.oraclecloud.com';
6+
mydata varchar2(32000);
7+
begin
8+
resp := DBMS_CLOUD.send_request(credential_name => 'OCI_KEY_CRED',uri =>
9+
endpoint || '/20220125/actions/analyzeImage',
10+
method => 'POST',
11+
body => UTL_RAW.cast_to_raw(
12+
JSON_OBJECT(
13+
'features' VALUE JSON_ARRAY(
14+
JSON_OBJECT('featureType' VALUE 'OBJECT_DETECTION')
15+
),
16+
'image' VALUE JSON_OBJECT(
17+
'source' VALUE 'OBJECT_STORAGE',
18+
'namespaceName' VALUE 'yournamespace',
19+
'bucketName' VALUE 'yourbucketname',
20+
'objectName' VALUE 'objectdetectiontestimage.jpg'
21+
),
22+
'compartmentId' VALUE 'ocid1.compartment.oc1..yourcompartmentid'
23+
))
24+
);
25+
mydata := DBMS_CLOUD.get_response_text(resp);
26+
dbms_output.put_line('result: ' || mydata);
27+
INSERT INTO aivision_results VALUES (SYS_GUID(), SYSTIMESTAMP, 'test', mydata );
28+
commit;
29+
end;
30+
31+
32+
33+
34+
35+
36+
37+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
create or replace FUNCTION call_analyze_image_api_inline (
2+
p_endpoint VARCHAR2,
3+
p_compartment_ocid VARCHAR2,
4+
p_image_data CLOB
5+
) RETURN CLOB IS
6+
resp DBMS_CLOUD_TYPES.resp;
7+
json_response CLOB;
8+
BEGIN
9+
resp := DBMS_CLOUD.send_request(
10+
credential_name => 'OCI_KEY_CRED',
11+
uri => p_endpoint || '/20220125/actions/analyzeImage',
12+
method => 'POST',
13+
body => UTL_RAW.cast_to_raw(
14+
JSON_OBJECT(
15+
'features' VALUE JSON_ARRAY(
16+
JSON_OBJECT('featureType' VALUE 'OBJECT_DETECTION')
17+
),
18+
'image' VALUE JSON_OBJECT(
19+
'source' VALUE 'INLINE',
20+
'data' VALUE p_image_data
21+
),
22+
'compartmentId' VALUE p_compartment_ocid
23+
)
24+
)
25+
);
26+
27+
json_response := DBMS_CLOUD.get_response_text(resp);
28+
dbms_output.put_line('json_response: ' || json_response);
29+
INSERT INTO metering VALUES (SYS_GUID(), SYSTIMESTAMP, 'test', json_response );
30+
31+
RETURN json_response;
32+
EXCEPTION
33+
WHEN OTHERS THEN
34+
-- Handle exceptions if needed and return an error message or raise
35+
RAISE;
36+
END call_analyze_image_api_inline;
37+
38+
BEGIN
39+
ORDS.ENABLE_OBJECT(
40+
P_ENABLED => TRUE,
41+
P_SCHEMA => 'AIUSER',
42+
P_OBJECT => 'CALL_ANALYZE_IMAGE_API_INLINE',
43+
P_OBJECT_TYPE => 'FUNCTION',
44+
P_OBJECT_ALIAS => 'call_analyze_image_api_inline',
45+
P_AUTO_REST_AUTH => FALSE
46+
);
47+
COMMIT;
48+
END;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
create or replace FUNCTION call_analyze_image_api_objectstore (
2+
p_endpoint VARCHAR2,
3+
p_compartment_ocid VARCHAR2,
4+
p_namespaceName VARCHAR2,
5+
p_bucketName VARCHAR2,
6+
p_objectName VARCHAR2
7+
) RETURN CLOB IS
8+
resp DBMS_CLOUD_TYPES.resp;
9+
json_response CLOB;
10+
BEGIN
11+
resp := DBMS_CLOUD.send_request(
12+
credential_name => 'OCI_KEY_CRED',
13+
uri => p_endpoint || '/20220125/actions/analyzeImage',
14+
method => 'POST',
15+
body => UTL_RAW.cast_to_raw(
16+
JSON_OBJECT(
17+
'features' VALUE JSON_ARRAY(
18+
JSON_OBJECT('featureType' VALUE 'OBJECT_DETECTION')
19+
),
20+
'image' VALUE JSON_OBJECT(
21+
'source' VALUE 'OBJECT_STORAGE',
22+
'namespaceName' VALUE p_namespaceName,
23+
'bucketName' VALUE p_bucketName,
24+
'objectName' VALUE p_objectName
25+
),
26+
'compartmentId' VALUE p_compartment_ocid
27+
)
28+
)
29+
);
30+
31+
json_response := DBMS_CLOUD.get_response_text(resp);
32+
dbms_output.put_line('json_response: ' || json_response);
33+
INSERT INTO metering VALUES (SYS_GUID(), SYSTIMESTAMP, 'test', json_response );
34+
35+
RETURN json_response;
36+
EXCEPTION
37+
WHEN OTHERS THEN
38+
-- Handle exceptions if needed and return an error message or raise
39+
RAISE;
40+
END call_analyze_image_api_objectstore;
41+
42+
BEGIN
43+
ORDS.ENABLE_OBJECT(
44+
P_ENABLED => TRUE,
45+
P_SCHEMA => 'AIUSER',
46+
P_OBJECT => 'CALL_ANALYZE_IMAGE_API_OBJECTSTORE',
47+
P_OBJECT_TYPE => 'FUNCTION',
48+
P_OBJECT_ALIAS => 'call_analyze_image_api_objectstore',
49+
P_AUTO_REST_AUTH => FALSE
50+
);
51+
COMMIT;
52+
END;

sql/setup.sql

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
CREATE USER aiuser identified BY [Yourpassword];
2+
grant CREATE session TO aiuser;
3+
grant RESOURCE, db_developer_role TO aiuser;
4+
grant unlimited tablespace TO aiuser;
5+
grant EXECUTE ON javascript TO aiuser;
6+
grant EXECUTE dynamic mle TO aiuser;
7+
grant execute on DBMS_CLOUD to aiuser;
8+
--switch to aiuser
9+
10+
--CREATE TABLE metering
11+
-- (id RAW (16) NOT NULL,
12+
-- date_loaded TIMESTAMP WITH TIME ZONE,
13+
-- label varchar2(20),
14+
-- jsondata CLOB
15+
-- CONSTRAINT ensure_metering_json CHECK (jsondata IS JSON));
16+
--
17+
CREATE TABLE aivision_results
18+
(id RAW (16) NOT NULL,
19+
date_loaded TIMESTAMP WITH TIME ZONE,
20+
label varchar2(20),
21+
jsondata CLOB
22+
CONSTRAINT ensure_aivision_results_json CHECK (jsondata IS JSON));
23+
24+
--CREATE TABLE image_storage (
25+
-- image_id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
26+
-- image_name VARCHAR2(255),
27+
-- image_data BLOB,
28+
-- image_description VARCHAR2(1000),
29+
-- uploaded_on DATE DEFAULT SYSDATE
30+
--);
31+
32+
BEGIN
33+
dbms_cloud.create_credential (
34+
credential_name => 'OCI_KEY_CRED',
35+
user_ocid => 'ocid1.user.oc1..[youruserocid]',
36+
tenancy_ocid => 'ocid1.tenancy.oc1..[yourtenancyocid]',
37+
private_key => '[yourprivatekey you can read this from file or put the contents of your pem without header, footer, and line wraps]'
38+
fingerprint => '[7f:yourfingerprint]'
39+
);
40+
END;

src/main/java/oracleai/AIApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public class AIApplication {
99
public static final String COMPARTMENT_ID = System.getenv("COMPARTMENT_ID");
1010
public static final String OBJECTSTORAGE_NAMESPACE = System.getenv("OBJECTSTORAGE_NAMESPACE");
1111
public static final String OBJECTSTORAGE_BUCKETNAME = System.getenv("OBJECTSTORAGE_BUCKETNAME");
12+
public static final String ORDS_ENDPOINT_ANALYZE_IMAGE_OBJECTSTORE = System.getenv("ORDS_ENDPOINT_ANALYZE_IMAGE_OBJECTSTORE");
13+
public static final String ORDS_ENDPOINT_ANALYZE_IMAGE_INLINE = System.getenv("ORDS_ENDPOINT_ANALYZE_IMAGE_INLINE");
14+
public static final String OCI_VISION_SERVICE_ENDPOINT = System.getenv("OCI_VISION_SERVICE_ENDPOINT"); ;
1215

1316
public static void main(String[] args) {
1417
SpringApplication.run(AIApplication.class, args);

src/main/java/oracleai/ORDSCalls.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package oracleai;
2+
3+
import org.springframework.http.*;
4+
import org.springframework.stereotype.Service;
5+
import org.springframework.web.client.RestTemplate;
6+
import org.springframework.web.multipart.MultipartFile;
7+
8+
import java.util.Base64;
9+
import java.util.Collections;
10+
11+
@Service
12+
public class ORDSCalls {
13+
14+
public static String callAnalyzeImageApi(String ordsEndpoint, String visionServiceIndpoint,
15+
String compartmentOcid, MultipartFile imageFile)
16+
throws Exception {
17+
RestTemplate restTemplate = new RestTemplate();
18+
19+
String base64ImageData = Base64.getEncoder().encodeToString(imageFile.getBytes());
20+
// Construct the JSON request body
21+
String jsonBody = String.format("{\"p_compartment_ocid\": \"%s\", \"p_endpoint\": \"%s\", \"p_image_data\": \"%s\"}",
22+
compartmentOcid, visionServiceIndpoint, base64ImageData);
23+
24+
// Set headers
25+
HttpHeaders headers = new HttpHeaders();
26+
headers.setContentType(MediaType.APPLICATION_JSON);
27+
28+
// Build the request entity
29+
HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);
30+
ResponseEntity<String> response = restTemplate.exchange(ordsEndpoint, HttpMethod.POST, requestEntity, String.class);
31+
32+
// Return the response body
33+
return response.getBody();
34+
}
35+
36+
37+
38+
public static String analyzeImageInObjectStore(
39+
String ordsEndpoint, String visionServiceIndpoint, String compartmentOcid,
40+
String bucketName, String namespaceName, String objectName) {
41+
RestTemplate restTemplate = new RestTemplate();
42+
String jsonPayload = String.format(
43+
"{\"p_bucketname\": \"%s\", \"p_compartment_ocid\": \"%s\", \"p_endpoint\": \"%s\", " +
44+
"\"p_namespacename\": \"%s\", \"p_objectname\": \"%s\"}",
45+
bucketName, compartmentOcid, visionServiceIndpoint, namespaceName, objectName);
46+
HttpHeaders headers = new HttpHeaders();
47+
headers.setContentType(MediaType.APPLICATION_JSON);
48+
HttpEntity<String> entity = new HttpEntity<>(jsonPayload, headers);
49+
ResponseEntity<String> response = restTemplate.exchange(ordsEndpoint, HttpMethod.POST, entity, String.class);
50+
System.out.println("ORDSCalls.analyzeImageInObjectStore response.getBody():" + response.getBody());
51+
return response.getBody();
52+
}
53+
54+
//
55+
// public static String callAnalyzeImageApi(String compartmentOcid, String endpoint, String base64ImageData) {
56+
// RestTemplate restTemplate = new RestTemplate();
57+
//
58+
// // Construct the JSON request body
59+
// String jsonBody = String.format(
60+
// "{\"p_compartment_ocid\": \"%s\", \"p_endpoint\": \"%s\", \"p_image_data\": \"%s\"}",
61+
// compartmentOcid, endpoint, base64ImageData);
62+
//
63+
// // Set headers
64+
// HttpHeaders headers = new HttpHeaders();
65+
// headers.setContentType(MediaType.APPLICATION_JSON);
66+
//
67+
// // Build the request entity
68+
// HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);
69+
//
70+
// // Define the API endpoint
71+
// String apiEndpoint = "https://rddainsuh6u1okc-gd740878851.adb.us-ashburn-1.oraclecloudapps.com/ords/aiuser/call_analyze_image_api/";
72+
//
73+
// // Send POST request
74+
// ResponseEntity<String> response = restTemplate.exchange(apiEndpoint, HttpMethod.POST, requestEntity, String.class);
75+
//
76+
// // Return the response body
77+
// return response.getBody();
78+
// }
79+
80+
81+
}
82+

src/main/java/oracleai/WriteAStoryAboutAPictureAndGiveItsSentiments.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,22 @@ public class WriteAStoryAboutAPictureAndGiveItsSentiments {
4747
public String tellastory(@RequestParam("file") MultipartFile file , @RequestParam("genopts") String genopts)
4848
throws Exception {
4949
log.info("got image file, now analyze, file = " + file);
50-
String objectDetectionResults = processImage(file.getBytes(), true);
50+
String objectDetectionResults = ORDSCalls.callAnalyzeImageApi(
51+
AIApplication.ORDS_ENDPOINT_ANALYZE_IMAGE_INLINE,
52+
AIApplication.OCI_VISION_SERVICE_ENDPOINT,
53+
AIApplication.COMPARTMENT_ID,
54+
file);
55+
ORDSCalls.analyzeImageInObjectStore(
56+
AIApplication.ORDS_ENDPOINT_ANALYZE_IMAGE_OBJECTSTORE,
57+
AIApplication.OCI_VISION_SERVICE_ENDPOINT,
58+
AIApplication.COMPARTMENT_ID,
59+
"doc", "oradbclouducm", "objectdetectiontestimage.jpg");
60+
// String objectDetectionResults = processImage(file.getBytes(), true);
5161
ImageAnalysis imageAnalysis = parseJsonToImageAnalysis(objectDetectionResults);
5262
List<ImageObject> images = imageAnalysis.getImageObjects();
5363
String fullText = "";
5464
for (ImageObject image : images) fullText += image.getName() + ", ";
55-
log.info("fullText = " + fullText);
65+
log.info("tellastory images = " + fullText);
5666
String generatedstory =
5767
chat("using strong negative and positive sentiments, " +
5868
"write a story that is " + genopts + " and includes " + fullText );

0 commit comments

Comments
 (0)