Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,195 +1,195 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
import torch
|
| 5 |
-
from typing import Optional
|
| 6 |
-
import uvicorn
|
| 7 |
-
|
| 8 |
-
# ===============================
|
| 9 |
-
# FastAPI ์ฑ ์ด๊ธฐํ
|
| 10 |
-
# ===============================
|
| 11 |
-
app = FastAPI(
|
| 12 |
-
title="Gemma ํ์ธํ๋ ๋ชจ๋ธ API",
|
| 13 |
-
description="ํ์ธํ๋๋ Gemma ๋ชจ๋ธ์ ํตํด ์ง๋ฌธ์ ๋ต๋ณํ๋ API",
|
| 14 |
-
version="1.0.0"
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
# ===============================
|
| 18 |
-
# ์ ์ญ ๋ณ์ (๋ชจ๋ธ ๋ก๋)
|
| 19 |
-
# ===============================
|
| 20 |
-
MODEL_PATH = "./gemma2-finetuned"
|
| 21 |
-
tokenizer = None
|
| 22 |
-
model = None
|
| 23 |
-
device = "cpu" # Hugging Face Spaces๋ ๊ธฐ๋ณธ์ ์ผ๋ก CPU
|
| 24 |
-
|
| 25 |
-
# ===============================
|
| 26 |
-
# ์์ฒญ/์๋ต ๋ชจ๋ธ ์ ์
|
| 27 |
-
# ===============================
|
| 28 |
-
class QuestionRequest(BaseModel):
|
| 29 |
-
question: str
|
| 30 |
-
max_tokens: Optional[int] = 120
|
| 31 |
-
temperature: Optional[float] = 0.3
|
| 32 |
-
top_k: Optional[int] = 40
|
| 33 |
-
top_p: Optional[float] = 0.8
|
| 34 |
-
repetition_penalty: Optional[float] = 1.8
|
| 35 |
-
|
| 36 |
-
class Config:
|
| 37 |
-
json_schema_extra = {
|
| 38 |
-
"example": {
|
| 39 |
-
"question": "์ค๋ต๋
ธํธ๋ ์ด๋ป๊ฒ ์ ๋ฆฌํ ๊น์?",
|
| 40 |
-
"max_tokens": 120,
|
| 41 |
-
"temperature": 0.3
|
| 42 |
-
}
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
class AnswerResponse(BaseModel):
|
| 46 |
-
question: str
|
| 47 |
-
answer: str
|
| 48 |
-
full_output: str
|
| 49 |
-
model_path: str
|
| 50 |
-
device: str
|
| 51 |
-
|
| 52 |
-
class HealthResponse(BaseModel):
|
| 53 |
-
status: str
|
| 54 |
-
model_loaded: bool
|
| 55 |
-
device: str
|
| 56 |
-
|
| 57 |
-
# ===============================
|
| 58 |
-
# ์์ ์ ๋ชจ๋ธ ๋ก๋
|
| 59 |
-
# ===============================
|
| 60 |
-
@app.on_event("startup")
|
| 61 |
-
async def load_model():
|
| 62 |
-
"""์๋ฒ ์์ ์ ๋ชจ๋ธ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋"""
|
| 63 |
-
global tokenizer, model
|
| 64 |
-
|
| 65 |
-
print("๐ ๋ชจ๋ธ ๋ก๋ ์ค...")
|
| 66 |
-
try:
|
| 67 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 68 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_PATH)
|
| 69 |
-
model = model.to(device)
|
| 70 |
-
model.eval() # ํ๊ฐ ๋ชจ๋๋ก ์ค์
|
| 71 |
-
print(f"โ
๋ชจ๋ธ ๋ก๋ ์๋ฃ! (Device: {device})")
|
| 72 |
-
except Exception as e:
|
| 73 |
-
print(f"โ ๋ชจ๋ธ ๋ก๋ ์คํจ: {e}")
|
| 74 |
-
raise
|
| 75 |
-
|
| 76 |
-
# ===============================
|
| 77 |
-
# API ์๋ํฌ์ธํธ
|
| 78 |
-
# ===============================
|
| 79 |
-
|
| 80 |
-
@app.get("/", response_model=dict)
|
| 81 |
-
async def root():
|
| 82 |
-
"""๋ฃจํธ ์๋ํฌ์ธํธ - API ์ ๋ณด"""
|
| 83 |
-
return {
|
| 84 |
-
"message": "Gemma ํ์ธํ๋ ๋ชจ๋ธ API ์๋ฒ",
|
| 85 |
-
"version": "1.0.0",
|
| 86 |
-
"description": "์ํ์ Q&A ๋ต๋ณ ๋ชจ๋ธ",
|
| 87 |
-
"endpoints": {
|
| 88 |
-
"health": "GET /health - ์๋ฒ ์ํ ํ์ธ",
|
| 89 |
-
"predict": "POST /predict - ์ง๋ฌธ์ ๋ต๋ณ",
|
| 90 |
-
"docs": "GET /docs - API ๋ฌธ์"
|
| 91 |
-
},
|
| 92 |
-
"usage": {
|
| 93 |
-
"example": {
|
| 94 |
-
"method": "POST",
|
| 95 |
-
"url": "/predict",
|
| 96 |
-
"body": {
|
| 97 |
-
"question": "์ค๋ต๋
ธํธ๋ ์ด๋ป๊ฒ ์ ๋ฆฌํ ๊น์?"
|
| 98 |
-
}
|
| 99 |
-
}
|
| 100 |
-
}
|
| 101 |
-
}
|
| 102 |
-
|
| 103 |
-
@app.get("/health", response_model=HealthResponse)
|
| 104 |
-
async def health_check():
|
| 105 |
-
"""์๋ฒ ์ํ ํ์ธ"""
|
| 106 |
-
return HealthResponse(
|
| 107 |
-
status="healthy" if model is not None else "model_not_loaded",
|
| 108 |
-
model_loaded=model is not None,
|
| 109 |
-
device=device
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
@app.post("/predict", response_model=AnswerResponse)
|
| 113 |
-
async def predict(request: QuestionRequest):
|
| 114 |
-
"""
|
| 115 |
-
์ง๋ฌธ์ ๋ํ ๋ต๋ณ ์์ฑ
|
| 116 |
-
|
| 117 |
-
- **question**: ๋ต๋ณ์ ์ํ๋ ์ง๋ฌธ
|
| 118 |
-
- **max_tokens**: ์์ฑํ ์ต๋ ํ ํฐ ์ (๊ธฐ๋ณธ๊ฐ: 120)
|
| 119 |
-
- **temperature**: ์์ฑ ๋ค์์ฑ ์กฐ์ (๊ธฐ๋ณธ๊ฐ: 0.3, ๋ฎ์์๋ก ๋ณด์์ )
|
| 120 |
-
- **top_k**: ์์ k๊ฐ ํ ํฐ๋ง ๊ณ ๋ ค (๊ธฐ๋ณธ๊ฐ: 40)
|
| 121 |
-
- **top_p**: nucleus sampling ํ๋ฅ (๊ธฐ๋ณธ๊ฐ: 0.8)
|
| 122 |
-
- **repetition_penalty**: ๋ฐ๋ณต ๋ฐฉ์ง ๊ฐ๋ (๊ธฐ๋ณธ๊ฐ: 1.8)
|
| 123 |
-
"""
|
| 124 |
-
if model is None or tokenizer is None:
|
| 125 |
-
raise HTTPException(status_code=503, detail="๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค")
|
| 126 |
-
|
| 127 |
-
try:
|
| 128 |
-
# ํ๋กฌํํธ ์์ฑ
|
| 129 |
-
prompt = f"์ง๋ฌธ: {request.question}\n๋ต๋ณ:"
|
| 130 |
-
|
| 131 |
-
# ํ ํฌ๋์ด์ง
|
| 132 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 133 |
-
inputs = inputs.to(device)
|
| 134 |
-
|
| 135 |
-
# ๋ต๋ณ ์์ฑ
|
| 136 |
-
with torch.no_grad():
|
| 137 |
-
outputs = model.generate(
|
| 138 |
-
**inputs,
|
| 139 |
-
max_new_tokens=request.max_tokens,
|
| 140 |
-
temperature=request.temperature,
|
| 141 |
-
top_k=request.top_k,
|
| 142 |
-
top_p=request.top_p,
|
| 143 |
-
do_sample=True,
|
| 144 |
-
repetition_penalty=request.repetition_penalty,
|
| 145 |
-
no_repeat_ngram_size=4,
|
| 146 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 147 |
-
pad_token_id=tokenizer.pad_token_id,
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
# ๊ฒฐ๊ณผ ๋์ฝ๋ฉ
|
| 151 |
-
full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 152 |
-
|
| 153 |
-
# ๋ต๋ณ ๋ถ๋ถ๋ง ์ถ์ถ
|
| 154 |
-
answer_only = full_output
|
| 155 |
-
if "๋ต๋ณ:" in full_output:
|
| 156 |
-
answer_only = full_output.split("๋ต๋ณ:", 1)[1].strip()
|
| 157 |
-
|
| 158 |
-
# ๋ค์ ์ง๋ฌธ์ด ๋์ค๋ฉด ๊ทธ ์ ๊น์ง๋ง ์ถ์ถ
|
| 159 |
-
if "์ง๋ฌธ:" in answer_only:
|
| 160 |
-
answer_only = answer_only.split("์ง๋ฌธ:", 1)[0].strip()
|
| 161 |
-
|
| 162 |
-
# HTML ํ๊ทธ๋ ์ด์ํ ๋ฌธ์ ์ ๊ฑฐ
|
| 163 |
-
if "<" in answer_only:
|
| 164 |
-
answer_only = answer_only.split("<", 1)[0].strip()
|
| 165 |
-
|
| 166 |
-
return AnswerResponse(
|
| 167 |
-
question=request.question,
|
| 168 |
-
answer=answer_only,
|
| 169 |
-
full_output=full_output,
|
| 170 |
-
model_path=MODEL_PATH,
|
| 171 |
-
device=device
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
except Exception as e:
|
| 175 |
-
raise HTTPException(status_code=500, detail=f"๋ต๋ณ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 176 |
-
|
| 177 |
-
# ===============================
|
| 178 |
-
# ์๋ฒ ์คํ
|
| 179 |
-
# ===============================
|
| 180 |
-
if __name__ == "__main__":
|
| 181 |
-
print("="*70)
|
| 182 |
-
print("๐ Gemma ํ์ธํ๋ ๋ชจ๋ธ API ์๋ฒ ์์")
|
| 183 |
-
print("="*70)
|
| 184 |
-
print(f"๐ ์๋ฒ ์ฃผ์: http://0.0.0.0:7860")
|
| 185 |
-
print(f"๐ API ๋ฌธ์: http://0.0.0.0:7860/docs")
|
| 186 |
-
print(f"๐ง Device: {device}")
|
| 187 |
-
print("="*70)
|
| 188 |
-
|
| 189 |
-
uvicorn.run(
|
| 190 |
-
app,
|
| 191 |
-
host="0.0.0.0",
|
| 192 |
-
port=7860, # Hugging Face Spaces๋ 7860 ํฌํธ ์ฌ์ฉ
|
| 193 |
-
log_level="info"
|
| 194 |
-
)
|
| 195 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
+
from typing import Optional
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
# ===============================
|
| 9 |
+
# FastAPI ์ฑ ์ด๊ธฐํ
|
| 10 |
+
# ===============================
|
| 11 |
+
app = FastAPI(
|
| 12 |
+
title="Gemma ํ์ธํ๋ ๋ชจ๋ธ API",
|
| 13 |
+
description="ํ์ธํ๋๋ Gemma ๋ชจ๋ธ์ ํตํด ์ง๋ฌธ์ ๋ต๋ณํ๋ API",
|
| 14 |
+
version="1.0.0"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# ===============================
|
| 18 |
+
# ์ ์ญ ๋ณ์ (๋ชจ๋ธ ๋ก๋)
|
| 19 |
+
# ===============================
|
| 20 |
+
MODEL_PATH = "./gemma2-finetuned"
|
| 21 |
+
tokenizer = None
|
| 22 |
+
model = None
|
| 23 |
+
device = "cpu" # Hugging Face Spaces๋ ๊ธฐ๋ณธ์ ์ผ๋ก CPU
|
| 24 |
+
|
| 25 |
+
# ===============================
|
| 26 |
+
# ์์ฒญ/์๋ต ๋ชจ๋ธ ์ ์
|
| 27 |
+
# ===============================
|
| 28 |
+
class QuestionRequest(BaseModel):
|
| 29 |
+
question: str
|
| 30 |
+
max_tokens: Optional[int] = 120
|
| 31 |
+
temperature: Optional[float] = 0.3
|
| 32 |
+
top_k: Optional[int] = 40
|
| 33 |
+
top_p: Optional[float] = 0.8
|
| 34 |
+
repetition_penalty: Optional[float] = 1.8
|
| 35 |
+
|
| 36 |
+
class Config:
|
| 37 |
+
json_schema_extra = {
|
| 38 |
+
"example": {
|
| 39 |
+
"question": "์ค๋ต๋
ธํธ๋ ์ด๋ป๊ฒ ์ ๋ฆฌํ ๊น์?",
|
| 40 |
+
"max_tokens": 120,
|
| 41 |
+
"temperature": 0.3
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
class AnswerResponse(BaseModel):
|
| 46 |
+
question: str
|
| 47 |
+
answer: str
|
| 48 |
+
full_output: str
|
| 49 |
+
model_path: str
|
| 50 |
+
device: str
|
| 51 |
+
|
| 52 |
+
class HealthResponse(BaseModel):
|
| 53 |
+
status: str
|
| 54 |
+
model_loaded: bool
|
| 55 |
+
device: str
|
| 56 |
+
|
| 57 |
+
# ===============================
|
| 58 |
+
# ์์ ์ ๋ชจ๋ธ ๋ก๋
|
| 59 |
+
# ===============================
|
| 60 |
+
@app.on_event("startup")
|
| 61 |
+
async def load_model():
|
| 62 |
+
"""์๋ฒ ์์ ์ ๋ชจ๋ธ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋"""
|
| 63 |
+
global tokenizer, model
|
| 64 |
+
|
| 65 |
+
print("๐ ๋ชจ๋ธ ๋ก๋ ์ค...")
|
| 66 |
+
try:
|
| 67 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 68 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 69 |
+
model = model.to(device)
|
| 70 |
+
model.eval() # ํ๊ฐ ๋ชจ๋๋ก ์ค์
|
| 71 |
+
print(f"โ
๋ชจ๋ธ ๋ก๋ ์๋ฃ! (Device: {device})")
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"โ ๋ชจ๋ธ ๋ก๋ ์คํจ: {e}")
|
| 74 |
+
raise
|
| 75 |
+
|
| 76 |
+
# ===============================
|
| 77 |
+
# API ์๋ํฌ์ธํธ
|
| 78 |
+
# ===============================
|
| 79 |
+
|
| 80 |
+
@app.get("/", response_model=dict)
|
| 81 |
+
async def root():
|
| 82 |
+
"""๋ฃจํธ ์๋ํฌ์ธํธ - API ์ ๋ณด"""
|
| 83 |
+
return {
|
| 84 |
+
"message": "Gemma ํ์ธํ๋ ๋ชจ๋ธ API ์๋ฒ",
|
| 85 |
+
"version": "1.0.0",
|
| 86 |
+
"description": "์ํ์ Q&A ๋ต๋ณ ๋ชจ๋ธ",
|
| 87 |
+
"endpoints": {
|
| 88 |
+
"health": "GET /health - ์๋ฒ ์ํ ํ์ธ",
|
| 89 |
+
"predict": "POST /predict - ์ง๋ฌธ์ ๋ต๋ณ",
|
| 90 |
+
"docs": "GET /docs - API ๋ฌธ์"
|
| 91 |
+
},
|
| 92 |
+
"usage": {
|
| 93 |
+
"example": {
|
| 94 |
+
"method": "POST",
|
| 95 |
+
"url": "/predict",
|
| 96 |
+
"body": {
|
| 97 |
+
"question": "์ค๋ต๋
ธํธ๋ ์ด๋ป๊ฒ ์ ๋ฆฌํ ๊น์?"
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
@app.get("/health", response_model=HealthResponse)
|
| 104 |
+
async def health_check():
|
| 105 |
+
"""์๋ฒ ์ํ ํ์ธ"""
|
| 106 |
+
return HealthResponse(
|
| 107 |
+
status="healthy" if model is not None else "model_not_loaded",
|
| 108 |
+
model_loaded=model is not None,
|
| 109 |
+
device=device
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
@app.post("/predict", response_model=AnswerResponse)
|
| 113 |
+
async def predict(request: QuestionRequest):
|
| 114 |
+
"""
|
| 115 |
+
์ง๋ฌธ์ ๋ํ ๋ต๋ณ ์์ฑ
|
| 116 |
+
|
| 117 |
+
- **question**: ๋ต๋ณ์ ์ํ๋ ์ง๋ฌธ
|
| 118 |
+
- **max_tokens**: ์์ฑํ ์ต๋ ํ ํฐ ์ (๊ธฐ๋ณธ๊ฐ: 120)
|
| 119 |
+
- **temperature**: ์์ฑ ๋ค์์ฑ ์กฐ์ (๊ธฐ๋ณธ๊ฐ: 0.3, ๋ฎ์์๋ก ๋ณด์์ )
|
| 120 |
+
- **top_k**: ์์ k๊ฐ ํ ํฐ๋ง ๊ณ ๋ ค (๊ธฐ๋ณธ๊ฐ: 40)
|
| 121 |
+
- **top_p**: nucleus sampling ํ๋ฅ (๊ธฐ๋ณธ๊ฐ: 0.8)
|
| 122 |
+
- **repetition_penalty**: ๋ฐ๋ณต ๋ฐฉ์ง ๊ฐ๋ (๊ธฐ๋ณธ๊ฐ: 1.8)
|
| 123 |
+
"""
|
| 124 |
+
if model is None or tokenizer is None:
|
| 125 |
+
raise HTTPException(status_code=503, detail="๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค")
|
| 126 |
+
|
| 127 |
+
try:
|
| 128 |
+
# ํ๋กฌํํธ ์์ฑ
|
| 129 |
+
prompt = f"์ง๋ฌธ: {request.question}\n๋ต๋ณ:"
|
| 130 |
+
|
| 131 |
+
# ํ ํฌ๋์ด์ง
|
| 132 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 133 |
+
inputs = inputs.to(device)
|
| 134 |
+
|
| 135 |
+
# ๋ต๋ณ ์์ฑ
|
| 136 |
+
with torch.no_grad():
|
| 137 |
+
outputs = model.generate(
|
| 138 |
+
**inputs,
|
| 139 |
+
max_new_tokens=request.max_tokens,
|
| 140 |
+
temperature=request.temperature,
|
| 141 |
+
top_k=request.top_k,
|
| 142 |
+
top_p=request.top_p,
|
| 143 |
+
do_sample=True,
|
| 144 |
+
repetition_penalty=request.repetition_penalty,
|
| 145 |
+
no_repeat_ngram_size=4,
|
| 146 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 147 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# ๊ฒฐ๊ณผ ๋์ฝ๋ฉ
|
| 151 |
+
full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 152 |
+
|
| 153 |
+
# ๋ต๋ณ ๋ถ๋ถ๋ง ์ถ์ถ
|
| 154 |
+
answer_only = full_output
|
| 155 |
+
if "๋ต๋ณ:" in full_output:
|
| 156 |
+
answer_only = full_output.split("๋ต๋ณ:", 1)[1].strip()
|
| 157 |
+
|
| 158 |
+
# ๋ค์ ์ง๋ฌธ์ด ๋์ค๋ฉด ๊ทธ ์ ๊น์ง๋ง ์ถ์ถ
|
| 159 |
+
if "์ง๋ฌธ:" in answer_only:
|
| 160 |
+
answer_only = answer_only.split("์ง๋ฌธ:", 1)[0].strip()
|
| 161 |
+
|
| 162 |
+
# HTML ํ๊ทธ๋ ์ด์ํ ๋ฌธ์ ์ ๊ฑฐ
|
| 163 |
+
if "<" in answer_only:
|
| 164 |
+
answer_only = answer_only.split("<", 1)[0].strip()
|
| 165 |
+
|
| 166 |
+
return AnswerResponse(
|
| 167 |
+
question=request.question,
|
| 168 |
+
answer=answer_only,
|
| 169 |
+
full_output=full_output,
|
| 170 |
+
model_path=MODEL_PATH,
|
| 171 |
+
device=device
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
except Exception as e:
|
| 175 |
+
raise HTTPException(status_code=500, detail=f"๋ต๋ณ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 176 |
+
|
| 177 |
+
# ===============================
|
| 178 |
+
# ์๋ฒ ์คํ
|
| 179 |
+
# ===============================
|
| 180 |
+
if __name__ == "__main__":
|
| 181 |
+
print("="*70)
|
| 182 |
+
print("๐ Gemma ํ์ธํ๋ ๋ชจ๋ธ API ์๋ฒ ์์")
|
| 183 |
+
print("="*70)
|
| 184 |
+
print(f"๐ ์๋ฒ ์ฃผ์: http://0.0.0.0:7860")
|
| 185 |
+
print(f"๐ API ๋ฌธ์: http://0.0.0.0:7860/docs")
|
| 186 |
+
print(f"๐ง Device: {device}")
|
| 187 |
+
print("="*70)
|
| 188 |
+
|
| 189 |
+
uvicorn.run(
|
| 190 |
+
app,
|
| 191 |
+
host="0.0.0.0",
|
| 192 |
+
port=7860, # Hugging Face Spaces๋ 7860 ํฌํธ ์ฌ์ฉ
|
| 193 |
+
log_level="info"
|
| 194 |
+
)
|
| 195 |
+
|