diff --git a/config.py b/config.py index 8edba04..f7e5d50 100644 --- a/config.py +++ b/config.py @@ -7,7 +7,7 @@ class Settings(BaseSettings): """ GOOGLE_API_KEY: str KAKAO_API_KEY: str - + OPENAI_API_KEY: str model_config = SettingsConfigDict(env_file=".env", extra='ignore') # 설정 객체를 생성합니다. diff --git a/main.py b/main.py index 3f79a27..a2c389d 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,9 @@ from typing import List, Optional # 타입 힌트: 리스트, Optional import httpx # 비동기 HTTP 클라이언트 (LLM API 호출용) +from fastapi import UploadFile, File # 파일 업로드 처리용 +from stt import transcribe_audio # STT 처리 함수 임포트 + from config import settings # 환경설정/비밀키를 담은 settings 객체 임포트 # FastAPI 앱 및 모델 정의 @@ -129,4 +132,15 @@ async def get_recommendations(request: RecommendationRequest): return RecommendationResponse( category=main_category, recommended_sentences=final_sentences - ) \ No newline at end of file + ) + +@app.post("/stt/transcribe", summary="음성 파일을 텍스트로 변환 (STT)") +async def stt_transcribe(file: UploadFile = File(...)): + """ + 업로드된 음성 파일을 OpenAI Whisper로 텍스트 변환합니다. + """ + try: + text = await transcribe_audio(file) + return {"transcription": text} + except Exception as e: + raise HTTPException(status_code=500, detail=f"STT 처리 중 오류: {e}") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 3d4b1eb..75bd0b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,5 +8,8 @@ httpx # 설정 관리 pydantic-settings +# 폼 데이터(파일 업로드) 지원 +python-multipart + # 테스트용 앱 streamlit \ No newline at end of file diff --git a/stt.py b/stt.py new file mode 100644 index 0000000..38fe366 --- /dev/null +++ b/stt.py @@ -0,0 +1,19 @@ +import httpx +from fastapi import UploadFile +from config import settings + +async def transcribe_audio(file: UploadFile) -> str: + """ + OpenAI Whisper API를 사용하여 음성 파일을 텍스트로 변환합니다. + """ + api_url = "https://api.openai.com/v1/audio/transcriptions" + headers = {"Authorization": f"Bearer {settings.OPENAI_API_KEY}"} + files = { + "file": (file.filename, await file.read(), file.content_type), + } + data = {"model": "whisper-1"} + + async with httpx.AsyncClient() as client: + response = await client.post(api_url, headers=headers, files=files, data=data) + response.raise_for_status() + return response.json()["text"] \ No newline at end of file