Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,37 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from peft import PeftModel, PeftConfig
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Load the model and tokenizer
|
7 |
+
peft_model_id = "rootxhacker/CodeAstra-7B"
|
8 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_4bit=True, device_map='auto')
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
11 |
+
|
12 |
+
# Load the Lora model
|
13 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
14 |
+
|
15 |
+
model.config.to_json_file("config.json")
|
16 |
+
|
17 |
+
def get_completion(query, model, tokenizer):
|
18 |
+
inputs = tokenizer(query, return_tensors="pt")
|
19 |
+
outputs = model.generate(**inputs, max_new_tokens=512, do_sample=True, temperature=0.7)
|
20 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
+
|
22 |
+
def code_review(code_to_analyze):
|
23 |
+
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}"
|
24 |
+
result = get_completion(query, model, tokenizer)
|
25 |
+
return result
|
26 |
+
|
27 |
+
# Create Gradio interface
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=code_review,
|
30 |
+
inputs=gr.Textbox(lines=10, label="Enter code to analyze"),
|
31 |
+
outputs=gr.Textbox(label="Code Review Result"),
|
32 |
+
title="Code Review Expert",
|
33 |
+
description="This tool analyzes code for potential security flaws and provides guidance on secure coding practices."
|
34 |
+
)
|
35 |
+
|
36 |
+
# Launch the Gradio app with a public link
|
37 |
+
iface.launch(share=True)
|