Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,10 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStream
|
|
| 4 |
from threading import Thread
|
| 5 |
import time
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
|
|
|
| 9 |
quantization_config = BitsAndBytesConfig(
|
| 10 |
load_in_4bit=True,
|
| 11 |
bnb_4bit_compute_dtype=torch.float16,
|
|
@@ -28,18 +30,29 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 28 |
print("✅ Model başarıyla yüklendi!")
|
| 29 |
|
| 30 |
def respond(message, history):
|
| 31 |
-
system_prompt = "NoiseAI/Noise adlı, MagnuX tarafından eğitilmiş bir yapay zekasın. Türkçe konuş ve en iyi kod pratiklerini uygula."
|
| 32 |
|
|
|
|
| 33 |
messages = [{"role": "system", "content": system_prompt}]
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
| 41 |
messages.append({"role": "user", "content": message})
|
| 42 |
|
|
|
|
| 43 |
text = tokenizer.apply_chat_template(
|
| 44 |
messages,
|
| 45 |
tokenize=False,
|
|
@@ -48,34 +61,36 @@ def respond(message, history):
|
|
| 48 |
|
| 49 |
inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 50 |
|
| 51 |
-
# Generation
|
| 52 |
-
generation_kwargs = dict(
|
| 53 |
-
**inputs,
|
| 54 |
-
max_new_tokens=2048,
|
| 55 |
-
temperature=0.6,
|
| 56 |
-
top_p=0.9,
|
| 57 |
-
do_sample=True,
|
| 58 |
-
pad_token_id=tokenizer.pad_token_id,
|
| 59 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# Thread'de çalıştır ve sonucu bekle
|
| 63 |
with torch.no_grad():
|
| 64 |
-
outputs = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Decode et
|
| 67 |
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
for i in range(0, len(response),
|
| 71 |
-
yield response[:i+
|
| 72 |
-
time.sleep(0.
|
| 73 |
|
|
|
|
| 74 |
demo = gr.ChatInterface(
|
| 75 |
respond,
|
| 76 |
title="NoiseAI - Qwen2.5-7B",
|
| 77 |
-
description="7B parametre -
|
| 78 |
-
examples=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
# --- MODEL AYARLARI ---
|
| 8 |
+
MODEL_ID = "HuggingFaceTB/SmolLM-360M-Instruct" # veya "microsoft/Phi-3.5-mini-instruct"
|
| 9 |
|
| 10 |
+
# 4-bit Quantization
|
| 11 |
quantization_config = BitsAndBytesConfig(
|
| 12 |
load_in_4bit=True,
|
| 13 |
bnb_4bit_compute_dtype=torch.float16,
|
|
|
|
| 30 |
print("✅ Model başarıyla yüklendi!")
|
| 31 |
|
| 32 |
def respond(message, history):
|
| 33 |
+
system_prompt = "NoiseAI/Noise adlı, MagnuX tarafından eğitilmiş bir yapay zekasın. Türkçe konuş ve en iyi kod pratiklerini uygula. Kullanıcının önceki mesajlarını hatırlarsın."
|
| 34 |
|
| 35 |
+
# ✅ HISTORY FİX: Her formatı dene
|
| 36 |
messages = [{"role": "system", "content": system_prompt}]
|
| 37 |
|
| 38 |
+
# History'yi işle (Gradio'nun formatı: [[user1, asst1], [user2, asst2], ...])
|
| 39 |
+
for item in history:
|
| 40 |
+
if isinstance(item, (list, tuple)) and len(item) == 2:
|
| 41 |
+
user_msg, asst_msg = item[0], item[1]
|
| 42 |
+
if user_msg:
|
| 43 |
+
messages.append({"role": "user", "content": user_msg})
|
| 44 |
+
if asst_msg:
|
| 45 |
+
messages.append({"role": "assistant", "content": asst_msg})
|
| 46 |
+
elif isinstance(item, dict): # Alternatif format
|
| 47 |
+
if item.get("role") == "user":
|
| 48 |
+
messages.append(item)
|
| 49 |
+
elif item.get("role") == "assistant":
|
| 50 |
+
messages.append(item)
|
| 51 |
|
| 52 |
+
# Yeni mesajı ekle
|
| 53 |
messages.append({"role": "user", "content": message})
|
| 54 |
|
| 55 |
+
# Prompt'u oluştur
|
| 56 |
text = tokenizer.apply_chat_template(
|
| 57 |
messages,
|
| 58 |
tokenize=False,
|
|
|
|
| 61 |
|
| 62 |
inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 63 |
|
| 64 |
+
# Generation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with torch.no_grad():
|
| 66 |
+
outputs = model.generate(
|
| 67 |
+
**inputs,
|
| 68 |
+
max_new_tokens=2048,
|
| 69 |
+
temperature=0.6,
|
| 70 |
+
top_p=0.9,
|
| 71 |
+
do_sample=True,
|
| 72 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 73 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 74 |
+
)
|
| 75 |
|
| 76 |
# Decode et
|
| 77 |
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 78 |
|
| 79 |
+
# Stream gibi göster (karakter karakter)
|
| 80 |
+
for i in range(0, len(response), 3):
|
| 81 |
+
yield response[:i+3]
|
| 82 |
+
time.sleep(0.01)
|
| 83 |
|
| 84 |
+
# Arayüz
|
| 85 |
demo = gr.ChatInterface(
|
| 86 |
respond,
|
| 87 |
title="NoiseAI - Qwen2.5-7B",
|
| 88 |
+
description="7B parametre - History hatası düzeltildi",
|
| 89 |
+
examples=[
|
| 90 |
+
["2+2=?"],
|
| 91 |
+
["Peki ya 3*3?"],
|
| 92 |
+
["Python ile yılan oyunu yaz"]
|
| 93 |
+
]
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|