|
| 1 | +package com.webservice.algorithmchef.controller; |
| 2 | + |
| 3 | +import com.webservice.algorithmchef.dto.gemini.GeminiRecipeResponse; |
| 4 | +import com.webservice.algorithmchef.service.GeminiRecipeService; |
| 5 | +import com.webservice.algorithmchef.service.SpeechService; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.springframework.http.HttpStatus; |
| 9 | +import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.web.bind.annotation.*; |
| 11 | +import org.springframework.web.multipart.MultipartFile; |
| 12 | + |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +@Slf4j |
| 17 | +@RestController |
| 18 | +@RequestMapping("/api") |
| 19 | +@CrossOrigin(origins = "http://localhost:3000") |
| 20 | +@RequiredArgsConstructor |
| 21 | +public class SttController { |
| 22 | + |
| 23 | + private final SpeechService speechService; |
| 24 | + private final GeminiRecipeService geminiRecipeService; // 레시피 서비스 객체 |
| 25 | + |
| 26 | + @PostMapping("/stt") |
| 27 | + public ResponseEntity<?> stt( |
| 28 | + @RequestParam("audio") MultipartFile audioFile, |
| 29 | + @RequestParam("userId") String userIdStr |
| 30 | + ) { |
| 31 | + log.info("음성 기반 레시피 추천 요청 - UserId: {}, 파일크기: {} bytes", userIdStr, audioFile.getSize()); |
| 32 | + |
| 33 | + try { |
| 34 | + // 1. 음성 -> 텍스트 변환 (STT) |
| 35 | + String convertedText = speechService.stt(audioFile); |
| 36 | + |
| 37 | + if (convertedText == null || convertedText.isEmpty()) { |
| 38 | + log.warn("STT 변환 실패: 음성 인식 결과 없음"); |
| 39 | + return ResponseEntity.badRequest().body("음성을 인식하지 못했습니다. 다시 말씀해 주세요."); |
| 40 | + } |
| 41 | + |
| 42 | + log.info("STT 변환 결과: \"{}\"", convertedText); |
| 43 | + |
| 44 | + // userId String -> Long 변환 |
| 45 | + Long userId = Long.parseLong(userIdStr); |
| 46 | + |
| 47 | + // 음성 검색은 보통 '새로운 검색'이므로 excludedTitles는 빈 리스트로 전달 |
| 48 | + List<String> excludedTitles = Collections.emptyList(); |
| 49 | + |
| 50 | + // recipeservice 호출 |
| 51 | + List<GeminiRecipeResponse> recipes = geminiRecipeService.recommendCondition( |
| 52 | + userId, |
| 53 | + convertedText, |
| 54 | + excludedTitles |
| 55 | + ); |
| 56 | + |
| 57 | + log.info("음성 추천 완료 - {}개의 레시피 반환", recipes.size()); |
| 58 | + |
| 59 | + // 3. 레시피 리스트 반환 |
| 60 | + return ResponseEntity.ok(recipes); |
| 61 | + |
| 62 | + } catch (NumberFormatException e) { |
| 63 | + log.error("UserId 형식 오류: {}", userIdStr, e); |
| 64 | + return ResponseEntity.badRequest().body("유효하지 않은 사용자 ID입니다."); |
| 65 | + } catch (Exception e) { |
| 66 | + log.error("음성 레시피 추천 중 오류 발생", e); |
| 67 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 68 | + .body("처리 중 서버 오류가 발생했습니다: " + e.getMessage()); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments