NiraIbrahem commited on
Commit ·
68eb72c
1
Parent(s): ded8bf3
Added imageurl in search
Browse files- src/main/java/edu/alexu/fitfinder/controller/FavoriteController.java +0 -1
- src/main/java/edu/alexu/fitfinder/controller/SearchController.java +14 -7
- src/main/java/edu/alexu/fitfinder/dto/SearchResponseDTO.java +1 -0
- src/main/java/edu/alexu/fitfinder/dto/SimilarItemsDTO.java +13 -0
- src/main/java/edu/alexu/fitfinder/service/SearchService.java +3 -13
src/main/java/edu/alexu/fitfinder/controller/FavoriteController.java
CHANGED
|
@@ -10,7 +10,6 @@ import lombok.RequiredArgsConstructor;
|
|
| 10 |
import org.springframework.http.HttpStatus;
|
| 11 |
import org.springframework.http.ResponseEntity;
|
| 12 |
import org.springframework.web.bind.annotation.*;
|
| 13 |
-
|
| 14 |
import java.util.List;
|
| 15 |
|
| 16 |
@RestController
|
|
|
|
| 10 |
import org.springframework.http.HttpStatus;
|
| 11 |
import org.springframework.http.ResponseEntity;
|
| 12 |
import org.springframework.web.bind.annotation.*;
|
|
|
|
| 13 |
import java.util.List;
|
| 14 |
|
| 15 |
@RestController
|
src/main/java/edu/alexu/fitfinder/controller/SearchController.java
CHANGED
|
@@ -2,12 +2,13 @@ package edu.alexu.fitfinder.controller;
|
|
| 2 |
|
| 3 |
import edu.alexu.fitfinder.dto.ItemDTO;
|
| 4 |
import edu.alexu.fitfinder.dto.SearchRequestDTO;
|
|
|
|
|
|
|
| 5 |
import edu.alexu.fitfinder.repository.ItemRepo;
|
| 6 |
import edu.alexu.fitfinder.service.JwtService;
|
| 7 |
import edu.alexu.fitfinder.service.SearchService;
|
| 8 |
import edu.alexu.fitfinder.service.StoredItemService;
|
| 9 |
import lombok.RequiredArgsConstructor;
|
| 10 |
-
import org.springframework.beans.factory.annotation.Autowired;
|
| 11 |
import org.springframework.http.HttpStatus;
|
| 12 |
import org.springframework.http.ResponseEntity;
|
| 13 |
import org.springframework.web.bind.annotation.*;
|
|
@@ -26,14 +27,20 @@ public class SearchController {
|
|
| 26 |
private final StoredItemService storedItemService;
|
| 27 |
|
| 28 |
@PostMapping("/search")
|
| 29 |
-
public ResponseEntity<?> searchByImageMask(
|
| 30 |
-
@RequestBody SearchRequestDTO searchInfo)
|
| 31 |
throws Exception {
|
| 32 |
-
|
| 33 |
try {
|
| 34 |
-
|
| 35 |
-
if (
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
} catch (IllegalArgumentException e) {
|
| 38 |
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
| 39 |
}
|
|
|
|
| 2 |
|
| 3 |
import edu.alexu.fitfinder.dto.ItemDTO;
|
| 4 |
import edu.alexu.fitfinder.dto.SearchRequestDTO;
|
| 5 |
+
import edu.alexu.fitfinder.dto.SearchResponseDTO;
|
| 6 |
+
import edu.alexu.fitfinder.dto.SimilarItemsDTO;
|
| 7 |
import edu.alexu.fitfinder.repository.ItemRepo;
|
| 8 |
import edu.alexu.fitfinder.service.JwtService;
|
| 9 |
import edu.alexu.fitfinder.service.SearchService;
|
| 10 |
import edu.alexu.fitfinder.service.StoredItemService;
|
| 11 |
import lombok.RequiredArgsConstructor;
|
|
|
|
| 12 |
import org.springframework.http.HttpStatus;
|
| 13 |
import org.springframework.http.ResponseEntity;
|
| 14 |
import org.springframework.web.bind.annotation.*;
|
|
|
|
| 27 |
private final StoredItemService storedItemService;
|
| 28 |
|
| 29 |
@PostMapping("/search")
|
| 30 |
+
public ResponseEntity<?> searchByImageMask(@RequestBody SearchRequestDTO searchInfo)
|
|
|
|
| 31 |
throws Exception {
|
|
|
|
| 32 |
try {
|
| 33 |
+
SearchResponseDTO searchResponse = searchService.getSimilarIndices(searchInfo);
|
| 34 |
+
if (searchResponse.getResults() == null || searchResponse.getResults().length == 0)
|
| 35 |
+
return ResponseEntity.ok().body(new LinkedList<>());
|
| 36 |
+
LinkedList<Long> vectorIds = new LinkedList<>();
|
| 37 |
+
for (SearchResponseDTO.Result result : searchResponse.getResults())
|
| 38 |
+
vectorIds.add(result.getFaiss_id());
|
| 39 |
+
return ResponseEntity.ok()
|
| 40 |
+
.body(
|
| 41 |
+
new SimilarItemsDTO(
|
| 42 |
+
searchResponse.getSegmented_image_url(),
|
| 43 |
+
storedItemService.getProductsByVectorIds(vectorIds)));
|
| 44 |
} catch (IllegalArgumentException e) {
|
| 45 |
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
| 46 |
}
|
src/main/java/edu/alexu/fitfinder/dto/SearchResponseDTO.java
CHANGED
|
@@ -20,4 +20,5 @@ public class SearchResponseDTO {
|
|
| 20 |
|
| 21 |
String job_id;
|
| 22 |
Result[] results;
|
|
|
|
| 23 |
}
|
|
|
|
| 20 |
|
| 21 |
String job_id;
|
| 22 |
Result[] results;
|
| 23 |
+
String segmented_image_url ;
|
| 24 |
}
|
src/main/java/edu/alexu/fitfinder/dto/SimilarItemsDTO.java
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package edu.alexu.fitfinder.dto;
|
| 2 |
+
|
| 3 |
+
import lombok.*;
|
| 4 |
+
import java.util.List;
|
| 5 |
+
|
| 6 |
+
@AllArgsConstructor
|
| 7 |
+
@NoArgsConstructor
|
| 8 |
+
@Getter
|
| 9 |
+
@Setter
|
| 10 |
+
public class SimilarItemsDTO {
|
| 11 |
+
String segmented_image_url;
|
| 12 |
+
List<ItemDTO> items;
|
| 13 |
+
}
|
src/main/java/edu/alexu/fitfinder/service/SearchService.java
CHANGED
|
@@ -9,8 +9,6 @@ import org.springframework.http.MediaType;
|
|
| 9 |
import org.springframework.stereotype.Service;
|
| 10 |
import org.springframework.web.reactive.function.client.WebClient;
|
| 11 |
import edu.alexu.fitfinder.dto.SearchResponseDTO;
|
| 12 |
-
import java.util.LinkedList;
|
| 13 |
-
import java.util.List;
|
| 14 |
|
| 15 |
@Service
|
| 16 |
public class SearchService {
|
|
@@ -27,7 +25,7 @@ public class SearchService {
|
|
| 27 |
this.webClient = WebClient.builder().baseUrl("https://fitfinder-ai-service.hf.space").build();
|
| 28 |
}
|
| 29 |
|
| 30 |
-
public
|
| 31 |
throws InvalidInputException, Exception {
|
| 32 |
|
| 33 |
// Validate mask
|
|
@@ -60,14 +58,6 @@ public class SearchService {
|
|
| 60 |
|
| 61 |
// Validate response
|
| 62 |
if (response == null) throw new Exception();
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
LinkedList<Long> vectorIds = new LinkedList<>();
|
| 66 |
-
if (response.getResults() == null || response.getResults().length == 0) return vectorIds;
|
| 67 |
-
|
| 68 |
-
// Extract Vector Ids
|
| 69 |
-
for (SearchResponseDTO.Result result : response.getResults())
|
| 70 |
-
vectorIds.add(result.getFaiss_id());
|
| 71 |
-
return vectorIds;
|
| 72 |
-
}
|
| 73 |
}
|
|
|
|
| 9 |
import org.springframework.stereotype.Service;
|
| 10 |
import org.springframework.web.reactive.function.client.WebClient;
|
| 11 |
import edu.alexu.fitfinder.dto.SearchResponseDTO;
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@Service
|
| 14 |
public class SearchService {
|
|
|
|
| 25 |
this.webClient = WebClient.builder().baseUrl("https://fitfinder-ai-service.hf.space").build();
|
| 26 |
}
|
| 27 |
|
| 28 |
+
public SearchResponseDTO getSimilarIndices(SearchRequestDTO searchInfo)
|
| 29 |
throws InvalidInputException, Exception {
|
| 30 |
|
| 31 |
// Validate mask
|
|
|
|
| 58 |
|
| 59 |
// Validate response
|
| 60 |
if (response == null) throw new Exception();
|
| 61 |
+
return response;
|
| 62 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|