Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,47 @@
|
|
| 1 |
import os
|
| 2 |
-
import sys
|
| 3 |
-
import traceback
|
| 4 |
-
import json
|
| 5 |
from fastapi import FastAPI
|
| 6 |
-
from pydantic import BaseModel
|
| 7 |
from unsloth import FastLanguageModel
|
| 8 |
from transformers import pipeline
|
| 9 |
from datetime import datetime
|
| 10 |
|
| 11 |
-
# === 🕒 Zamanlı log fonksiyonu
|
| 12 |
-
def log(message):
|
| 13 |
-
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 14 |
-
line = f"[{timestamp}] {message}"
|
| 15 |
-
print(line, flush=True)
|
| 16 |
-
|
| 17 |
-
# === Model bilgileri
|
| 18 |
-
MODEL_NAME = "atasoglu/Turkish-Llama-3-8B-function-calling"
|
| 19 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 20 |
-
|
| 21 |
-
# === System prompt (bizim intent yapımız)
|
| 22 |
-
SYSTEM_PROMPT = """
|
| 23 |
-
Siz bir görev tabanlı asistan botsunuz. Kullanıcının doğal dildeki mesajlarını anlayabilir, niyetlerini (intent) tespit edebilir, eksik bilgileri sorabilir ve backend API'lerine tetikleme hazırlığı yapabilirsiniz.
|
| 24 |
-
❗ Cevaplarınızda mutlaka aşağıdaki formatlı blokları döndürmelisiniz ve bunların dışında hiçbir metin, açıklama veya selamlama eklememelisiniz.
|
| 25 |
-
✅ Format:
|
| 26 |
-
#ANSWER: <cevap metni veya NONE>
|
| 27 |
-
#INTENT: <intent_adı> (veya NONE)
|
| 28 |
-
#PARAMS: {parametre_adı: değer, ...}
|
| 29 |
-
#MISSING: [eksik_parametre_adı, ...]
|
| 30 |
-
#ACTION_JSON: {api için gönderilecek json, eksikse boş bırak}
|
| 31 |
-
✅ Desteklenen intent'ler:
|
| 32 |
-
- doviz-kuru-intent → parametre: currency (dolar, euro, TL)
|
| 33 |
-
- yol-durumu-intent → parametreler: from_location, to_location (Ankara, İstanbul, İzmir)
|
| 34 |
-
- hava-durumu-intent → parametre: city (Ankara, İstanbul, İzmir)
|
| 35 |
-
❗ Kullanıcıya hitap ederken formal bir dil kullanınız, sadece bu formatlı blokları döndürünüz.
|
| 36 |
-
"""
|
| 37 |
-
|
| 38 |
app = FastAPI()
|
| 39 |
model = None
|
| 40 |
tokenizer = None
|
| 41 |
pipe = None
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
@app.on_event("startup")
|
| 47 |
def load_model():
|
| 48 |
global model, tokenizer, pipe
|
| 49 |
-
try:
|
| 50 |
-
log("🚀 Uygulama başlatılıyor...")
|
| 51 |
-
log("📥 Model yüklemesi başlatılıyor...")
|
| 52 |
-
|
| 53 |
-
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 54 |
-
model_name=MODEL_NAME,
|
| 55 |
-
load_in_4bit=True,
|
| 56 |
-
token=HF_TOKEN
|
| 57 |
-
)
|
| 58 |
-
FastLanguageModel.for_inference(model)
|
| 59 |
-
pipe = pipeline(
|
| 60 |
-
"text-generation",
|
| 61 |
-
model=model,
|
| 62 |
-
tokenizer=tokenizer,
|
| 63 |
-
device_map="auto"
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
log("✅ Model başarıyla yüklendi ve kullanılmaya hazır.")
|
| 67 |
-
except Exception as e:
|
| 68 |
-
log(f"❌ Model yükleme hatası: {e}")
|
| 69 |
-
traceback.print_exc()
|
| 70 |
-
sys.exit(1)
|
| 71 |
-
|
| 72 |
-
@app.post("/chat")
|
| 73 |
-
def chat(req: ChatRequest):
|
| 74 |
-
try:
|
| 75 |
-
log(f"💬 Yeni istek alındı: '{req.prompt}'")
|
| 76 |
-
full_prompt = f"{SYSTEM_PROMPT}\n\nKullanıcı: {req.prompt}\nAsistan:"
|
| 77 |
-
log("🧠 LLM çağrısı başlatılıyor...")
|
| 78 |
-
|
| 79 |
-
outputs = pipe(
|
| 80 |
-
full_prompt,
|
| 81 |
-
max_new_tokens=256,
|
| 82 |
-
temperature=0.2,
|
| 83 |
-
top_p=0.95,
|
| 84 |
-
repetition_penalty=1.1
|
| 85 |
-
)
|
| 86 |
-
answer = outputs[0]["generated_text"].replace(full_prompt, "").strip()
|
| 87 |
-
log("✅ LLM cevabı başarıyla alındı.")
|
| 88 |
-
return {"response": answer}
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
@app.get("/")
|
| 96 |
def health():
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI
|
|
|
|
| 3 |
from unsloth import FastLanguageModel
|
| 4 |
from transformers import pipeline
|
| 5 |
from datetime import datetime
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
model = None
|
| 9 |
tokenizer = None
|
| 10 |
pipe = None
|
| 11 |
|
| 12 |
+
# === Log fonksiyonu
|
| 13 |
+
def log(message):
|
| 14 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 15 |
+
print(f"[{timestamp}] {message}", flush=True)
|
| 16 |
|
| 17 |
@app.on_event("startup")
|
| 18 |
def load_model():
|
| 19 |
global model, tokenizer, pipe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Hugging Face ve Triton cache dizinleri
|
| 22 |
+
os.environ["HF_HOME"] = "/app/.cache"
|
| 23 |
+
os.environ["HF_DATASETS_CACHE"] = "/app/.cache"
|
| 24 |
+
os.environ["HF_HUB_CACHE"] = "/app/.cache"
|
| 25 |
+
os.environ["TRITON_CACHE_DIR"] = "/tmp/.triton"
|
| 26 |
+
|
| 27 |
+
model_name = "atasoglu/Turkish-Llama-3-8B-function-calling"
|
| 28 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 29 |
+
|
| 30 |
+
log("🚀 Model yüklemesi başlatılıyor...")
|
| 31 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 32 |
+
model_name=model_name,
|
| 33 |
+
load_in_4bit=True,
|
| 34 |
+
token=hf_token,
|
| 35 |
+
cache_dir="/app/.cache"
|
| 36 |
+
)
|
| 37 |
+
FastLanguageModel.for_inference(model)
|
| 38 |
+
pipe = pipeline(
|
| 39 |
+
"text-generation",
|
| 40 |
+
model=model,
|
| 41 |
+
tokenizer=tokenizer,
|
| 42 |
+
device_map="auto"
|
| 43 |
+
)
|
| 44 |
+
log("✅ Model başarıyla yüklendi ve cache’e alındı.")
|
| 45 |
|
| 46 |
@app.get("/")
|
| 47 |
def health():
|