Skip to content

Commit

Permalink
feat: add import
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed May 12, 2024
1 parent 6f877c2 commit 286d4e0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public ImageController(ImageMapper imageMapper, ImageService imageService) {
}

@PostMapping("/query")
public List<ImageDto> postImageQuery(@RequestParam("query") String query,@RequestParam("type") String type, @RequestParam("file") MultipartFile imageQuery) {
var result = this.imageService.queryImage(this.imageMapper.map(imageQuery, query));
public List<ImageDto> postImageQuery(@RequestParam("query") String query,@RequestParam("type") String type) {
var result = this.imageService.queryImage(query);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.imageio.ImageIO;

Expand Down Expand Up @@ -57,6 +59,9 @@ public class ImageService {
private record ResultData(String answer, ImageQueryDto imageQueryDto) {
}

private record ImageContainer(Document document, Image image, Float distance) {
}

public ImageService(ChatClient chatClient, ImageRepository imageRepository,
DocumentVsRepository documentVsRepository) {
this.chatClient = chatClient;
Expand All @@ -77,8 +82,8 @@ public ImageDto importImage(ImageQueryDto imageDto, Image image) {
resultData.imageQueryDto().getImageType());
}

public List<ImageDto> queryImage(ImageQueryDto imageDto) {
var aiDocuments = this.documentVsRepository.retrieve(imageDto.getQuery(), MetaData.DataType.IMAGE).stream()
public List<ImageDto> queryImage(String imageQuery) {
var aiDocuments = this.documentVsRepository.retrieve(imageQuery, MetaData.DataType.IMAGE).stream()
.filter(myDoc -> myDoc.getMetadata().get(MetaData.DATATYPE).equals(DataType.IMAGE.toString()))
.sorted((myDocA, myDocB) -> ((Float) myDocA.getMetadata().get(MetaData.DISTANCE))
.compareTo(((Float) myDocB.getMetadata().get(MetaData.DISTANCE))))
Expand All @@ -87,25 +92,29 @@ public List<ImageDto> queryImage(ImageQueryDto imageDto) {
.findAllById(aiDocuments.stream().map(myDoc -> (String) myDoc.getMetadata().get(MetaData.ID))
.map(myUuid -> UUID.fromString(myUuid)).toList())
.stream().collect(Collectors.toMap(myDoc -> myDoc.getId(), myDoc -> myDoc));
record Container(Document document, Image image, Float distance) {
}
return imageMap.entrySet().stream().map(myEntry -> new Container(
aiDocuments.stream()
.filter(myDoc -> myEntry.getKey().toString()
.equals((String) myDoc.getMetadata().get(MetaData.ID)))
.findFirst().orElseThrow(),
myEntry.getValue(),
aiDocuments.stream()
.filter(myDoc -> myEntry.getKey().toString()
.equals((String) myDoc.getMetadata().get(MetaData.ID)))
.map(myDoc -> (Float) myDoc.getMetadata().get(MetaData.DISTANCE)).findFirst().orElseThrow()))
return imageMap.entrySet().stream().map(myEntry -> createImageContainer(aiDocuments, myEntry))
.sorted((containerA, containerB) -> containerA.distance().compareTo(containerB.distance()))
.map(myContainer -> new ImageDto(myContainer.document().getContent(),
Base64.getEncoder().encodeToString(myContainer.image().getImageContent()),
myContainer.image().getImageType()))
.limit(this.resultSize).toList();
}

private ImageContainer createImageContainer(List<Document> aiDocuments, Entry<UUID, Image> myEntry) {
return new ImageContainer(
createIdFilteredStream(aiDocuments, myEntry)
.findFirst().orElseThrow(),
myEntry.getValue(),
createIdFilteredStream(aiDocuments, myEntry)
.map(myDoc -> (Float) myDoc.getMetadata().get(MetaData.DISTANCE)).findFirst().orElseThrow());
}

private Stream<Document> createIdFilteredStream(List<Document> aiDocuments, Entry<UUID, Image> myEntry) {
return aiDocuments.stream()
.filter(myDoc -> myEntry.getKey().toString()
.equals((String) myDoc.getMetadata().get(MetaData.ID)));
}

private ResultData createAIResult(ImageQueryDto imageDto) {
if (ImageType.JPEG.equals(imageDto.getImageType()) || ImageType.PNG.equals(imageDto.getImageType())) {
imageDto = this.resizeImage(imageDto);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/angular/src/app/service/image.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export class ImageService {
constructor(private httpClient: HttpClient) { }

public postImageForm(formData: FormData): Observable<ImageFile> {
return this.httpClient.post<ImageFile>('/rest/image/query', formData);
return this.httpClient.post<ImageFile>('/rest/image/import', formData);
}
}

0 comments on commit 286d4e0

Please sign in to comment.