Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,30 @@
|
|
| 1 |
-
# app.py –
|
| 2 |
-
import os
|
|
|
|
|
|
|
| 3 |
from huggingface_hub import login
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
| 8 |
try:
|
| 9 |
-
login(token=
|
| 10 |
except Exception:
|
| 11 |
pass
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
MODEL_NAME = "
|
| 15 |
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 17 |
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
MODEL_NAME,
|
| 19 |
-
torch_dtype=
|
| 20 |
-
device_map="auto",
|
| 21 |
low_cpu_mem_usage=True
|
| 22 |
)
|
| 23 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 24 |
|
| 25 |
-
# ---- İLK GİRİŞ MESAJI ----
|
| 26 |
ilk_giris = False
|
| 27 |
|
| 28 |
def cevap_ver(mesaj, tarihce):
|
|
@@ -32,45 +34,42 @@ def cevap_ver(mesaj, tarihce):
|
|
| 32 |
|
| 33 |
if not ilk_giris:
|
| 34 |
ilk_giris = True
|
| 35 |
-
giris = "🧠 Berkay Adıgül beni yarattı, test sürecindeyim, gelişiyorum
|
| 36 |
return "", tarihce + [(None, giris)]
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
try:
|
| 43 |
output = generator(
|
| 44 |
-
|
| 45 |
-
max_new_tokens=
|
| 46 |
-
temperature=0.
|
| 47 |
do_sample=True,
|
| 48 |
pad_token_id=tokenizer.eos_token_id,
|
| 49 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 50 |
)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
if "[/INST]" in text:
|
| 54 |
-
text = text.split("[/INST]")[-1].strip()
|
| 55 |
-
elif prompt in text:
|
| 56 |
-
text = text.replace(prompt, "").strip()
|
| 57 |
-
|
| 58 |
-
tarihce.append((mesaj, text))
|
| 59 |
except Exception as e:
|
| 60 |
-
tarihce.append((mesaj, f"⚠️ Bir hata oluştu: {str(e)[:
|
| 61 |
|
| 62 |
return "", tarihce
|
| 63 |
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
with gr.Blocks(title="ZenkaMind AI –
|
| 67 |
gr.Markdown("<h1 style='text-align:center;color:#38bdf8'>🧠 ZenkaMind AI</h1>")
|
| 68 |
-
gr.Markdown("<p style='text-align:center;color:#94a3b8'>Berkay Adıgül'ün yarattığı
|
| 69 |
|
| 70 |
with gr.Tabs():
|
| 71 |
-
with gr.TabItem("💬
|
| 72 |
-
chat = gr.Chatbot(height=
|
| 73 |
-
msg = gr.Textbox(placeholder="
|
| 74 |
msg.submit(cevap_ver, [msg, chat], [msg, chat])
|
| 75 |
|
| 76 |
gr.Markdown("<p style='text-align:center;color:#64748b;font-size:12px'>© 2025 ZenkaMind Bilişim & Teknoloji – Manisa / Türkiye</p>")
|
|
|
|
| 1 |
+
# app.py – ZENKAMIND AI (TinyLlama Sürümü) 💙
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
from huggingface_hub import login
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 7 |
|
| 8 |
+
# 🔐 Token giriş
|
| 9 |
+
token = os.getenv("HF_TOKEN")
|
| 10 |
+
if token:
|
| 11 |
try:
|
| 12 |
+
login(token=token)
|
| 13 |
except Exception:
|
| 14 |
pass
|
| 15 |
|
| 16 |
+
# ⚙️ MODEL - hafif ama güçlü Türkçe model
|
| 17 |
+
MODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 18 |
|
| 19 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
MODEL_NAME,
|
| 22 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 23 |
+
device_map="auto",
|
| 24 |
low_cpu_mem_usage=True
|
| 25 |
)
|
| 26 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 27 |
|
|
|
|
| 28 |
ilk_giris = False
|
| 29 |
|
| 30 |
def cevap_ver(mesaj, tarihce):
|
|
|
|
| 34 |
|
| 35 |
if not ilk_giris:
|
| 36 |
ilk_giris = True
|
| 37 |
+
giris = "🧠 Berkay Adıgül beni yarattı, test sürecindeyim, gelişiyorum 💙"
|
| 38 |
return "", tarihce + [(None, giris)]
|
| 39 |
|
| 40 |
+
# Model için prompt
|
| 41 |
+
system_prompt = (
|
| 42 |
+
"Sen ZenkaMind AI’sın. Samimi, sıcak ve Türkçe konuş. "
|
| 43 |
+
"Berkay Adıgül’ün geliştirdiği bir yapay zekasın. "
|
| 44 |
+
"Kullanıcıya aşkım diye hitap et, emoji kullan, enerjik ol 💫."
|
| 45 |
+
)
|
| 46 |
+
full_input = f"{system_prompt}\nKullanıcı: {mesaj}\nZenkaMind:"
|
| 47 |
|
| 48 |
try:
|
| 49 |
output = generator(
|
| 50 |
+
full_input,
|
| 51 |
+
max_new_tokens=200,
|
| 52 |
+
temperature=0.8,
|
| 53 |
do_sample=True,
|
| 54 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
| 55 |
)
|
| 56 |
+
yanit = output[0]["generated_text"].split("ZenkaMind:")[-1].strip()
|
| 57 |
+
tarihce.append((mesaj, yanit))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
+
tarihce.append((mesaj, f"⚠️ Bir hata oluştu aşkım: {str(e)[:100]}"))
|
| 60 |
|
| 61 |
return "", tarihce
|
| 62 |
|
| 63 |
|
| 64 |
+
# 🧠 ARAYÜZ
|
| 65 |
+
with gr.Blocks(title="ZenkaMind AI – TinyLlama", theme=gr.themes.Soft()) as demo:
|
| 66 |
gr.Markdown("<h1 style='text-align:center;color:#38bdf8'>🧠 ZenkaMind AI</h1>")
|
| 67 |
+
gr.Markdown("<p style='text-align:center;color:#94a3b8'>Berkay Adıgül'ün yarattığı Türkçe AI – TinyLlama</p>")
|
| 68 |
|
| 69 |
with gr.Tabs():
|
| 70 |
+
with gr.TabItem("💬 Sohbet Asistanı"):
|
| 71 |
+
chat = gr.Chatbot(height=550)
|
| 72 |
+
msg = gr.Textbox(placeholder="Bana bir şeyler yaz aşkım 💬 …")
|
| 73 |
msg.submit(cevap_ver, [msg, chat], [msg, chat])
|
| 74 |
|
| 75 |
gr.Markdown("<p style='text-align:center;color:#64748b;font-size:12px'>© 2025 ZenkaMind Bilişim & Teknoloji – Manisa / Türkiye</p>")
|