File size: 2,098 Bytes
8b644df
 
 
d1bab71
18ef497
d1bab71
a93c076
 
a04d31e
 
 
 
 
 
a93c076
 
 
 
a04d31e
8b644df
a04d31e
8b644df
a04d31e
a93c076
8b644df
 
 
758f9db
8b644df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f08ab3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import torch
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import spaces



@spaces.GPU(duration=200)
def get_completion(query, model, tokenizer):
    peft_model_id = "rootxhacker/CodeAstra-7B"
    config = PeftConfig.from_pretrained(peft_model_id)
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = AutoModelForCausalLM.from_pretrained(
    config.base_model_name_or_path,
    return_dict=True,
    load_in_4bit=True,
    device_map="auto"  # This will automatically handle device placement
    )

    tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)

    model = PeftModel.from_pretrained(model, peft_model_id)
    inputs = tokenizer(query, return_tensors="pt").to(device)  # Move inputs to the same device as the model
    outputs = model.generate(**inputs, max_new_tokens=512, do_sample=True, temperature=0.7)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

@spaces.GPU(duration=200)
def code_review(code_to_analyze):
    query = f"As a code review expert, your role will be to carefully examine the code for potential security flaws and provide guidance on secure coding practices. This may include identifying common coding mistakes that could lead to vulnerabilities, suggesting ways to improve the code's overall security, and recommending tools or techniques that can be used to detect and prevent potential threats. Your expertise in security will be particularly valuable in ensuring that any code developed meets the highest security standard:\n{code_to_analyze}"
    result = get_completion(query, model, tokenizer)
    return result

# Create Gradio interface
iface = gr.Interface(
    fn=code_review,
    inputs=gr.Textbox(lines=10, label="Enter code to analyze"),
    outputs=gr.Textbox(label="Code Review Result"),
    title="Code Review Expert",
    description="This tool analyzes code for potential security flaws and provides guidance on secure coding practices."
)

# Launch the Gradio app with a public link
iface.launch()