Skip to content

Commit 00cf030

Browse files
authored
Merge pull request #18 from paulparkinson/main
workshop init 3 - working and will use for doc...
2 parents 6bac439 + ffa9940 commit 00cf030

16 files changed

+164
-106
lines changed

Dockerfile

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
FROM openjdk:11-jre-slim
1+
FROM maven:3.6.3-openjdk-11 AS maven_build
2+
COPY pom.xml /tmp/pom.xml
3+
COPY env.properties /tmp/env.properties
4+
COPY src /tmp/src
5+
COPY lib /tmp/lib
6+
COPY lib/oci-java-sdk-generativeai-3.25.1-preview1-20230906.204234-1.jar /tmp/lib/oci-java-sdk-generativeai-3.25.1-preview1-20230906.204234-1.jar
7+
WORKDIR /tmp/
28

3-
ENTRYPOINT ["java", "-jar", "/usr/share/oracleai/oracleai.jar"]
9+
RUN mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=/tmp/lib/oci-java-sdk-generativeai-3.25.1-preview1-20230906.204234-1.jar
10+
RUN mvn -f /tmp/pom.xml clean package
411

5-
ADD target/oracleai-0.0.1-SNAPSHOT.jar /usr/share/oracleai/oracleai.jar
12+
FROM openjdk
13+
EXPOSE 8080
14+
15+
CMD ls /tmp
16+
COPY --from=maven_build /tmp/target/oracleai-0.0.1-SNAPSHOT.jar /app/oracleai-0.0.1-SNAPSHOT.jar
17+
ENTRYPOINT ["java","-jar","/app/oracleai-0.0.1-SNAPSHOT.jar"]

build.sh

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

3-
source env.properties
4-
53
#The following is temporary until release is available in maven and only required to be called once...
64
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=lib/oci-java-sdk-generativeai-3.25.1-preview1-20230906.204234-1.jar
75
mvn clean package

build_and_push.sh

Lines changed: 0 additions & 24 deletions
This file was deleted.

build_and_run.sh

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

3-
#source ~/Downloads/env.properties
4-
source env.properties
3+
export `cat env.properties`
4+
#export `cat ~/Downloads/env.properties`
55

6-
mvn clean package ; java -Djava.security.debug="access,failure" -jar target/oracleai-0.0.1-SNAPSHOT.jar
6+
mvn clean package ; java -jar target/oracleai-0.0.1-SNAPSHOT.jar

build_docker.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
export IMAGE_NAME=oracleai
4+
export IMAGE_VERSION=0.1
5+
6+
7+
if [ -z "$DOCKER_REGISTRY" ]; then
8+
echo "DOCKER_REGISTRY not set. Will set it to 'test"
9+
export DOCKER_REGISTRY=test
10+
fi
11+
12+
export IMAGE=${DOCKER_REGISTRY}/${IMAGE_NAME}:${IMAGE_VERSION}
13+
14+
docker build -t=$IMAGE .
15+
16+
#docker push "$IMAGE"
17+
#if [ $? -eq 0 ]; then
18+
# docker rmi "$IMAGE"
19+
#fi
20+

env.properties

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
#!/bin/bash
2-
3-
## Add exports here... For example...
4-
#export COHERE_KEY="mykeyvalue"
5-
#export OPENAI_KEY="mykeyvalue"
6-
#export OCICONFIG_FILE=~/.oci/config
7-
#export OCICONFIG_PROFILE=DEFAULT
8-
#only set region to override the default found in OCI config, eg if using a feature only avaiable in a certain region
9-
#export REGION="US_CHICAGO_1"
10-
#export COMPARTMENT_ID="ocid1.compartment.oc1..mycompartmentvalue"
11-
#export OBJECTSTORAGE_NAMESPACE="myobjectstorenamespacename"
12-
#export OBJECTSTORAGE_BUCKETNAME="myobjectstorebucketname"
13-
#export ORDS_ENDPOINT_URL="myordsendpointurl"
14-
#export OCI_VISION_SERVICE_ENDPOINT="https://vision.aiservice.myregion.oci.oraclecloud.com"
15-
#export OCI_SPEECH_SERVICE_ENDPOINT="https://speech.aiservice.myregion.oci.oraclecloud.com"
16-
#export OCI_GENAI_SERVICE_ENDPOINT="https://genai.aiservice.us-chicago-1.oci.oraclecloud.com"
17-
18-
## The following are only applicable when using Kubernetes...
19-
#export KUBECONFIG=~/.kube/config-mykubeconfig
20-
#export DOCKER_REGISTRY=us-myregion-1.ocir.io/myocirnamespacename/myocirfolder
21-
1+
OCICONFIG_FILE=~/.oci/config
2+
OCICONFIG_PROFILE=DEFAULT
3+
COMPARTMENT_ID=ocid1.compartment.oc1..mycompartmentvalue
4+
OBJECTSTORAGE_NAMESPACE=myobjectstorenamespacename
5+
OBJECTSTORAGE_BUCKETNAME=myobjectstorebucketname
6+
ORDS_ENDPOINT_URL=https://myordsendpointurl
7+
ORDS_ENDPOINT_URL=https://rddainsuh6u1okc-aidatabaseworkshop.adb.us-ashburn-1.oraclecloudapps.com/ords/aiuser/_sdw/
8+
OCI_VISION_SERVICE_ENDPOINT=https://vision.aiservice.myregion.oci.oraclecloud.com
9+
OCI_SPEECH_SERVICE_ENDPOINT=https://speech.aiservice.myregion.oci.oraclecloud.com
10+
OCI_GENAI_SERVICE_ENDPOINT=https://genai.aiservice.us-chicago-1.oci.oraclecloud.com
11+
OPENAI_KEY=mykeyfordalleifused

pom.xml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
<spring.boot.version>2.7.6</spring.boot.version>
2121
<spring.vault.version>3.1.1</spring.vault.version>
2222
<oci.sdk.version>3.29.0</oci.sdk.version>
23-
24-
2523
<jib-maven-plugin.version>3.3.1</jib-maven-plugin.version>
2624
<liquibase.version>4.17.2</liquibase.version>
2725
<docker.registry>${env.DOCKER_REGISTRY}</docker.registry>
@@ -176,19 +174,14 @@
176174
</dependencyManagement>
177175
<build>
178176
<plugins>
179-
<plugin>
180-
<groupId>org.springframework.boot</groupId>
181-
<artifactId>spring-boot-maven-plugin</artifactId>
182-
</plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>9</source><target>9</target></configuration></plugin>
183-
184177
<plugin>
185178
<groupId>org.springframework.boot</groupId>
186179
<artifactId>spring-boot-maven-plugin</artifactId>
187180
</plugin>
188181
<plugin>
189182
<groupId>org.apache.maven.plugins</groupId>
190183
<artifactId>maven-compiler-plugin</artifactId>
191-
<configuration><source>8</source><target>8</target></configuration>
184+
<configuration><source>11</source><target>11</target></configuration>
192185
</plugin>
193186

194187
</plugins>

run.sh

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

3-
source env.properties
3+
export `cat env.properties`
44

5-
mvn clean package ; java -jar target/oracleai-0.0.1-SNAPSHOT.jar
5+
java -jar target/oracleai-0.0.1-SNAPSHOT.jar

run_docker.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
export IMAGE_NAME=oracleai
4+
export IMAGE_VERSION=0.1
5+
6+
7+
if [ -z "$DOCKER_REGISTRY" ]; then
8+
echo "DOCKER_REGISTRY not set. Will set it to 'test"
9+
export DOCKER_REGISTRY=test
10+
fi
11+
12+
export IMAGE=${DOCKER_REGISTRY}/${IMAGE_NAME}:${IMAGE_VERSION}
13+
14+
docker run --env-file env.properties -p 127.0.0.1:8080:8080/tcp $IMAGE

sql/aiuser-tables-indexes-functions.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
--run as aiuser
2-
31
CREATE TABLE aivision_results
42
(id RAW (16) NOT NULL,
53
date_loaded TIMESTAMP WITH TIME ZONE,
@@ -10,10 +8,12 @@ CREATE TABLE aivision_results
108
/
119

1210
create index aivisionresultsindex on aivision_results(textfromai) indextype is ctxsys.context;
11+
/
12+
1313
--select index_name, index_type, status from user_indexes where index_name = 'AIVISIONAIRESULTSINDEX';
1414
--select idx_name, idx_table, idx_text_name from ctx_user_indexes;
1515
--select token_text from dr$aivisionresultsindex$i;
16-
/
16+
1717

1818
CREATE OR REPLACE FUNCTION VISIONAI_RESULTS_TEXT_SEARCH(p_sql IN VARCHAR2) RETURN SYS_REFCURSOR AS refcursor SYS_REFCURSOR;
1919
BEGIN
@@ -50,10 +50,10 @@ dbms_cloud.create_credential (
5050
);
5151
END;
5252

53-
--The following two functions (text and object detection) are indentical
53+
--The following two functions (text and object detection) are identical
5454
--except for the json_table parsing for the textfromai field,
5555
--and so technically the p_featureType is not necessary and the function could be made to handle/parse both types,
56-
--however, for readability they are broken into two distinct functions
56+
--however, for readability they are broken into two distinct functions.
5757

5858

5959
CREATE OR REPLACE FUNCTION VISIONAI_TEXTDETECTION (

sql/create_aiuser.sql

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
--run as admin
2-
31
CREATE USER aiuser identified BY [Yourpassword];
42
grant CREATE session TO aiuser;
5-
grant RESOURCE, db_developer_role TO aiuser;
3+
grant RESOURCE TO aiuser;
64
grant unlimited tablespace TO aiuser;
7-
grant EXECUTE ON javascript TO aiuser;
8-
grant EXECUTE dynamic mle TO aiuser;
95
grant execute on DBMS_CLOUD to aiuser;
6+

src/main/java/oracleai/GenerateAPictureStoryUsingOnlySpeech.java

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package oracleai;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import oracleai.services.ImageGeneration;
46
import oracleai.services.OracleObjectStore;
57
import oracleai.services.OracleSpeechAI;
8+
import org.jetbrains.annotations.NotNull;
69
import org.springframework.stereotype.Controller;
710
import org.springframework.ui.Model;
811
import org.springframework.web.bind.annotation.*;
@@ -11,6 +14,7 @@
1114
import javax.sound.sampled.*;
1215
import java.io.*;
1316
import java.util.*;
17+
import java.util.stream.Collectors;
1418

1519
@Controller
1620
@RequestMapping("/picturestory")
@@ -26,25 +30,32 @@ public String reset(Model model) {
2630
}
2731

2832
@PostMapping("/picturestory")
29-
public String picturestory(@RequestParam("genopts") String genopts,
30-
@RequestParam("file") MultipartFile multipartFile, Model model) throws Exception {
33+
public String picturestory(@RequestParam("opts") String opts,
34+
@RequestParam("genopts") String genopts,
35+
@RequestParam("file") MultipartFile multipartFile,
36+
Model model) throws Exception {
37+
if (opts.equals("fileaudio") ) return fileaudio(genopts, multipartFile, model);
38+
else return liveaudio(genopts, model);
39+
}
40+
41+
@NotNull
42+
private String fileaudio(String genopts, MultipartFile multipartFile, Model model) throws Exception {
3143
OracleObjectStore.sendToObjectStorage(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
3244
String transcriptionJobId = OracleSpeechAI.getTranscriptFromOCISpeech(multipartFile.getOriginalFilename());
3345
System.out.println("transcriptionJobId: " + transcriptionJobId);
3446
String jsonTranscriptFromObjectStorage =
3547
OracleObjectStore.getFromObjectStorage(transcriptionJobId,
36-
AIApplication.OBJECTSTORAGE_NAMESPACE + "_" + AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" + multipartFile.getOriginalFilename() + ".json");
48+
AIApplication.OBJECTSTORAGE_NAMESPACE + "_" +
49+
AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" +
50+
multipartFile.getOriginalFilename() + ".json");
3751
System.out.println("jsonTranscriptFromObjectStorage: " + jsonTranscriptFromObjectStorage);
38-
// System.out.println("getFromObjectStorage: " + getFromObjectStorage("leia.m4a"));
39-
// String pictureDescription = parse(jsonTranscriptFromObjectStorage);
40-
String pictureDescription = "man rowing a boat through the forest";
52+
String pictureDescription = getConcatenatedTokens(jsonTranscriptFromObjectStorage);
4153
imageLocations.add(ImageGeneration.imagegeneration(pictureDescription + " " + genopts));
4254
model.addAttribute("imageLocations", imageLocations.toArray(new String[0]));
4355
return "resultswithimages";
4456
}
4557

46-
@GetMapping("/picturestoryrecordlocally")
47-
public String picturestoryrecordlocally(@RequestParam("genopts") String genopts, Model model) throws Exception {
58+
public String liveaudio(String genopts, Model model) throws Exception {
4859
AudioFormat format =
4960
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0f, 16, 1,
5061
(16 / 8) * 1, 44100.0f, true);
@@ -65,24 +76,33 @@ public String picturestoryrecordlocally(@RequestParam("genopts") String genopts,
6576
AudioSystem.write(audioInputStream, fileType, file);
6677
System.out.println("Saved " + file.getAbsolutePath());
6778
OracleObjectStore.sendToObjectStorage(file.getName(), new FileInputStream(file));
68-
69-
String transcriptionJobId = OracleSpeechAI.getTranscriptFromOCISpeech(file.getName()); //json file
70-
// String transcriptionJobId = getTranscriptFromOCISpeech("testing123.wav");
79+
String transcriptionJobId = OracleSpeechAI.getTranscriptFromOCISpeech(file.getName());
7180
System.out.println("transcriptionJobId: " + transcriptionJobId);
7281
String jsonTranscriptFromObjectStorage =
7382
OracleObjectStore.getFromObjectStorage(transcriptionJobId,
74-
// AIApplication.OBJECTSTORAGE_NAMESPACE + "_" + AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" + "testing123.wav" + ".json"));
75-
AIApplication.OBJECTSTORAGE_NAMESPACE + "_" + AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" + file.getName() + ".json");
83+
AIApplication.OBJECTSTORAGE_NAMESPACE + "_" +
84+
AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" + file.getName() + ".json");
7685
System.out.println("jsonTranscriptFromObjectStorage: " + jsonTranscriptFromObjectStorage);
77-
// System.out.println("getFromObjectStorage: " + getFromObjectStorage("leia.m4a"));
78-
// String pictureDescription = parse(jsonTranscriptFromObjectStorage);
79-
String pictureDescription = "man rowing a boat through the forest";
86+
String pictureDescription = getConcatenatedTokens(jsonTranscriptFromObjectStorage);
8087
imageLocations.add(ImageGeneration.imagegeneration(pictureDescription + " " + genopts));
8188
model.addAttribute("imageLocations", imageLocations.toArray(new String[0]));
8289
return "resultswithimages";
8390
}
8491

85-
92+
public String getConcatenatedTokens(String json) {
93+
ObjectMapper objectMapper = new ObjectMapper();
94+
try {
95+
OracleSpeechAI.TranscriptionResponse response =
96+
objectMapper.readValue(json, OracleSpeechAI.TranscriptionResponse.class);
97+
return response.getTranscriptions().stream()
98+
.flatMap(transcription -> transcription.getTokens().stream())
99+
.map(OracleSpeechAI.TranscriptionResponse.Transcription.Token::getToken)
100+
.collect(Collectors.joining(" "));
101+
} catch (JsonProcessingException e) {
102+
e.printStackTrace();
103+
return null;
104+
}
105+
}
86106

87107
public class SoundRecorder implements Runnable {
88108
AudioInputStream audioInputStream;
@@ -106,7 +126,8 @@ public void stop() {
106126

107127
@Override
108128
public void run() {
109-
try (final ByteArrayOutputStream out = new ByteArrayOutputStream(); final TargetDataLine line = getTargetDataLineForRecord();) {
129+
try (final ByteArrayOutputStream out = new ByteArrayOutputStream();
130+
final TargetDataLine line = getTargetDataLineForRecord();) {
110131
int frameSizeInBytes = format.getFrameSize();
111132
int bufferLengthInFrames = line.getBufferSize() / 8;
112133
final int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
@@ -121,7 +142,9 @@ public void run() {
121142
}
122143
}
123144

124-
public void buildByteOutputStream(final ByteArrayOutputStream out, final TargetDataLine line, int frameSizeInBytes, final int bufferLengthInBytes) throws IOException {
145+
public void buildByteOutputStream(final ByteArrayOutputStream out,
146+
final TargetDataLine line, int frameSizeInBytes,
147+
final int bufferLengthInBytes) throws IOException {
125148
final byte[] data = new byte[bufferLengthInBytes];
126149
int numBytesRead;
127150

0 commit comments

Comments
 (0)