File size: 3,125 Bytes
8370b61 b12512e 8370b61 b12512e 8370b61 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
"""
๋ค์ด๋ฒ ํด๋ก๋ฐ ์์ฑ์ธ์(STT) API ์ฐ๋ ๋ชจ๋
"""
import os
import json
import requests
import tempfile
# config.py์์ ์ค์ ๊ฐ์ ธ์ค๊ธฐ
from config import NAVER_CLIENT_ID, NAVER_CLIENT_SECRET
class ClovaSTT:
"""
๋ค์ด๋ฒ ํด๋ก๋ฐ ์์ฑ์ธ์(STT) API ํด๋์ค
"""
def __init__(self):
"""
ํด๋ก๋ฐ STT ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
"""
self.client_id = NAVER_CLIENT_ID
self.client_secret = NAVER_CLIENT_SECRET
# ํด๋ผ์ด์ธํธ ID์ Secret ๊ฒ์ฆ
if not self.client_id or not self.client_secret:
print("๊ฒฝ๊ณ : ๋ค์ด๋ฒ ํด๋ก๋ฐ API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
print("NAVER_CLIENT_ID์ NAVER_CLIENT_SECRET ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํด์ฃผ์ธ์.")
else:
print("๋ค์ด๋ฒ ํด๋ก๋ฐ STT API ์ค์ ์๋ฃ")
def recognize(self, audio_bytes, language="Kor"):
"""
์ค๋์ค ๋ฐ์ดํฐ๋ฅผ ํ
์คํธ๋ก ๋ณํ
Args:
audio_bytes: ์ค๋์ค ํ์ผ ๋ฐ์ดํธ ๋ฐ์ดํฐ
language: ์ธ์ด ์ฝ๋ (๊ธฐ๋ณธ๊ฐ: 'Kor')
Returns:
์ธ์๋ ํ
์คํธ ๋๋ ์ค๋ฅ ๋ฉ์์ง
"""
if not self.client_id or not self.client_secret:
return {"error": "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."}
try:
# API ์๋ํฌ์ธํธ URL
url = f"https://naveropenapi.apigw.ntruss.com/recog/v1/stt?lang={language}"
# ์์ฒญ ํค๋ ์ค์
headers = {
"X-NCP-APIGW-API-KEY-ID": self.client_id,
"X-NCP-APIGW-API-KEY": self.client_secret,
"Content-Type": "application/octet-stream"
}
print("[STT] ๋ค์ด๋ฒ ํด๋ก๋ฐ STT ์์ฒญ ์ ์ก ์ค...")
# API ์์ฒญ ์ ์ก
response = requests.post(url, headers=headers, data=audio_bytes)
# ์๋ต ์ฒ๋ฆฌ
if response.status_code == 200:
result = response.json()
print(f"[STT] ์ธ์ ์ฑ๊ณต: {result}")
return result
else:
print(f"[STT] API ์ค๋ฅ ์๋ต: {response.status_code}, {response.text}")
return {"error": f"API ์ค๋ฅ: {response.status_code}", "details": response.text}
except Exception as e:
print(f"[STT] ์์ฑ์ธ์ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
return {"error": "์์ฑ์ธ์ ์ฒ๋ฆฌ ์คํจ", "details": str(e)}
def recognize_file(self, file_path, language="Kor"):
"""
์ค๋์ค ํ์ผ์ ํ
์คํธ๋ก ๋ณํ
Args:
file_path: ์ค๋์ค ํ์ผ ๊ฒฝ๋ก
language: ์ธ์ด ์ฝ๋ (๊ธฐ๋ณธ๊ฐ: 'Kor')
Returns:
์ธ์๋ ํ
์คํธ ๋๋ ์ค๋ฅ ๋ฉ์์ง
"""
try:
with open(file_path, "rb") as f:
audio_bytes = f.read()
return self.recognize(audio_bytes, language)
except Exception as e:
print(f"[STT] ํ์ผ ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}")
return {"error": "ํ์ผ ์ฝ๊ธฐ ์คํจ", "details": str(e)} |