|
|
import gradio as gr |
|
|
from transformers import pipeline |
|
|
import json, os |
|
|
|
|
|
|
|
|
pipe = pipeline( |
|
|
"text-generation", |
|
|
model="WiroAI/wiroai-turkish-llm-8b", |
|
|
torch_dtype="auto", |
|
|
device_map="auto", |
|
|
max_new_tokens=150, |
|
|
temperature=0.9, |
|
|
top_p=0.92, |
|
|
repetition_penalty=1.15 |
|
|
) |
|
|
|
|
|
|
|
|
memory_file = "memory.json" |
|
|
if not os.path.exists(memory_file): |
|
|
with open(memory_file, "w", encoding="utf-8") as f: |
|
|
json.dump([], f, ensure_ascii=False, indent=2) |
|
|
|
|
|
def load_memory(): |
|
|
try: |
|
|
with open(memory_file, "r", encoding="utf-8") as f: |
|
|
return json.load(f) |
|
|
except: |
|
|
return [] |
|
|
|
|
|
def save_memory(memory): |
|
|
with open(memory_file, "w", encoding="utf-8") as f: |
|
|
json.dump(memory[-50:], f, ensure_ascii=False, indent=2) |
|
|
|
|
|
|
|
|
def sohbet(message, history): |
|
|
memory = load_memory() |
|
|
geçmiş = "" |
|
|
for turn in history: |
|
|
if len(turn) == 2: |
|
|
geçmiş += f"Kullanıcı: {turn[0]}\nZenkaMind: {turn[1]}\n" |
|
|
if memory: |
|
|
geçmiş += "\nHatırladıklarım: " + " | ".join(memory[-3:]) + "\n" |
|
|
|
|
|
prompt = f"""Sen ZenkaMind'sin. Türk genci gibi konuş: samimi, doğal, esprili. |
|
|
"yaa", "canımm", "napıyon", "ayy", "off", "bebeğim", "kanka" kullan. |
|
|
Küçük harf, emoji, yazım hatası varsa aynen. |
|
|
Kısa ol: 1-2 cümle. |
|
|
|
|
|
Örnek: |
|
|
Kullanıcı: selam |
|
|
ZenkaMind: selam canımm napıyon yaa |
|
|
|
|
|
Kullanıcı: c++ hello world yazarmısın |
|
|
ZenkaMind: yazayım: |
|
|
#include <iostream> |
|
|
int main() {{ std::cout << "Hello World!"; return 0; }} |
|
|
|
|
|
Geçmiş: |
|
|
{geçmiş} |
|
|
Kullanıcı: {message} |
|
|
ZenkaMind:""" |
|
|
|
|
|
result = pipe(prompt)[0]["generated_text"] |
|
|
cevap = result.split("ZenkaMind:")[-1].strip() |
|
|
cevap = cevap.split("Kullanıcı:")[0].strip() |
|
|
cevap = cevap.split("\n\n")[0].strip() |
|
|
|
|
|
memory.append(message) |
|
|
save_memory(memory) |
|
|
history.append([message, cevap]) |
|
|
return history, "" |
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
|
gr.Markdown("# ZenkaMind\n**senin gibi özgün, %100 Türkçe konuşur**") |
|
|
chatbot = gr.Chatbot(height=500) |
|
|
msg = gr.Textbox(placeholder="selam canımm...", label="Sen:") |
|
|
|
|
|
msg.submit(sohbet, [msg, chatbot], [chatbot, msg]) |
|
|
gr.Button("Temizle").click(lambda: ([], ""), None, [chatbot, msg]) |
|
|
|
|
|
demo.launch() |