import json
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
model_path = "Vilyam888/Broken_Code_Generation.1.0"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "left"
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.bfloat16 if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else (
torch.float16 if torch.cuda.is_available() else torch.float32
),
device_map="auto",
trust_remote_code=True,
)
SYSTEM_PROMPT = (
"Ты генерируешь новую ML bugfix-задачу строго в формате объектов из датасета. "
"Верни только один JSON-объект без Markdown и без пояснений. "
"Порядок полей должен быть ровно таким: "
"`title`, `difficulty`, `topic_tags`, `task_context`, `tests`, "
"`expected_output`, `input_example`, `output_example`, `requirements`, "
"`constraints`, `broken_code`. "
"`tests`, `requirements` и `constraints` должны быть массивами строк. "
"`broken_code` должен быть одной строкой с полным Python-кодом и символами `\\n`. "
"Не добавляй лишние поля и не обрывай JSON."
)
topic_tags = {
"TabularData": 0.4,
"Statistics": 0.3,
"DataPreprocessing": 0.3,
}
payload = {
"difficulty": "medium",
"topic_tags": topic_tags,
}
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{
"role": "user",
"content": (
"Сгенерируй новую ML bugfix-задачу по параметрам.\n"
"Формат должен совпадать со структурой датасета: "
"все поля обязательны, `tests`/`requirements`/`constraints` - это списки строк, "
"`broken_code` - полная строка кода с ошибками и комментариями `ВОТ ТУТ НУЖНО ИСПРАВИТЬ КОД`.\n"
"Поля должны идти в порядке: "
"title, difficulty, topic_tags, task_context, tests, expected_output, "
"input_example, output_example, requirements, constraints, broken_code.\n"
+ json.dumps(payload, ensure_ascii=False, indent=2)
),
},
]
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
prompt_length = inputs["input_ids"].shape[1]
with torch.no_grad():
output = model.generate(
**inputs,
max_new_tokens=1200,
temperature=0.7,
top_p=0.95,
do_sample=True,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)
completion_tokens = output[0][prompt_length:]
completion = tokenizer.decode(completion_tokens, skip_special_tokens=True).strip()
print(completion)