Spaces:
Paused
Paused
Delete llm_model.py
Browse files- llm_model.py +0 -91
llm_model.py
DELETED
@@ -1,91 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import traceback
|
3 |
-
from datetime import datetime
|
4 |
-
from pydantic import BaseModel
|
5 |
-
from unsloth import FastLanguageModel
|
6 |
-
from transformers import pipeline
|
7 |
-
import torch
|
8 |
-
from log import log
|
9 |
-
|
10 |
-
class Message(BaseModel):
|
11 |
-
user_input: str
|
12 |
-
|
13 |
-
class LLMModel:
|
14 |
-
def __init__(self):
|
15 |
-
self.model = None
|
16 |
-
self.tokenizer = None
|
17 |
-
self.pipe = None
|
18 |
-
|
19 |
-
def setup(self, s_config, project_config, project_path):
|
20 |
-
try:
|
21 |
-
log("🧠 LLMModel setup() başladı")
|
22 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
23 |
-
log(f"📡 Kullanılan cihaz: {device}")
|
24 |
-
|
25 |
-
model_base = project_config["model_base"]
|
26 |
-
hf_token = s_config.get_auth_token()
|
27 |
-
|
28 |
-
# Ortam değişkenleri
|
29 |
-
os.environ.setdefault("HF_HOME", "/app/.cache")
|
30 |
-
os.environ.setdefault("HF_DATASETS_CACHE", "/app/.cache")
|
31 |
-
os.environ.setdefault("HF_HUB_CACHE", "/app/.cache")
|
32 |
-
os.environ.setdefault("TRITON_CACHE_DIR", "/tmp/.triton")
|
33 |
-
os.environ.setdefault("TORCHINDUCTOR_CACHE_DIR", "/tmp/torchinductor_cache")
|
34 |
-
|
35 |
-
log(f"📦 UnsLoTH modeli yükleniyor: {model_base}")
|
36 |
-
self.model, self.tokenizer = FastLanguageModel.from_pretrained(
|
37 |
-
model_name=model_base,
|
38 |
-
load_in_4bit=True,
|
39 |
-
token=hf_token,
|
40 |
-
cache_dir="/app/.cache"
|
41 |
-
)
|
42 |
-
FastLanguageModel.for_inference(self.model)
|
43 |
-
|
44 |
-
self.pipe = pipeline(
|
45 |
-
"text-generation",
|
46 |
-
model=self.model,
|
47 |
-
tokenizer=self.tokenizer,
|
48 |
-
device_map="auto"
|
49 |
-
)
|
50 |
-
|
51 |
-
log("✅ LLMModel setup() başarıyla tamamlandı.")
|
52 |
-
|
53 |
-
except Exception as e:
|
54 |
-
log(f"❌ LLMModel setup() hatası: {e}")
|
55 |
-
traceback.print_exc()
|
56 |
-
|
57 |
-
async def generate_response_with_messages(self, messages, project_config, system_prompt):
|
58 |
-
try:
|
59 |
-
# Son 10 mesajla sınırla
|
60 |
-
max_history = 10
|
61 |
-
recent_messages = messages[-max_history:]
|
62 |
-
|
63 |
-
# System prompt + mesaj geçmişini birleştir
|
64 |
-
all_messages = [{"role": "system", "content": system_prompt}] + recent_messages
|
65 |
-
history_text = ""
|
66 |
-
for msg in all_messages:
|
67 |
-
if msg["role"] == "user":
|
68 |
-
history_text += f"Kullanıcı: {msg['content']}\n"
|
69 |
-
elif msg["role"] == "assistant":
|
70 |
-
history_text += f"Asistan: {msg['content']}\n"
|
71 |
-
elif msg["role"] == "system":
|
72 |
-
history_text += f"{msg['content']}\n"
|
73 |
-
|
74 |
-
full_prompt = f"{history_text}Asistan:"
|
75 |
-
log("🧠 LLM çağrısı başlatılıyor...")
|
76 |
-
|
77 |
-
outputs = self.pipe(
|
78 |
-
full_prompt,
|
79 |
-
max_new_tokens=512,
|
80 |
-
temperature=0.2,
|
81 |
-
top_p=0.95,
|
82 |
-
repetition_penalty=1.1
|
83 |
-
)
|
84 |
-
answer = outputs[0]["generated_text"].replace(full_prompt, "").strip()
|
85 |
-
log("✅ LLM cevabı başarıyla alındı.")
|
86 |
-
return answer
|
87 |
-
|
88 |
-
except Exception as e:
|
89 |
-
log(f"❌ generate_response_with_messages sırasında hata oluştu: {e}")
|
90 |
-
traceback.print_exc()
|
91 |
-
return "Hata: Model cevabı alınamadı."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|