1
1
package oracleai ;
2
2
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .databind .ObjectMapper ;
3
5
import oracleai .services .ImageGeneration ;
4
6
import oracleai .services .OracleObjectStore ;
5
7
import oracleai .services .OracleSpeechAI ;
8
+ import org .jetbrains .annotations .NotNull ;
6
9
import org .springframework .stereotype .Controller ;
7
10
import org .springframework .ui .Model ;
8
11
import org .springframework .web .bind .annotation .*;
11
14
import javax .sound .sampled .*;
12
15
import java .io .*;
13
16
import java .util .*;
17
+ import java .util .stream .Collectors ;
14
18
15
19
@ Controller
16
20
@ RequestMapping ("/picturestory" )
@@ -26,25 +30,32 @@ public String reset(Model model) {
26
30
}
27
31
28
32
@ 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 {
31
43
OracleObjectStore .sendToObjectStorage (multipartFile .getOriginalFilename (), multipartFile .getInputStream ());
32
44
String transcriptionJobId = OracleSpeechAI .getTranscriptFromOCISpeech (multipartFile .getOriginalFilename ());
33
45
System .out .println ("transcriptionJobId: " + transcriptionJobId );
34
46
String jsonTranscriptFromObjectStorage =
35
47
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" );
37
51
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 );
41
53
imageLocations .add (ImageGeneration .imagegeneration (pictureDescription + " " + genopts ));
42
54
model .addAttribute ("imageLocations" , imageLocations .toArray (new String [0 ]));
43
55
return "resultswithimages" ;
44
56
}
45
57
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 {
48
59
AudioFormat format =
49
60
new AudioFormat (AudioFormat .Encoding .PCM_SIGNED , 44100.0f , 16 , 1 ,
50
61
(16 / 8 ) * 1 , 44100.0f , true );
@@ -65,24 +76,33 @@ public String picturestoryrecordlocally(@RequestParam("genopts") String genopts,
65
76
AudioSystem .write (audioInputStream , fileType , file );
66
77
System .out .println ("Saved " + file .getAbsolutePath ());
67
78
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 ());
71
80
System .out .println ("transcriptionJobId: " + transcriptionJobId );
72
81
String jsonTranscriptFromObjectStorage =
73
82
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" );
76
85
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 );
80
87
imageLocations .add (ImageGeneration .imagegeneration (pictureDescription + " " + genopts ));
81
88
model .addAttribute ("imageLocations" , imageLocations .toArray (new String [0 ]));
82
89
return "resultswithimages" ;
83
90
}
84
91
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
+ }
86
106
87
107
public class SoundRecorder implements Runnable {
88
108
AudioInputStream audioInputStream ;
@@ -106,7 +126,8 @@ public void stop() {
106
126
107
127
@ Override
108
128
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 ();) {
110
131
int frameSizeInBytes = format .getFrameSize ();
111
132
int bufferLengthInFrames = line .getBufferSize () / 8 ;
112
133
final int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes ;
@@ -121,7 +142,9 @@ public void run() {
121
142
}
122
143
}
123
144
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 {
125
148
final byte [] data = new byte [bufferLengthInBytes ];
126
149
int numBytesRead ;
127
150
0 commit comments