Skip to content

Commit

Permalink
Fix signature of converse endpoint in client; we now produce an octet…
Browse files Browse the repository at this point in the history
… stream of audio data. Other minor adjustments to UploadView.
  • Loading branch information
pacphi committed Dec 20, 2024
1 parent 0570bc1 commit a286b67
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
17 changes: 5 additions & 12 deletions src/main/java/org/cftoolsuite/client/SanfordClient.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.cftoolsuite.client;

import java.io.IOException;
import java.util.List;

import org.cftoolsuite.domain.FileMetadata;
import org.cftoolsuite.domain.chat.AudioResponse;
import org.cftoolsuite.domain.chat.Inquiry;
Expand All @@ -14,15 +11,11 @@
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@FeignClient(name = "sanford-client", url = "${document.service.url}")
public interface SanfordClient {

Expand All @@ -35,8 +28,8 @@ public interface SanfordClient {
@PostMapping("/api/fetch")
public ResponseEntity<FetchResponse> fetchUrls(@RequestBody FetchRequest request);

@PostMapping("/api/converse")
public ResponseEntity<AudioResponse> converse(@RequestParam("file") MultipartFile file) throws IOException;
@PostMapping(value = "/api/converse", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<AudioResponse> converse(@RequestBody byte[] audioBytes);

@PostMapping("/api/chat")
public ResponseEntity<String> chat(@RequestBody Inquiry inquiry);
Expand Down
20 changes: 3 additions & 17 deletions src/main/java/org/cftoolsuite/ui/view/ConverseView.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import elemental.json.JsonObject;
import org.apache.commons.io.FileUtils;
import org.cftoolsuite.client.SanfordClient;
import org.cftoolsuite.domain.AppProperties;
import org.cftoolsuite.domain.CustomMultipartFile;
Expand All @@ -21,7 +20,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;

@PageTitle("sanford-ui » Converse")
Expand Down Expand Up @@ -131,23 +131,12 @@ private void handleRecordingResult(JsonObject result) {
String base64Audio = result.getString("audioData");
byte[] audioData = Base64.getDecoder().decode(base64Audio);

// Create temporary file
File tempFile = File.createTempFile("audio-", ".webm");
FileUtils.writeByteArrayToFile(tempFile, audioData);

// Convert to MultipartFile
MultipartFile multipartFile = new CustomMultipartFile(
"file",
"recording.webm",
"audio/webm",
FileUtils.readFileToByteArray(tempFile)
);

// Show loading indicator
UI.getCurrent().access(() -> loadingDiv.getStyle().set("display", "flex"));

// Make API call using Feign client
var response = sanfordClient.converse(multipartFile);
var response = sanfordClient.converse(audioData);
if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
// Update UI with response
UI.getCurrent().access(() -> {
Expand All @@ -170,9 +159,6 @@ private void handleRecordingResult(JsonObject result) {
showNotification("Error conversing with chatbot", NotificationVariant.LUMO_ERROR);
}

// Cleanup temp file
tempFile.delete();

} catch (Exception e) {
log.error("Error conversing with chatbot", e);
UI.getCurrent().access(() -> {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/cftoolsuite/ui/view/UploadView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.io.FilenameUtils;
import org.cftoolsuite.client.SanfordClient;
Expand Down Expand Up @@ -44,7 +45,7 @@ protected void setupUI() {
int numberOfSupportedContentTypes = supportedContentTypes.size() * 2;
List<String> acceptedFileExtensions = supportedContentTypes.keySet().stream().map(k -> String.format(".%s", k)).toList();
List<String> acceptedContentTypes = supportedContentTypes.values().stream().toList();
List<String> combinedList = List.of(acceptedFileExtensions, acceptedContentTypes).stream().flatMap(List::stream).toList();
List<String> combinedList = Stream.of(acceptedFileExtensions, acceptedContentTypes).flatMap(List::stream).toList();
String[] acceptedFileTypes = combinedList.toArray(new String[numberOfSupportedContentTypes]);
upload = new Upload(buffer);
upload.setDropAllowed(true);
Expand Down

0 comments on commit a286b67

Please sign in to comment.