|
""" |
|
๋ค์ด๋ฒ ํด๋ก๋ฐ ์์ฑ์ธ์(STT) API ์ฐ๋ ๋ชจ๋ |
|
""" |
|
import os |
|
import json |
|
import requests |
|
import tempfile |
|
|
|
|
|
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 |
|
|
|
|
|
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: |
|
|
|
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 ์์ฒญ ์ ์ก ์ค...") |
|
|
|
|
|
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)} |