ogulcanaydogan's picture
Improve UI and add benchmark info
ee079fb verified
import gradio as gr
import spaces
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import re
_model = None
_tokenizer = None
MODEL_ID = "ogulcanaydogan/Turkish-LLM-14B-Instruct"
SYSTEM = "Sen yardimci bir Turkce asistansin. Acik, dogal ve anlasilir sekilde Turkce yanit ver."
def _load():
global _model, _tokenizer
if _model is None:
_tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
_model = AutoModelForCausalLM.from_pretrained(
MODEL_ID, torch_dtype=torch.float16, device_map="auto"
)
return _model, _tokenizer
def _clean(text):
cjk = re.search(r"[\u4e00-\u9fff\u3400-\u4dbf\u3040-\u309f\u30a0-\u30ff]", text)
if cjk:
text = text[:cjk.start()]
text = text.rstrip()
if text and text[-1] not in ".!?:;)]\"\'":
last = max(text.rfind("."), text.rfind("!"), text.rfind("?"), text.rfind("\n"))
if last > len(text) // 2:
text = text[:last + 1]
return text.strip()
@spaces.GPU(duration=120)
def respond(message, history):
model, tok = _load()
prompt = f"<|im_start|>system\n{SYSTEM}<|im_end|>\n"
for u, a in (history or []):
prompt += f"<|im_start|>user\n{u}<|im_end|>\n"
prompt += f"<|im_start|>assistant\n{a}<|im_end|>\n"
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
ids = tok(prompt, return_tensors="pt").to(model.device)
out = model.generate(**ids, max_new_tokens=512, temperature=0.7, top_p=0.9,
repetition_penalty=1.1, do_sample=True, pad_token_id=tok.eos_token_id)
resp = tok.decode(out[0][ids["input_ids"].shape[1]:], skip_special_tokens=False)
resp = resp.split("<|im_end|>")[0] if "<|im_end|>" in resp else resp
resp = resp.split("\nuser")[0] if "\nuser" in resp.lower() else resp
return _clean(resp)
CSS = """
.gradio-container { max-width: 800px !important; margin: auto; }
footer { display: none !important; }
"""
with gr.Blocks(css=CSS, title="Turkish LLM 14B") as demo:
gr.Markdown("## Turkish LLM 14B Chat\nTurkce egitilmis 14 milyar parametreli acik kaynak dil modeli. Qwen2.5-14B-Instruct uzerine Turkce veri setiyle fine-tune edilmistir.\n\n**MMLU-TR:** 0.5977 **|** **XCOPA-TR:** 0.6400 **|** [Model](https://huggingface.co/ogulcanaydogan/Turkish-LLM-14B-Instruct) **|** [Koleksiyon](https://huggingface.co/collections/ogulcanaydogan/turkish-llm-family-69b303b4ef1c36caffca4e94)")
chatbot = gr.ChatInterface(
fn=respond,
examples=[
"Kuantum bilgisayarlar klasik bilgisayarlardan nasil farklidir?",
"Turkiye\'nin UNESCO Dunya Mirasi listesindeki yerleri nelerdir?",
"Python ile bir web scraper nasil yazilir?",
"Iklim degisikliginin Akdeniz bolgesine etkileri nelerdir?",
],
cache_examples=False,
)
gr.Markdown("---\n*Turkish LLM Family - 7B, 14B, 32B modelleriyle acik kaynak Turkce dil modeli ailesi.*")
demo.launch(ssr_mode=False)