@@ -260,26 +260,31 @@ async def ocr_and_create_note(
260260
261261
262262@router .post ("/audio" )
263- async def upload_audio_file (
263+ async def upload_audio_and_transcribe (
264264 file : UploadFile = File (...),
265+ note_id : Optional [int ] = Form (None ),
266+ folder_id : Optional [int ] = Form (None ),
265267 db : Session = Depends (get_db ),
266- user = Depends (get_current_user )
268+ user = Depends (get_current_user )
267269):
268- # 파일명 및 저장 경로
270+ # 📁 저장 경로 생성
269271 timestamp = datetime .now ().strftime ("%Y%m%d_%H%M%S" )
270272 filename = f"user{ user .u_id } _{ timestamp } _{ file .filename } "
271- save_dir = os .path .join (BASE_UPLOAD_DIR , str (user .u_id ), "audio" )
273+ save_dir = os .path .join (BASE_UPLOAD_DIR , str (user .u_id ))
272274 os .makedirs (save_dir , exist_ok = True )
273275 save_path = os .path .join (save_dir , filename )
274276
275- # 저장
277+ # 📥 파일 저장
276278 with open (save_path , "wb" ) as f :
277279 f .write (await file .read ())
278280
279- # DB 기록
281+ # ✅ note_id가 있으면 folder_id는 무시
282+ folder_id_to_use = folder_id if note_id is None else None
283+
284+ # 📦 files 테이블에 기록
280285 new_file = FileModel (
281286 user_id = user .u_id ,
282- folder_id = None ,
287+ folder_id = folder_id_to_use ,
283288 original_name = filename ,
284289 saved_path = save_path ,
285290 content_type = "audio"
@@ -288,33 +293,44 @@ async def upload_audio_file(
288293 db .commit ()
289294 db .refresh (new_file )
290295
291- # Whisper로 STT 수행
296+ # 🧠 STT 처리
292297 try :
293- result = model .transcribe (save_path , language = "ko" ) # 언어 설정
298+ import whisper
299+ model = whisper .load_model ("base" )
300+ result = model .transcribe (save_path , language = "ko" )
294301 transcript = result .get ("text" , "" ).strip ()
295302 except Exception as e :
296- raise HTTPException (status_code = 500 , detail = f"STT 실패: { e } " )
303+ raise HTTPException (status_code = 500 , detail = f"STT 처리 실패: { e } " )
297304
298- if not transcript :
299- raise HTTPException (status_code = 500 , detail = "음성에서 텍스트를 추출할 수 없습니다." )
305+ # 📝 노트 처리
306+ if note_id :
307+ # 기존 노트에 텍스트 추가
308+ note = db .query (NoteModel ).filter (
309+ NoteModel .id == note_id ,
310+ NoteModel .user_id == user .u_id
311+ ).first ()
300312
301- # 노트로 저장
302- try :
313+ if not note :
314+ raise HTTPException (status_code = 404 , detail = "해당 노트를 찾을 수 없습니다." )
315+
316+ note .content = (note .content or "" ) + "\n \n " + transcript
317+ note .updated_at = datetime .utcnow ()
318+ db .commit ()
319+ db .refresh (note )
320+
321+ else :
322+ # 새 노트 생성
303323 new_note = NoteModel (
304324 user_id = user .u_id ,
305- folder_id = None , # 선택적으로 연결
306- title = f"[음성 노트] { timestamp } " ,
325+ folder_id = folder_id_to_use ,
326+ title = "녹음 텍스트 " ,
307327 content = transcript
308328 )
309329 db .add (new_note )
310330 db .commit ()
311331 db .refresh (new_note )
312- except Exception as e :
313- raise HTTPException (status_code = 500 , detail = f"노트 저장 실패: { e } " )
314332
315333 return {
316- "message" : "✅ 음성 업로드 및 노트 생성 성공" ,
317- "file_id" : new_file .id ,
318- "note_id" : new_note .id ,
334+ "message" : "STT 및 노트 저장 완료" ,
319335 "transcript" : transcript
320336 }
0 commit comments