Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from pydantic import BaseModel
|
|
| 6 |
from llama_cpp import Llama
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from datetime import datetime
|
|
|
|
| 9 |
|
| 10 |
# === 🕒 Zamanlı log fonksiyonu
|
| 11 |
def log(message):
|
|
@@ -15,7 +16,7 @@ def log(message):
|
|
| 15 |
|
| 16 |
# === Model bilgileri
|
| 17 |
REPO_ID = "oncu/Turkish-Llama-3-8B-function-calling-GGUF"
|
| 18 |
-
FILENAME = "turkish-llama-3-8b-function-calling.q8_0.gguf"
|
| 19 |
LOCAL_MODEL_PATH = f"/tmp/{FILENAME}"
|
| 20 |
HF_TOKEN = os.getenv("HF_TOKEN") # ✅ Hugging Face Token (varsa)
|
| 21 |
|
|
@@ -58,7 +59,7 @@ def load_model():
|
|
| 58 |
log(f"✅ Model indirildi: {model_path}")
|
| 59 |
|
| 60 |
log("📦 GGUF model yükleniyor...")
|
| 61 |
-
llm = Llama(model_path=model_path, n_gpu_layers=-1, n_ctx=
|
| 62 |
log("✅ Model başarıyla yüklendi ve kullanılmaya hazır.")
|
| 63 |
log("💡 Artık /chat endpoint'ine POST isteği gönderebilirsiniz.")
|
| 64 |
except Exception as e:
|
|
@@ -72,10 +73,23 @@ def chat(req: ChatRequest):
|
|
| 72 |
log(f"💬 Yeni istek alındı: '{req.prompt}'")
|
| 73 |
prompt = f"{SYSTEM_PROMPT}\n\nKullanıcı: {req.prompt}\nAsistan:"
|
| 74 |
log("🧠 LLM çağrısı başlatılıyor...")
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
answer = response["choices"][0]["text"].strip()
|
| 77 |
log("✅ LLM cevabı başarıyla alındı.")
|
| 78 |
return {"response": answer}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
log(f"❌ /chat sırasında hata oluştu: {e}")
|
| 81 |
traceback.print_exc()
|
|
|
|
| 6 |
from llama_cpp import Llama
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from datetime import datetime
|
| 9 |
+
import concurrent.futures
|
| 10 |
|
| 11 |
# === 🕒 Zamanlı log fonksiyonu
|
| 12 |
def log(message):
|
|
|
|
| 16 |
|
| 17 |
# === Model bilgileri
|
| 18 |
REPO_ID = "oncu/Turkish-Llama-3-8B-function-calling-GGUF"
|
| 19 |
+
FILENAME = "turkish-llama-3-8b-function-calling.q8_0.gguf" # ✅ doğru dosya adı
|
| 20 |
LOCAL_MODEL_PATH = f"/tmp/{FILENAME}"
|
| 21 |
HF_TOKEN = os.getenv("HF_TOKEN") # ✅ Hugging Face Token (varsa)
|
| 22 |
|
|
|
|
| 59 |
log(f"✅ Model indirildi: {model_path}")
|
| 60 |
|
| 61 |
log("📦 GGUF model yükleniyor...")
|
| 62 |
+
llm = Llama(model_path=model_path, n_gpu_layers=-1, n_ctx=1024) # ✅ n_ctx düşürüldü
|
| 63 |
log("✅ Model başarıyla yüklendi ve kullanılmaya hazır.")
|
| 64 |
log("💡 Artık /chat endpoint'ine POST isteği gönderebilirsiniz.")
|
| 65 |
except Exception as e:
|
|
|
|
| 73 |
log(f"💬 Yeni istek alındı: '{req.prompt}'")
|
| 74 |
prompt = f"{SYSTEM_PROMPT}\n\nKullanıcı: {req.prompt}\nAsistan:"
|
| 75 |
log("🧠 LLM çağrısı başlatılıyor...")
|
| 76 |
+
|
| 77 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 78 |
+
future = executor.submit(
|
| 79 |
+
llm,
|
| 80 |
+
prompt,
|
| 81 |
+
max_tokens=512,
|
| 82 |
+
stop=["Kullanıcı:", "Asistan:"],
|
| 83 |
+
echo=False
|
| 84 |
+
)
|
| 85 |
+
response = future.result(timeout=30) # ✅ 30 saniye timeout
|
| 86 |
answer = response["choices"][0]["text"].strip()
|
| 87 |
log("✅ LLM cevabı başarıyla alındı.")
|
| 88 |
return {"response": answer}
|
| 89 |
+
|
| 90 |
+
except concurrent.futures.TimeoutError:
|
| 91 |
+
log("❌ LLM çağrısı timeout oldu (30 saniye).")
|
| 92 |
+
return {"error": "LLM çağrısı zaman aşımına uğradı."}
|
| 93 |
except Exception as e:
|
| 94 |
log(f"❌ /chat sırasında hata oluştu: {e}")
|
| 95 |
traceback.print_exc()
|