Spaces:
Sleeping
Sleeping
모델 output 후처리 로직 수정
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from llama_cpp import Llama
|
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
import uvicorn
|
| 8 |
import json
|
|
|
|
| 9 |
|
| 10 |
# 1. FastAPI 앱 인스턴스 생성
|
| 11 |
app = FastAPI()
|
|
@@ -74,13 +75,27 @@ async def translate_all_in_one(request: TranslationRequest):
|
|
| 74 |
generated_output = output["choices"][0]["text"].strip()
|
| 75 |
|
| 76 |
try:
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
print(f"모델 원본 출력: {generated_output}")
|
| 83 |
-
return {"error": "Failed to parse model output as
|
| 84 |
|
| 85 |
@app.get("/")
|
| 86 |
def read_root():
|
|
|
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
import uvicorn
|
| 8 |
import json
|
| 9 |
+
import ast
|
| 10 |
|
| 11 |
# 1. FastAPI 앱 인스턴스 생성
|
| 12 |
app = FastAPI()
|
|
|
|
| 75 |
generated_output = output["choices"][0]["text"].strip()
|
| 76 |
|
| 77 |
try:
|
| 78 |
+
# 1. 모델이 생성한 텍스트에서 앞뒤의 불필요한 부분을 정리
|
| 79 |
+
# (혹시 모를 따옴표나 공백, 마크다운 코드 블록 제거)
|
| 80 |
+
clean_output = generated_output.strip().strip("'\"")
|
| 81 |
+
if clean_output.startswith("```json"):
|
| 82 |
+
clean_output = clean_output[7:]
|
| 83 |
+
if clean_output.endswith("```"):
|
| 84 |
+
clean_output = clean_output[:-3]
|
| 85 |
+
clean_output = clean_output.strip()
|
| 86 |
+
|
| 87 |
+
# 2. ast.literal_eval을 사용해 문자열을 안전하게 파이썬 딕셔너리로 변환
|
| 88 |
+
# 이것이 바로 홑따옴표 문제를 해결하는 열쇠!
|
| 89 |
+
parsed_data = ast.literal_eval(clean_output)
|
| 90 |
+
|
| 91 |
+
# 3. 성공적으로 변환된 딕셔너리를 반환 (FastAPI가 JSON으로 만들어줌)
|
| 92 |
+
return parsed_data
|
| 93 |
+
|
| 94 |
+
except (ValueError, SyntaxError) as e:
|
| 95 |
+
# ast.literal_eval이 실패하면 ValueError 또는 SyntaxError 발생
|
| 96 |
+
print(f"AST 파싱 에러: {e}")
|
| 97 |
print(f"모델 원본 출력: {generated_output}")
|
| 98 |
+
return {"error": "Failed to parse model output as a dictionary", "raw_output": generated_output}
|
| 99 |
|
| 100 |
@app.get("/")
|
| 101 |
def read_root():
|