Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,20 +2,16 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
-
# Kullanacağımız modelin
|
| 6 |
-
MODEL_NAME = "gorkemgoknar/gpt2-small-turkish"
|
| 7 |
|
| 8 |
-
# Cihaz ayarı: GPU varsa cuda, yoksa CPU
|
| 9 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
-
|
| 11 |
# Tokenizer ve modeli yükle, cihaza taşı
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 13 |
-
model
|
| 14 |
|
| 15 |
-
# Sistem mesajı ile asistanın tonunu belirle
|
| 16 |
-
SYSTEM_PROMPT = (
|
| 17 |
"Sen Türkçe konuşan, nazik ve açıklayıcı bir yapay zeka asistanısın. "
|
| 18 |
-
"
|
| 19 |
)
|
| 20 |
|
| 21 |
def respond(user_input: str) -> str:
|
|
@@ -26,16 +22,18 @@ def respond(user_input: str) -> str:
|
|
| 26 |
# Yanıt üretimi: tekrarı azalt ve beam search kullan
|
| 27 |
outputs = model.generate(
|
| 28 |
**inputs,
|
| 29 |
-
max_new_tokens=
|
| 30 |
-
no_repeat_ngram_size=
|
| 31 |
-
repetition_penalty=1.
|
| 32 |
num_beams=3, # Beam search ışın sayısı
|
| 33 |
-
early_stopping=True, # EOS token’ı
|
| 34 |
-
temperature=0.
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# Tam metni çözüp asistan kısmını ayıkla
|
| 39 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
return full_text.split("Asistan:")[-1].strip()
|
| 41 |
|
|
@@ -45,9 +43,9 @@ demo = gr.Interface(
|
|
| 45 |
inputs=gr.Textbox(lines=3, placeholder="Sorunuzu buraya yazın..."),
|
| 46 |
outputs="text",
|
| 47 |
title="AR Güneş Sistemi Asistanı",
|
| 48 |
-
description="AR projeniz için Türkçe sohbet asistanı.
|
| 49 |
allow_flagging="never"
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
-
demo.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
# Kullanacağımız modelin adı\ nMODEL_NAME = "gorkemgoknar/gpt2-small-turkish"
|
|
|
|
| 6 |
|
|
|
|
| 7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 8 |
# Tokenizer ve modeli yükle, cihaza taşı
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME).to(device)
|
| 11 |
|
| 12 |
+
# Sistem mesajı ile asistanın tonunu belirle\ nSYSTEM_PROMPT = (
|
|
|
|
| 13 |
"Sen Türkçe konuşan, nazik ve açıklayıcı bir yapay zeka asistanısın. "
|
| 14 |
+
"Gereksiz tekrar yapmadan, kısa ve net şekilde cevap ver."
|
| 15 |
)
|
| 16 |
|
| 17 |
def respond(user_input: str) -> str:
|
|
|
|
| 22 |
# Yanıt üretimi: tekrarı azalt ve beam search kullan
|
| 23 |
outputs = model.generate(
|
| 24 |
**inputs,
|
| 25 |
+
max_new_tokens=30, # Maksimum 30 token üret
|
| 26 |
+
no_repeat_ngram_size=4, # 4-gram tekrarını engelle
|
| 27 |
+
repetition_penalty=1.5, # Tekrar cezalandırma gücünü artır
|
| 28 |
num_beams=3, # Beam search ışın sayısı
|
| 29 |
+
early_stopping=True, # EOS token’ı geldiğinde dur
|
| 30 |
+
temperature=0.6, # Daha düşük sıcaklık
|
| 31 |
+
top_k=50, # Top-k sampling
|
| 32 |
+
top_p=0.85, # Nucleus sampling
|
| 33 |
+
pad_token_id=tokenizer.eos_token_id
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# Tam metni çözüp asistan kısmını ayıkla ve döndür
|
| 37 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
return full_text.split("Asistan:")[-1].strip()
|
| 39 |
|
|
|
|
| 43 |
inputs=gr.Textbox(lines=3, placeholder="Sorunuzu buraya yazın..."),
|
| 44 |
outputs="text",
|
| 45 |
title="AR Güneş Sistemi Asistanı",
|
| 46 |
+
description="AR projeniz için Türkçe sohbet asistanı.",
|
| 47 |
allow_flagging="never"
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|