Spaces:
Sleeping
Sleeping
| def generate_code(prompt, language): | |
| full_prompt = f""" | |
| You are an expert {language} programmer. | |
| Write clean, correct, and complete code. | |
| Requirements: | |
| - No explanations | |
| - Only code | |
| - Proper syntax | |
| - Complete function/program | |
| Task: | |
| {prompt} | |
| Code: | |
| """ | |
| inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True) | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=300, | |
| do_sample=True, # β IMPORTANT | |
| temperature=0.2, # β slight creativity | |
| top_p=0.9, # β better coherence | |
| repetition_penalty=1.1, # β avoid loops | |
| eos_token_id=tokenizer.eos_token_id, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| result = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| # Remove prompt part safely | |
| if "Code:" in result: | |
| result = result.split("Code:")[-1] | |
| return clean_code(result) |