Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

# 설정 객체를 생성합니다.
Expand Down
16 changes: 15 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 앱 및 모델 정의
Expand Down Expand Up @@ -129,4 +132,15 @@ async def get_recommendations(request: RecommendationRequest):
return RecommendationResponse(
category=main_category,
recommended_sentences=final_sentences
)
)

@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}")
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ httpx
# 설정 관리
pydantic-settings

# 폼 데이터(파일 업로드) 지원
python-multipart

# 테스트용 앱
streamlit
19 changes: 19 additions & 0 deletions stt.py
Original file line number Diff line number Diff line change
@@ -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"]
Loading