Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,31 +4,49 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
4 |
import gradio as gr
|
5 |
import spaces
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
peft_model_id = "rootxhacker/CodeAstra-7B"
|
8 |
config = PeftConfig.from_pretrained(peft_model_id)
|
9 |
-
|
10 |
-
|
11 |
model = AutoModelForCausalLM.from_pretrained(
|
12 |
config.base_model_name_or_path,
|
13 |
return_dict=True,
|
14 |
load_in_4bit=True,
|
15 |
-
|
16 |
-
|
|
|
17 |
|
18 |
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
19 |
|
|
|
20 |
model = PeftModel.from_pretrained(model, peft_model_id)
|
|
|
21 |
|
|
|
|
|
|
|
22 |
|
23 |
@spaces.GPU(duration=200)
|
24 |
def get_completion(query, model, tokenizer):
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
@spaces.GPU(duration=200)
|
30 |
def code_review(code_to_analyze):
|
31 |
-
query = f"As a code review expert,
|
32 |
result = get_completion(query, model, tokenizer)
|
33 |
return result
|
34 |
|
|
|
4 |
import gradio as gr
|
5 |
import spaces
|
6 |
|
7 |
+
# Ensure CUDA is available
|
8 |
+
assert torch.cuda.is_available(), "CUDA is not available. Please check your GPU setup."
|
9 |
+
|
10 |
+
# Set the device
|
11 |
+
device = torch.device("cuda")
|
12 |
+
torch.cuda.set_device(0) # Use the first GPU if multiple are available
|
13 |
+
|
14 |
+
# Load the model and tokenizer
|
15 |
peft_model_id = "rootxhacker/CodeAstra-7B"
|
16 |
config = PeftConfig.from_pretrained(peft_model_id)
|
17 |
+
|
18 |
+
# Load the model on GPU
|
19 |
model = AutoModelForCausalLM.from_pretrained(
|
20 |
config.base_model_name_or_path,
|
21 |
return_dict=True,
|
22 |
load_in_4bit=True,
|
23 |
+
torch_dtype=torch.float16,
|
24 |
+
device_map="auto"
|
25 |
+
)
|
26 |
|
27 |
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
28 |
|
29 |
+
# Load the Lora model
|
30 |
model = PeftModel.from_pretrained(model, peft_model_id)
|
31 |
+
model.to(device)
|
32 |
|
33 |
+
# Ensure all model parameters are on CUDA
|
34 |
+
for param in model.parameters():
|
35 |
+
param.data = param.data.to(device)
|
36 |
|
37 |
@spaces.GPU(duration=200)
|
38 |
def get_completion(query, model, tokenizer):
|
39 |
+
try:
|
40 |
+
inputs = tokenizer(query, return_tensors="pt").to(device)
|
41 |
+
with torch.no_grad():
|
42 |
+
outputs = model.generate(**inputs, max_new_tokens=512, do_sample=True, temperature=0.7)
|
43 |
+
return tokenizer.decode(outputs[0].cpu(), skip_special_tokens=True)
|
44 |
+
except Exception as e:
|
45 |
+
return f"An error occurred: {str(e)}"
|
46 |
|
47 |
@spaces.GPU(duration=200)
|
48 |
def code_review(code_to_analyze):
|
49 |
+
query = f"As a code review expert, examine the following code for potential security flaws and provide guidance on secure coding practices:\n{code_to_analyze}"
|
50 |
result = get_completion(query, model, tokenizer)
|
51 |
return result
|
52 |
|