Skip to content

Commit

Permalink
Merge pull request #18 from paulparkinson/main
Browse files Browse the repository at this point in the history
workshop init 3 - working and will use for doc...
  • Loading branch information
jasperan authored Dec 16, 2023
2 parents 6bac439 + ffa9940 commit 00cf030
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 106 deletions.
18 changes: 15 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
FROM openjdk:11-jre-slim
FROM maven:3.6.3-openjdk-11 AS maven_build
COPY pom.xml /tmp/pom.xml
COPY env.properties /tmp/env.properties
COPY src /tmp/src
COPY lib /tmp/lib
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
WORKDIR /tmp/

ENTRYPOINT ["java", "-jar", "/usr/share/oracleai/oracleai.jar"]
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
RUN mvn -f /tmp/pom.xml clean package

ADD target/oracleai-0.0.1-SNAPSHOT.jar /usr/share/oracleai/oracleai.jar
FROM openjdk
EXPOSE 8080

CMD ls /tmp
COPY --from=maven_build /tmp/target/oracleai-0.0.1-SNAPSHOT.jar /app/oracleai-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/app/oracleai-0.0.1-SNAPSHOT.jar"]
2 changes: 0 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/bash

source env.properties

#The following is temporary until release is available in maven and only required to be called once...
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
mvn clean package
24 changes: 0 additions & 24 deletions build_and_push.sh

This file was deleted.

6 changes: 3 additions & 3 deletions build_and_run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

#source ~/Downloads/env.properties
source env.properties
export `cat env.properties`
#export `cat ~/Downloads/env.properties`

mvn clean package ; java -Djava.security.debug="access,failure" -jar target/oracleai-0.0.1-SNAPSHOT.jar
mvn clean package ; java -jar target/oracleai-0.0.1-SNAPSHOT.jar
20 changes: 20 additions & 0 deletions build_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

export IMAGE_NAME=oracleai
export IMAGE_VERSION=0.1


if [ -z "$DOCKER_REGISTRY" ]; then
echo "DOCKER_REGISTRY not set. Will set it to 'test"
export DOCKER_REGISTRY=test
fi

export IMAGE=${DOCKER_REGISTRY}/${IMAGE_NAME}:${IMAGE_VERSION}

docker build -t=$IMAGE .

#docker push "$IMAGE"
#if [ $? -eq 0 ]; then
# docker rmi "$IMAGE"
#fi

32 changes: 11 additions & 21 deletions env.properties
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
#!/bin/bash

## Add exports here... For example...
#export COHERE_KEY="mykeyvalue"
#export OPENAI_KEY="mykeyvalue"
#export OCICONFIG_FILE=~/.oci/config
#export OCICONFIG_PROFILE=DEFAULT
#only set region to override the default found in OCI config, eg if using a feature only avaiable in a certain region
#export REGION="US_CHICAGO_1"
#export COMPARTMENT_ID="ocid1.compartment.oc1..mycompartmentvalue"
#export OBJECTSTORAGE_NAMESPACE="myobjectstorenamespacename"
#export OBJECTSTORAGE_BUCKETNAME="myobjectstorebucketname"
#export ORDS_ENDPOINT_URL="myordsendpointurl"
#export OCI_VISION_SERVICE_ENDPOINT="https://vision.aiservice.myregion.oci.oraclecloud.com"
#export OCI_SPEECH_SERVICE_ENDPOINT="https://speech.aiservice.myregion.oci.oraclecloud.com"
#export OCI_GENAI_SERVICE_ENDPOINT="https://genai.aiservice.us-chicago-1.oci.oraclecloud.com"

## The following are only applicable when using Kubernetes...
#export KUBECONFIG=~/.kube/config-mykubeconfig
#export DOCKER_REGISTRY=us-myregion-1.ocir.io/myocirnamespacename/myocirfolder

OCICONFIG_FILE=~/.oci/config
OCICONFIG_PROFILE=DEFAULT
COMPARTMENT_ID=ocid1.compartment.oc1..mycompartmentvalue
OBJECTSTORAGE_NAMESPACE=myobjectstorenamespacename
OBJECTSTORAGE_BUCKETNAME=myobjectstorebucketname
ORDS_ENDPOINT_URL=https://myordsendpointurl
ORDS_ENDPOINT_URL=https://rddainsuh6u1okc-aidatabaseworkshop.adb.us-ashburn-1.oraclecloudapps.com/ords/aiuser/_sdw/
OCI_VISION_SERVICE_ENDPOINT=https://vision.aiservice.myregion.oci.oraclecloud.com
OCI_SPEECH_SERVICE_ENDPOINT=https://speech.aiservice.myregion.oci.oraclecloud.com
OCI_GENAI_SERVICE_ENDPOINT=https://genai.aiservice.us-chicago-1.oci.oraclecloud.com
OPENAI_KEY=mykeyfordalleifused
9 changes: 1 addition & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
<spring.boot.version>2.7.6</spring.boot.version>
<spring.vault.version>3.1.1</spring.vault.version>
<oci.sdk.version>3.29.0</oci.sdk.version>


<jib-maven-plugin.version>3.3.1</jib-maven-plugin.version>
<liquibase.version>4.17.2</liquibase.version>
<docker.registry>${env.DOCKER_REGISTRY}</docker.registry>
Expand Down Expand Up @@ -176,19 +174,14 @@
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>9</source><target>9</target></configuration></plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration><source>8</source><target>8</target></configuration>
<configuration><source>11</source><target>11</target></configuration>
</plugin>

</plugins>
Expand Down
4 changes: 2 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

source env.properties
export `cat env.properties`

mvn clean package ; java -jar target/oracleai-0.0.1-SNAPSHOT.jar
java -jar target/oracleai-0.0.1-SNAPSHOT.jar
14 changes: 14 additions & 0 deletions run_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

export IMAGE_NAME=oracleai
export IMAGE_VERSION=0.1


if [ -z "$DOCKER_REGISTRY" ]; then
echo "DOCKER_REGISTRY not set. Will set it to 'test"
export DOCKER_REGISTRY=test
fi

export IMAGE=${DOCKER_REGISTRY}/${IMAGE_NAME}:${IMAGE_VERSION}

docker run --env-file env.properties -p 127.0.0.1:8080:8080/tcp $IMAGE
10 changes: 5 additions & 5 deletions sql/aiuser-tables-indexes-functions.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
--run as aiuser

CREATE TABLE aivision_results
(id RAW (16) NOT NULL,
date_loaded TIMESTAMP WITH TIME ZONE,
Expand All @@ -10,10 +8,12 @@ CREATE TABLE aivision_results
/

create index aivisionresultsindex on aivision_results(textfromai) indextype is ctxsys.context;
/

--select index_name, index_type, status from user_indexes where index_name = 'AIVISIONAIRESULTSINDEX';
--select idx_name, idx_table, idx_text_name from ctx_user_indexes;
--select token_text from dr$aivisionresultsindex$i;
/


CREATE OR REPLACE FUNCTION VISIONAI_RESULTS_TEXT_SEARCH(p_sql IN VARCHAR2) RETURN SYS_REFCURSOR AS refcursor SYS_REFCURSOR;
BEGIN
Expand Down Expand Up @@ -50,10 +50,10 @@ dbms_cloud.create_credential (
);
END;

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


CREATE OR REPLACE FUNCTION VISIONAI_TEXTDETECTION (
Expand Down
7 changes: 2 additions & 5 deletions sql/create_aiuser.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
--run as admin

CREATE USER aiuser identified BY [Yourpassword];
grant CREATE session TO aiuser;
grant RESOURCE, db_developer_role TO aiuser;
grant RESOURCE TO aiuser;
grant unlimited tablespace TO aiuser;
grant EXECUTE ON javascript TO aiuser;
grant EXECUTE dynamic mle TO aiuser;
grant execute on DBMS_CLOUD to aiuser;

61 changes: 42 additions & 19 deletions src/main/java/oracleai/GenerateAPictureStoryUsingOnlySpeech.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package oracleai;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import oracleai.services.ImageGeneration;
import oracleai.services.OracleObjectStore;
import oracleai.services.OracleSpeechAI;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
Expand All @@ -11,6 +14,7 @@
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/picturestory")
Expand All @@ -26,25 +30,32 @@ public String reset(Model model) {
}

@PostMapping("/picturestory")
public String picturestory(@RequestParam("genopts") String genopts,
@RequestParam("file") MultipartFile multipartFile, Model model) throws Exception {
public String picturestory(@RequestParam("opts") String opts,
@RequestParam("genopts") String genopts,
@RequestParam("file") MultipartFile multipartFile,
Model model) throws Exception {
if (opts.equals("fileaudio") ) return fileaudio(genopts, multipartFile, model);
else return liveaudio(genopts, model);
}

@NotNull
private String fileaudio(String genopts, MultipartFile multipartFile, Model model) throws Exception {
OracleObjectStore.sendToObjectStorage(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
String transcriptionJobId = OracleSpeechAI.getTranscriptFromOCISpeech(multipartFile.getOriginalFilename());
System.out.println("transcriptionJobId: " + transcriptionJobId);
String jsonTranscriptFromObjectStorage =
OracleObjectStore.getFromObjectStorage(transcriptionJobId,
AIApplication.OBJECTSTORAGE_NAMESPACE + "_" + AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" + multipartFile.getOriginalFilename() + ".json");
AIApplication.OBJECTSTORAGE_NAMESPACE + "_" +
AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" +
multipartFile.getOriginalFilename() + ".json");
System.out.println("jsonTranscriptFromObjectStorage: " + jsonTranscriptFromObjectStorage);
// System.out.println("getFromObjectStorage: " + getFromObjectStorage("leia.m4a"));
// String pictureDescription = parse(jsonTranscriptFromObjectStorage);
String pictureDescription = "man rowing a boat through the forest";
String pictureDescription = getConcatenatedTokens(jsonTranscriptFromObjectStorage);
imageLocations.add(ImageGeneration.imagegeneration(pictureDescription + " " + genopts));
model.addAttribute("imageLocations", imageLocations.toArray(new String[0]));
return "resultswithimages";
}

@GetMapping("/picturestoryrecordlocally")
public String picturestoryrecordlocally(@RequestParam("genopts") String genopts, Model model) throws Exception {
public String liveaudio(String genopts, Model model) throws Exception {
AudioFormat format =
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0f, 16, 1,
(16 / 8) * 1, 44100.0f, true);
Expand All @@ -65,24 +76,33 @@ public String picturestoryrecordlocally(@RequestParam("genopts") String genopts,
AudioSystem.write(audioInputStream, fileType, file);
System.out.println("Saved " + file.getAbsolutePath());
OracleObjectStore.sendToObjectStorage(file.getName(), new FileInputStream(file));

String transcriptionJobId = OracleSpeechAI.getTranscriptFromOCISpeech(file.getName()); //json file
// String transcriptionJobId = getTranscriptFromOCISpeech("testing123.wav");
String transcriptionJobId = OracleSpeechAI.getTranscriptFromOCISpeech(file.getName());
System.out.println("transcriptionJobId: " + transcriptionJobId);
String jsonTranscriptFromObjectStorage =
OracleObjectStore.getFromObjectStorage(transcriptionJobId,
// AIApplication.OBJECTSTORAGE_NAMESPACE + "_" + AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" + "testing123.wav" + ".json"));
AIApplication.OBJECTSTORAGE_NAMESPACE + "_" + AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" + file.getName() + ".json");
AIApplication.OBJECTSTORAGE_NAMESPACE + "_" +
AIApplication.OBJECTSTORAGE_BUCKETNAME + "_" + file.getName() + ".json");
System.out.println("jsonTranscriptFromObjectStorage: " + jsonTranscriptFromObjectStorage);
// System.out.println("getFromObjectStorage: " + getFromObjectStorage("leia.m4a"));
// String pictureDescription = parse(jsonTranscriptFromObjectStorage);
String pictureDescription = "man rowing a boat through the forest";
String pictureDescription = getConcatenatedTokens(jsonTranscriptFromObjectStorage);
imageLocations.add(ImageGeneration.imagegeneration(pictureDescription + " " + genopts));
model.addAttribute("imageLocations", imageLocations.toArray(new String[0]));
return "resultswithimages";
}


public String getConcatenatedTokens(String json) {
ObjectMapper objectMapper = new ObjectMapper();
try {
OracleSpeechAI.TranscriptionResponse response =
objectMapper.readValue(json, OracleSpeechAI.TranscriptionResponse.class);
return response.getTranscriptions().stream()
.flatMap(transcription -> transcription.getTokens().stream())
.map(OracleSpeechAI.TranscriptionResponse.Transcription.Token::getToken)
.collect(Collectors.joining(" "));
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}

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

@Override
public void run() {
try (final ByteArrayOutputStream out = new ByteArrayOutputStream(); final TargetDataLine line = getTargetDataLineForRecord();) {
try (final ByteArrayOutputStream out = new ByteArrayOutputStream();
final TargetDataLine line = getTargetDataLineForRecord();) {
int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
final int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
Expand All @@ -121,7 +142,9 @@ public void run() {
}
}

public void buildByteOutputStream(final ByteArrayOutputStream out, final TargetDataLine line, int frameSizeInBytes, final int bufferLengthInBytes) throws IOException {
public void buildByteOutputStream(final ByteArrayOutputStream out,
final TargetDataLine line, int frameSizeInBytes,
final int bufferLengthInBytes) throws IOException {
final byte[] data = new byte[bufferLengthInBytes];
int numBytesRead;

Expand Down
Loading

0 comments on commit 00cf030

Please sign in to comment.