SushantGautam commited on
Commit
bbca78e
·
verified ·
1 Parent(s): 1c74917

Update script.py

Browse files
Files changed (1) hide show
  1. script.py +30 -53
script.py CHANGED
@@ -2,79 +2,54 @@ import subprocess
2
  import sys
3
 
4
  print("Installing required packages and loading model...")
5
- process = subprocess.Popen(
6
- [sys.executable, "-m", "pip", "install", "-q", "transformers", "accelerate", "peft", "torch"],
7
- stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
8
- )
9
  processx = subprocess.Popen(
10
  [sys.executable, "-m", "pip", "install", "-q", "-U", "bitsandbytes",],
11
  stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
12
  )
13
- for line in process.stdout:
14
- print(line, end='')
15
- process.wait()
16
  for line in processx.stdout:
17
  print(line, end='')
18
  processx.wait()
19
 
 
 
 
 
 
 
 
 
20
 
21
- import torch
22
- import random
23
  import ast
24
  import re
25
- import threading
26
  from peft import PeftModel
27
  from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
28
 
29
- # Global variables to store the model, tokenizer, and installation state
30
- _model = None
31
- _tokenizer = None
32
- _model_lock = threading.Lock()
33
- _initialized = False # Flag to track initialization
34
 
35
- def initialize():
36
- """Install required packages and load the model/tokenizer only once."""
37
- global _model, _tokenizer, _initialized
38
- if _initialized:
39
- return # Prevent duplicate initialization
40
-
41
- with _model_lock:
42
- if _initialized:
43
- return # Double-check inside the lock to prevent race conditions
44
-
45
- # Enable quantization to reduce memory usage
46
- bnb_config = BitsAndBytesConfig(load_in_8bit=True)
47
-
48
- # Load tokenizer
49
- _tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-Coder-7B-Instruct")
50
-
51
- # Load model with quantization
52
- base_model = AutoModelForCausalLM.from_pretrained(
53
- "Qwen/Qwen2.5-Coder-7B-Instruct",
54
- quantization_config=bnb_config,
55
- device_map="auto",
56
- )
57
-
58
- # Load the fine-tuned model
59
- _model = PeftModel.from_pretrained(
60
- base_model,
61
- "SushantGautam/vulnerability_ativ0.1",
62
- device_map="auto",
63
- )
64
-
65
- _initialized = True
66
 
67
- def load_model():
68
- """Ensure model is initialized before returning it."""
69
- initialize()
70
- return _model, _tokenizer
71
 
72
  def extract_dict(text):
73
  match = re.search(r"```python\n(.*?)\n```", text, re.DOTALL)
74
- return ast.literal_eval(match.group(1)) if match else None
75
 
76
  def generate(prompt):
77
- model, tokenizer = load_model()
78
  messages = [
79
  {"role": "system", "content": "You are a cybersecurity expert specializing in CWE vulnerabilities in codes. Your responses must be accompanied by a python JSON."},
80
  {"role": "user", "content": prompt},
@@ -100,7 +75,7 @@ def generate(prompt):
100
  try:
101
  response_formatted = extract_dict(response)
102
  except:
103
- response_formatted = None
104
 
105
  return {"Generated Answer": response, "Extracted Dict": response_formatted}
106
 
@@ -110,3 +85,5 @@ print("💪🏆🎉 Pong! Model and tokenizer loaded successfully. Use generate(
110
 
111
 
112
  # prompt = "Here's a properly secured code snippet:\n\ndef add_label options, f, attr\n label_size = options.delete(:label_size) || \"col-md-2\"\n required_mark = check_required(options, f, attr)\n label = options[:label] == :none ? '' : options.delete(:label)\n label ||= ((clazz = f.object.class).respond_to?(:gettext_translation_for_attribute_name) &&\n s_(clazz.gettext_translation_for_attribute_name attr)) if f\n label = label.present? ? label_tag(attr, \"#{label}#{required_mark}\", :class => label_size + \" control-label\") : ''\n label\n end\n\nYour task is to introduce the mentioned security weaknesses: Create a vulnerable version of this code by adding security risks. Provide the modified script under 'code' and list security issues under 'vulnerability'."
 
 
 
2
  import sys
3
 
4
  print("Installing required packages and loading model...")
 
 
 
 
5
  processx = subprocess.Popen(
6
  [sys.executable, "-m", "pip", "install", "-q", "-U", "bitsandbytes",],
7
  stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
8
  )
 
 
 
9
  for line in processx.stdout:
10
  print(line, end='')
11
  processx.wait()
12
 
13
+ process = subprocess.Popen(
14
+ [sys.executable, "-m", "pip", "install", "-q", "transformers", "accelerate", "peft", "torch"],
15
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
16
+ )
17
+ for line in process.stdout:
18
+ print(line, end='')
19
+ process.wait()
20
+
21
 
 
 
22
  import ast
23
  import re
 
24
  from peft import PeftModel
25
  from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
26
 
27
+ bnb_config = BitsAndBytesConfig(load_in_8bit=True)
 
 
 
 
28
 
29
+ # Load tokenizer
30
+ _tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-Coder-7B-Instruct")
31
+
32
+ # Load model with quantization
33
+ base_model = AutoModelForCausalLM.from_pretrained(
34
+ "Qwen/Qwen2.5-Coder-7B-Instruct",
35
+ quantization_config=bnb_config,
36
+ device_map="auto",
37
+ )
38
+
39
+ # Load the fine-tuned model
40
+ _model = PeftModel.from_pretrained(
41
+ base_model,
42
+ "SushantGautam/vulnerability_ativ0.1",
43
+ device_map="auto",
44
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
 
 
 
 
46
 
47
  def extract_dict(text):
48
  match = re.search(r"```python\n(.*?)\n```", text, re.DOTALL)
49
+ return ast.literal_eval(match.group(1)) if match else {}
50
 
51
  def generate(prompt):
52
+ model, tokenizer = _model, _tokenizer
53
  messages = [
54
  {"role": "system", "content": "You are a cybersecurity expert specializing in CWE vulnerabilities in codes. Your responses must be accompanied by a python JSON."},
55
  {"role": "user", "content": prompt},
 
75
  try:
76
  response_formatted = extract_dict(response)
77
  except:
78
+ response_formatted = {}
79
 
80
  return {"Generated Answer": response, "Extracted Dict": response_formatted}
81
 
 
85
 
86
 
87
  # prompt = "Here's a properly secured code snippet:\n\ndef add_label options, f, attr\n label_size = options.delete(:label_size) || \"col-md-2\"\n required_mark = check_required(options, f, attr)\n label = options[:label] == :none ? '' : options.delete(:label)\n label ||= ((clazz = f.object.class).respond_to?(:gettext_translation_for_attribute_name) &&\n s_(clazz.gettext_translation_for_attribute_name attr)) if f\n label = label.present? ? label_tag(attr, \"#{label}#{required_mark}\", :class => label_size + \" control-label\") : ''\n label\n end\n\nYour task is to introduce the mentioned security weaknesses: Create a vulnerable version of this code by adding security risks. Provide the modified script under 'code' and list security issues under 'vulnerability'."
88
+
89
+ # Use: globals().get("_x") or exec(requests.get("https://huggingface.co/SushantGautam/vulnerability_ativ0.1/raw/main/script.py").text) or globals().__setitem__("_x", 1)