import spaces import torch import gradio as gr from transformers import AutoModelForCausalLM, AutoTokenizer MODEL = "Qwen/Qwen2.5-7B-Instruct" SYSTEM = "Тебя зовут Profik AI. Ты помощник, отвечаешь на русском языке, понятно и по делу." tok = AutoTokenizer.from_pretrained(MODEL) model = AutoModelForCausalLM.from_pretrained(MODEL, dtype=torch.bfloat16) model.to ("cuda") def txt(c): return c if isinstance(c, str) else "".join(p.get("text", "") for p in c if isinstance(p, dict)) def build(message, history): return [{"role": "system", "content": SYSTEM}] + [{"role": m["role"], "content": txt(m["content"])} for m in history] + [{"role": "user", "content": txt(message)}] @spaces.GPU def chat(message, history): text = tok.apply_chat_template(build(message, history), tokenize=False, add_generation_prompt=True); ids = tok(text, return_tensors="pt").input_ids.to("cuda"); out = model.generate(ids, max_new_tokens=512, do_sample=True, temperature=0.7); return tok.decode(out[0][ids.shape[1]:].cpu(), skip_special_tokens=True) gr.ChatInterface(chat, title="Profik AI").launch()