Skip to content

Commit

Permalink
Bugfixing of speech-to-text component
Browse files Browse the repository at this point in the history
  • Loading branch information
jschm42 committed May 6, 2024
1 parent ba7a1b6 commit f858b2a
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.talkforgeai.backend.assistant.repository.AssistantRepository;
import com.talkforgeai.backend.assistant.repository.MessageRepository;
import com.talkforgeai.backend.assistant.repository.ThreadRepository;
import com.talkforgeai.backend.service.UniqueIdGenerator;
import com.talkforgeai.backend.storage.FileStorageService;
import com.talkforgeai.backend.transformers.MessageProcessor;
import jakarta.transaction.Transactional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.talkforgeai.backend.assistant.service;
package com.talkforgeai.backend.service;

import java.security.SecureRandom;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -45,6 +45,10 @@ public String generateImageId() {
return generateUniqueId("img", 20);
}

public String generateAudioId() {
return generateUniqueId("aud", 20);
}

public String generateUniqueId(String prefix, int length) {
String randomChars = generateRandomString(length);
return prefix + "_" + randomChars;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Jean Schmitz.
* Copyright (c) 2023-2024 Jean Schmitz.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,71 +17,77 @@
package com.talkforgeai.backend.storage;

import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class FileStorageService {

public static final String TALK_FORGE_DIR = ".talkforgeai";
public static final Logger LOGGER = LoggerFactory.getLogger(FileStorageService.class);
public static final String TALK_FORGE_DIR = ".talkforgeai";
public static final Logger LOGGER = LoggerFactory.getLogger(FileStorageService.class);

@Value("${talkforgeai.datadir:}")
private String configDataDirectory;

@Value("${talkforgeai.datadir:}")
private String configDataDirectory;
private Path dataDirectory;

private Path dataDirectory;
public FileStorageService() {
}

public FileStorageService() {
@PostConstruct
private void postConstruct() {
if (configDataDirectory != null && !configDataDirectory.isEmpty()) {
this.dataDirectory = Path.of(configDataDirectory);

} else {
this.dataDirectory = Paths.get(System.getProperty("user.home"))
.resolve(TALK_FORGE_DIR)
.normalize();
}

@PostConstruct
private void postConstruct() {
if (configDataDirectory != null && !configDataDirectory.isEmpty()) {
this.dataDirectory = Path.of(configDataDirectory);
LOGGER.info("Data directory set to {}", this.dataDirectory);
}

} else {
this.dataDirectory = Paths.get(System.getProperty("user.home"))
.resolve(TALK_FORGE_DIR)
.normalize();
}
public Path getTempDirectory() {
return this.dataDirectory.resolve("temp");
}

LOGGER.info("Data directory set to {}", this.dataDirectory);
}
public Path getDataDirectory() {
return this.dataDirectory;
}

public Path getDataDirectory() {
return this.dataDirectory;
}
public Path getAssistantsDirectory() {
return getDataDirectory().resolve("assistants");
}

public Path getAssistantsDirectory() {
return getDataDirectory().resolve("assistants");
}
public Path getThreadDirectory() {
return getDataDirectory().resolve("threads");
}

public Path getThreadDirectory() {
return getDataDirectory().resolve("threads");
}
public void createDataDirectories() {
try {
Path createdPath = Files.createDirectories(getDataDirectory());
LOGGER.info("Created data directory {}", createdPath);

public void createDataDirectories() {
try {
Path createdPath = Files.createDirectories(getDataDirectory());
LOGGER.info("Created data directory {}", createdPath);
createdPath = Files.createDirectories(getThreadDirectory());
LOGGER.info("Created threads directory {}", createdPath);

createdPath = Files.createDirectories(getThreadDirectory());
LOGGER.info("Created threads directory {}", createdPath);
createdPath = Files.createDirectories(getAssistantsDirectory());
LOGGER.info("Created assistants directory {}", createdPath);

createdPath = Files.createDirectories(getAssistantsDirectory());
LOGGER.info("Created assistants directory {}", createdPath);
createdPath = Files.createDirectories(getTempDirectory());
LOGGER.info("Created temp directory {}", createdPath);

LOGGER.info("Directories created successfully");
} catch (IOException e) {
LOGGER.error("Failed to create directory: " + e.getMessage());
}
LOGGER.info("Directories created successfully");
} catch (IOException e) {
LOGGER.error("Failed to create directory: " + e.getMessage());
}
}

}
44 changes: 0 additions & 44 deletions backend/src/main/java/com/talkforgeai/backend/util/HashUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public SpeechToTextController(SpeechToTextService speechToTextService,

@PostMapping("/convert")
public ResponseEntity<String> convert(@RequestParam("file") MultipartFile file) {
return speechToTextService.convert(file,
fileStorageService.getDataDirectory().resolve("uploads"));
return speechToTextService.convert(file, fileStorageService.getTempDirectory());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.talkforgeai.backend.voice.service;

import com.talkforgeai.backend.service.UniqueIdGenerator;
import com.talkforgeai.backend.voice.dto.TranscriptionSystem;
import java.io.File;
import java.io.IOException;
Expand All @@ -39,16 +40,21 @@ public class SpeechToTextService {

private final UniversalTranscriptionService universalTranscriptionService;

public SpeechToTextService(UniversalTranscriptionService universalTranscriptionService) {
private final UniqueIdGenerator uniqueIdGenerator;

public SpeechToTextService(UniversalTranscriptionService universalTranscriptionService,
UniqueIdGenerator uniqueIdGenerator) {
this.universalTranscriptionService = universalTranscriptionService;
this.uniqueIdGenerator = uniqueIdGenerator;
}

public ResponseEntity<String> convert(MultipartFile file, Path uploadDirectory) {
public ResponseEntity<String> convert(MultipartFile file, Path tempDirectory) {
try {
Files.createDirectories(uploadDirectory);
Path path = uploadDirectory.resolve("audio.wav");
file.transferTo(path);
String text = transscribeAudio(path.toFile());
Files.createDirectories(tempDirectory);
Path audioFile = tempDirectory.resolve(uniqueIdGenerator.generateAudioId() + ".wav");
file.transferTo(audioFile);
String text = transscribeAudio(audioFile.toFile());
Files.delete(audioFile);
return ResponseEntity.ok(text);
} catch (IOException e) {
LOGGER.error("Failed to create directory or transfer file: {}", e.getMessage());
Expand Down
89 changes: 89 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f858b2a

Please sign in to comment.