Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,115 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
---
|
| 4 |
+
This is a stable version of BSJCode, a model capable of fixing as well as optimizing java code.
|
| 5 |
+
|
| 6 |
+
------------------
|
| 7 |
+
## How to use it:
|
| 8 |
+
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
# Load the model and tokenizer
|
| 13 |
+
bnb_config = BitsAndBytesConfig(
|
| 14 |
+
load_in_4bit=True,
|
| 15 |
+
bnb_4bit_quant_type="nf4",
|
| 16 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 17 |
+
bnb_4bit_use_double_quant=True,
|
| 18 |
+
llm_int8_enable_fp32_cpu_offload=True
|
| 19 |
+
)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
"BSAtlas/BSJCode-1-Stable",
|
| 22 |
+
quantization_config=bnb_config,
|
| 23 |
+
device_map="auto"
|
| 24 |
+
).to(device="cuda")
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained("BSAtlas/BSJCode-1-Stable")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def detect_and_fix_bugs(code_snippet):
|
| 29 |
+
# Prepare the prompt
|
| 30 |
+
prompt = f"""You are an expert Java code optimizer and bug fixer.
|
| 31 |
+
Analyze the following code, identify any bugs or inefficiencies,
|
| 32 |
+
and provide an optimized and corrected version:
|
| 33 |
+
|
| 34 |
+
```java
|
| 35 |
+
{code_snippet}
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
Optimized and Fixed Code:"""
|
| 39 |
+
|
| 40 |
+
# Tokenize the input
|
| 41 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
|
| 42 |
+
|
| 43 |
+
# Generate code
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
outputs = model.generate(
|
| 46 |
+
input_ids=inputs['input_ids'],
|
| 47 |
+
attention_mask=inputs['attention_mask'],
|
| 48 |
+
max_length=1024, # Adjust based on your model's training
|
| 49 |
+
num_return_sequences=1,
|
| 50 |
+
do_sample=True,
|
| 51 |
+
temperature=0.7,
|
| 52 |
+
top_k=50,
|
| 53 |
+
top_p=0.95,
|
| 54 |
+
repetition_penalty=1.2
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Decode the output
|
| 58 |
+
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 59 |
+
|
| 60 |
+
# Extract the code portion after the prompt
|
| 61 |
+
code_start = generated_code.find("Optimized and Fixed Code:")
|
| 62 |
+
if code_start != -1:
|
| 63 |
+
fixed_code = generated_code[code_start + len("Optimized and Fixed Code:"):].strip()
|
| 64 |
+
else:
|
| 65 |
+
fixed_code = generated_code
|
| 66 |
+
|
| 67 |
+
return fixed_code
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
sample_code = """
|
| 71 |
+
public class ThreadSafetyExample {
|
| 72 |
+
private int counter = 0;
|
| 73 |
+
|
| 74 |
+
public void increment() {
|
| 75 |
+
// Not thread-safe method
|
| 76 |
+
counter++;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
public int getCounter() {
|
| 80 |
+
return counter;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
public static void main(String[] args) {
|
| 84 |
+
ThreadSafetyExample example = new ThreadSafetyExample();
|
| 85 |
+
|
| 86 |
+
Thread t1 = new Thread(() -> {
|
| 87 |
+
for (int i = 0; i < 1000; i++) {
|
| 88 |
+
example.increment();
|
| 89 |
+
}
|
| 90 |
+
});
|
| 91 |
+
|
| 92 |
+
Thread t2 = new Thread(() -> {
|
| 93 |
+
for (int i = 0; i < 1000; i++) {
|
| 94 |
+
example.increment();
|
| 95 |
+
}
|
| 96 |
+
});
|
| 97 |
+
|
| 98 |
+
t1.start();
|
| 99 |
+
t2.start();
|
| 100 |
+
|
| 101 |
+
try {
|
| 102 |
+
t1.join();
|
| 103 |
+
t2.join();
|
| 104 |
+
} catch (InterruptedException e) {
|
| 105 |
+
e.printStackTrace();
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
System.out.println("Final Counter: " + example.getCounter());
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
"""
|
| 112 |
+
fixed_code = detect_and_fix_bugs(sample_code)
|
| 113 |
+
print(fixed_code)
|
| 114 |
+
--------------------------------------------------------------------------
|
| 115 |
+
|