Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,42 +2,43 @@ 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 |
-
|
| 16 |
-
""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 22 |
|
| 23 |
-
# Yanıt üretimi
|
| 24 |
outputs = model.generate(
|
| 25 |
**inputs,
|
| 26 |
-
max_new_tokens=
|
| 27 |
-
no_repeat_ngram_size=
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
# Gradio arayüzü tanımı
|
| 38 |
demo = gr.Interface(
|
| 39 |
fn=respond,
|
| 40 |
-
inputs=gr.Textbox(lines=
|
| 41 |
outputs="text",
|
| 42 |
title="AR Güneş Sistemi Asistanı",
|
| 43 |
description="AR projeniz için Türkçe sohbet asistanı. Bir cümle yazıp Gönder’e tıklayın.",
|
|
|
|
| 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 |
+
"Kullanıcının sorusuna kısaca ve anlaşılır şekilde cevap ver.\n\n"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def respond(user_input: str) -> str:
|
| 18 |
+
# Prompt’u oluştur: sistem mesajı + kullanıcı girişi
|
| 19 |
+
prompt = SYSTEM_PROMPT + f"Kullanıcı: {user_input}\nAsistan:"
|
| 20 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 21 |
|
| 22 |
+
# Yanıt üretimi: tekrarı azalt ve beam search kullan
|
| 23 |
outputs = model.generate(
|
| 24 |
**inputs,
|
| 25 |
+
max_new_tokens=40, # Üretilen token sayısı
|
| 26 |
+
no_repeat_ngram_size=3, # 3-gram tekrarını engelle
|
| 27 |
+
repetition_penalty=1.2, # Tekrar cezalandırma
|
| 28 |
+
num_beams=3, # Beam search ışın sayısı
|
| 29 |
+
early_stopping=True, # EOS token’ında dur
|
| 30 |
+
temperature=0.7, # Daha düşük sıcaklık
|
| 31 |
+
top_p=0.9 # Nucleus sampling
|
| 32 |
)
|
| 33 |
|
| 34 |
+
# Tam metni çözüp asistan kısmını ayıkla
|
| 35 |
+
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
+
return full_text.split("Asistan:")[-1].strip()
|
| 37 |
|
| 38 |
# Gradio arayüzü tanımı
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=respond,
|
| 41 |
+
inputs=gr.Textbox(lines=3, placeholder="Sorunuzu buraya yazın..."),
|
| 42 |
outputs="text",
|
| 43 |
title="AR Güneş Sistemi Asistanı",
|
| 44 |
description="AR projeniz için Türkçe sohbet asistanı. Bir cümle yazıp Gönder’e tıklayın.",
|