Update main.py
Browse files
main.py
CHANGED
|
@@ -3,20 +3,28 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List, Optional
|
| 5 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 9 |
model_id = "google/gemma-2b-it"
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cpu", torch_dtype=torch.float32)
|
| 12 |
|
| 13 |
class Message(BaseModel):
|
| 14 |
-
role: str
|
| 15 |
content: str
|
| 16 |
|
| 17 |
class ChatRequest(BaseModel):
|
| 18 |
messages: List[Message]
|
| 19 |
-
|
|
|
|
| 20 |
temperature: Optional[float] = 0.7
|
| 21 |
|
| 22 |
API_KEY = "bazi2281488133767695242"
|
|
@@ -26,21 +34,33 @@ def chat(request: ChatRequest, x_api_key: str = Header(None)):
|
|
| 26 |
if x_api_key != API_KEY:
|
| 27 |
raise HTTPException(status_code=403, detail="Go away")
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List, Optional
|
| 5 |
import torch
|
| 6 |
+
import logging
|
| 7 |
+
import traceback
|
| 8 |
+
|
| 9 |
+
# Настройка логирования
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
+
# Загрузка
|
| 16 |
model_id = "google/gemma-2b-it"
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cpu", torch_dtype=torch.float32)
|
| 19 |
|
| 20 |
class Message(BaseModel):
|
| 21 |
+
role: str
|
| 22 |
content: str
|
| 23 |
|
| 24 |
class ChatRequest(BaseModel):
|
| 25 |
messages: List[Message]
|
| 26 |
+
system_prompt: Optional[str] = "You are a helpful assistant."
|
| 27 |
+
max_tokens: Optional[int] = 150
|
| 28 |
temperature: Optional[float] = 0.7
|
| 29 |
|
| 30 |
API_KEY = "bazi2281488133767695242"
|
|
|
|
| 34 |
if x_api_key != API_KEY:
|
| 35 |
raise HTTPException(status_code=403, detail="Go away")
|
| 36 |
|
| 37 |
+
try:
|
| 38 |
+
messages =[{"role": "system", "content": request.system_prompt}] + [m.dict() for m in request.messages]
|
| 39 |
+
|
| 40 |
+
input_ids = tokenizer.apply_chat_template(
|
| 41 |
+
messages,
|
| 42 |
+
return_tensors="pt",
|
| 43 |
+
add_generation_prompt=True
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if not isinstance(input_ids, torch.Tensor):
|
| 47 |
+
input_ids = torch.tensor(input_ids)
|
| 48 |
+
|
| 49 |
+
input_ids = input_ids.to("cpu")
|
| 50 |
+
|
| 51 |
+
outputs = model.generate(
|
| 52 |
+
input_ids,
|
| 53 |
+
max_new_tokens=request.max_tokens,
|
| 54 |
+
temperature=request.temperature,
|
| 55 |
+
do_sample=True,
|
| 56 |
+
pad_token_id=tokenizer.eos_token_id
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
|
| 60 |
+
|
| 61 |
+
return {"response": response}
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
error_trace = traceback.format_exc()
|
| 65 |
+
logger.error(f"Error in chat generation: {error_trace}")
|
| 66 |
+
raise HTTPException(status_code=500, detail=str(e))
|