Spaces:
Sleeping
Sleeping
/pronunciation api 추가
Browse files
app.py
CHANGED
|
@@ -28,20 +28,23 @@ pipe = pipeline(
|
|
| 28 |
# 3. 요청 본문(Request Body)의 데이터 형식을 지정
|
| 29 |
# 프론트엔드에서 "text"라는 키에 번역할 문장을 담아서 보내야 한다는 규칙.
|
| 30 |
class TranslationRequest(BaseModel):
|
| 31 |
-
|
| 32 |
-
target_lang: str # '
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
@app.post("/translate")
|
| 37 |
async def translate(request: TranslationRequest):
|
| 38 |
korean_text = request.text
|
| 39 |
target_lang = request.target_lang
|
| 40 |
|
| 41 |
# 프롬프트 엔지니어링: 모델에게 원하는 결과물을 명확하게 지시
|
| 42 |
-
if target_lang == '
|
| 43 |
prompt = f"Translate the following Korean sentence into natural, everyday English. Provide only the translated sentence, without any additional explanations or quotation marks.\n\nKorean: \"{korean_text}\"\n\nEnglish:"
|
| 44 |
-
elif target_lang == '
|
| 45 |
prompt = f"Translate the following Korean sentence into natural, everyday Japanese. Provide only the translated sentence, without any additional explanations or quotation marks.\n\nKorean: \"{korean_text}\"\n\nJapanese:"
|
| 46 |
else:
|
| 47 |
return {"error": "Invalid target language"}
|
|
@@ -65,9 +68,34 @@ async def translate(request: TranslationRequest):
|
|
| 65 |
# 'assistant\n' 다음에 오는 실제 번역 결과만 깔끔하게 잘라내기
|
| 66 |
translated_text = generated_text.split("assistant\n")[-1].strip()
|
| 67 |
|
| 68 |
-
#
|
| 69 |
return {"translated_text": translated_text}
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# 서버가 잘 작동하는지 확인하기 위한 기본 주소
|
| 72 |
@app.get("/")
|
| 73 |
def read_root():
|
|
|
|
| 28 |
# 3. 요청 본문(Request Body)의 데이터 형식을 지정
|
| 29 |
# 프론트엔드에서 "text"라는 키에 번역할 문장을 담아서 보내야 한다는 규칙.
|
| 30 |
class TranslationRequest(BaseModel):
|
| 31 |
+
korean_text: str
|
| 32 |
+
target_lang: str # 'en' 또는 'ja'
|
| 33 |
|
| 34 |
+
# 발음 변환 API를 위한 주문서 양식
|
| 35 |
+
class PronunciationRequest(BaseModel):
|
| 36 |
+
japanese_text: str
|
| 37 |
+
|
| 38 |
+
# 4. "/translate" API 엔드포인트 생성
|
| 39 |
@app.post("/translate")
|
| 40 |
async def translate(request: TranslationRequest):
|
| 41 |
korean_text = request.text
|
| 42 |
target_lang = request.target_lang
|
| 43 |
|
| 44 |
# 프롬프트 엔지니어링: 모델에게 원하는 결과물을 명확하게 지시
|
| 45 |
+
if target_lang == 'en':
|
| 46 |
prompt = f"Translate the following Korean sentence into natural, everyday English. Provide only the translated sentence, without any additional explanations or quotation marks.\n\nKorean: \"{korean_text}\"\n\nEnglish:"
|
| 47 |
+
elif target_lang == 'ja':
|
| 48 |
prompt = f"Translate the following Korean sentence into natural, everyday Japanese. Provide only the translated sentence, without any additional explanations or quotation marks.\n\nKorean: \"{korean_text}\"\n\nJapanese:"
|
| 49 |
else:
|
| 50 |
return {"error": "Invalid target language"}
|
|
|
|
| 68 |
# 'assistant\n' 다음에 오는 실제 번역 결과만 깔끔하게 잘라내기
|
| 69 |
translated_text = generated_text.split("assistant\n")[-1].strip()
|
| 70 |
|
| 71 |
+
# 프론트엔드에 번역된 텍스트를 JSON 형태로 반환
|
| 72 |
return {"translated_text": translated_text}
|
| 73 |
|
| 74 |
+
# 5. "/pronunciation" API 엔드포인트 생성
|
| 75 |
+
@app.post("/pronunciation")
|
| 76 |
+
async def get_pronunciation(request: PronunciationRequest):
|
| 77 |
+
japanese_text = request.japanese_text
|
| 78 |
+
|
| 79 |
+
# 발음 변환을 위한 새로운 프롬프트
|
| 80 |
+
prompt = f"Provide the Korean pronunciation (Hangul) for the following Japanese sentence. Provide only the Hangul pronunciation, without any other text, labels, or quotation marks.\n\nJapanese: \"{japanese_text}\"\n\nKorean Pronunciation:"
|
| 81 |
+
|
| 82 |
+
messages = [
|
| 83 |
+
{"role": "user", "content": prompt}
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
outputs = pipe(
|
| 87 |
+
messages,
|
| 88 |
+
max_new_tokens=150,
|
| 89 |
+
do_sample=True,
|
| 90 |
+
temperature=0.7,
|
| 91 |
+
top_k=50,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
generated_text = outputs[0]["generated_text"]
|
| 95 |
+
pronunciation_text = generated_text.split("assistant\n")[-1].strip()
|
| 96 |
+
|
| 97 |
+
return {"pronunciation_text": pronunciation_text}
|
| 98 |
+
|
| 99 |
# 서버가 잘 작동하는지 확인하기 위한 기본 주소
|
| 100 |
@app.get("/")
|
| 101 |
def read_root():
|