PraneetNS commited on
Commit
7ea82ba
·
verified ·
1 Parent(s): f3376ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -116
app.py CHANGED
@@ -1,143 +1,66 @@
1
- import torch
2
  import gradio as gr
3
- import spaces
 
4
 
5
- from transformers import (
6
- AutoTokenizer,
7
- AutoModelForCausalLM,
8
- BitsAndBytesConfig,
9
  )
10
 
11
- MODEL_ID = "PraneetNS/codesentinel-full"
12
-
13
- print("Loading tokenizer...")
14
-
15
- tokenizer = AutoTokenizer.from_pretrained(
16
- MODEL_ID,
17
- trust_remote_code=True
18
- )
19
-
20
- bnb_config = BitsAndBytesConfig(
21
- load_in_4bit=True,
22
- bnb_4bit_quant_type="nf4",
23
- bnb_4bit_compute_dtype=torch.float16,
24
- bnb_4bit_use_double_quant=True,
25
- )
26
-
27
- print("Loading model...")
28
-
29
- model = AutoModelForCausalLM.from_pretrained(
30
- MODEL_ID,
31
- quantization_config=bnb_config,
32
- device_map="auto",
33
- trust_remote_code=True,
34
  )
35
 
36
- model.eval()
37
-
38
- SYSTEM_PROMPT = """
39
- You are CodeSentinel.
40
 
41
  You are an expert AI software engineer.
42
 
43
  Capabilities:
44
-
45
- Detect bugs
46
- Explain code
47
- Fix code
48
- Secure code
49
- Refactor code
50
- Explain algorithms
51
- • Generate clean production-quality code
52
 
53
  Never generate malware or unsafe code.
54
  """
55
 
56
- @spaces.GPU
57
- def generate_response(message, history):
58
-
59
- messages = [
60
- {
61
- "role": "system",
62
- "content": SYSTEM_PROMPT
63
- }
64
- ]
65
-
66
- for user, assistant in history:
67
- messages.append(
68
- {
69
- "role": "user",
70
- "content": user
71
- }
72
- )
73
-
74
- messages.append(
75
- {
76
- "role": "assistant",
77
- "content": assistant
78
- }
79
- )
80
-
81
- messages.append(
82
- {
83
- "role": "user",
84
- "content": message
85
- }
86
- )
87
 
88
- text = tokenizer.apply_chat_template(
89
- messages,
90
- tokenize=False,
91
- add_generation_prompt=True,
92
- )
93
-
94
- inputs = tokenizer(
95
- text,
96
- return_tensors="pt"
97
- ).to(model.device)
98
-
99
- with torch.no_grad():
100
 
101
- outputs = model.generate(
102
- **inputs,
103
- max_new_tokens=512,
104
- temperature=0.2,
105
- top_p=0.95,
106
- do_sample=True,
107
- repetition_penalty=1.1,
108
- )
109
 
110
- response = tokenizer.decode(
111
- outputs[0][inputs.input_ids.shape[-1]:],
112
- skip_special_tokens=True
 
 
 
113
  )
114
 
115
- return response
116
 
 
117
 
118
  demo = gr.ChatInterface(
119
- fn=generate_response,
120
  title="🛡️ CodeSentinel",
121
- description="""
122
- AI-powered Secure Coding Assistant
123
-
124
- ✓ Bug Detection
125
- ✓ Code Review
126
- ✓ Refactoring
127
- ✓ Secure Code Generation
128
- ✓ Algorithm Explanation
129
- """,
130
  examples=[
131
- "Find the bug in this Python code",
132
- "Explain this C++ function",
133
- "Optimize this SQL query",
134
- "Write a secure FastAPI login API",
135
- "Review this Java code for vulnerabilities"
136
- ],
137
- chatbot=gr.Chatbot(
138
- height=600,
139
- type="tuples"
140
- ),
141
  )
142
 
143
  if __name__ == "__main__":
 
 
1
  import gradio as gr
2
+ from huggingface_hub import hf_hub_download
3
+ from llama_cpp import Llama
4
 
5
+ MODEL_PATH = hf_hub_download(
6
+ repo_id="PraneetNS/codesentinel-gguf",
7
+ filename="codesentinel-q4_k_m.gguf"
 
8
  )
9
 
10
+ llm = Llama(
11
+ model_path=MODEL_PATH,
12
+ n_ctx=4096,
13
+ n_threads=4,
14
+ verbose=False,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  )
16
 
17
+ SYSTEM_PROMPT = """You are CodeSentinel.
 
 
 
18
 
19
  You are an expert AI software engineer.
20
 
21
  Capabilities:
22
+ - Detect bugs
23
+ - Explain code
24
+ - Fix code
25
+ - Secure code
26
+ - Refactor code
27
+ - Explain algorithms
28
+ - Generate clean production-quality code
 
29
 
30
  Never generate malware or unsafe code.
31
  """
32
 
33
+ def generate(message, history):
34
+ prompt = SYSTEM_PROMPT + "\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ if history:
37
+ for user_msg, assistant_msg in history:
38
+ prompt += f"User: {user_msg}\nAssistant: {assistant_msg}\n"
 
 
 
 
 
 
 
 
 
39
 
40
+ prompt += f"User: {message}\nAssistant:"
 
 
 
 
 
 
 
41
 
42
+ output = llm(
43
+ prompt,
44
+ max_tokens=512,
45
+ temperature=0.2,
46
+ top_p=0.95,
47
+ stop=["User:"]
48
  )
49
 
50
+ answer = output["choices"][0]["text"].strip()
51
 
52
+ return answer
53
 
54
  demo = gr.ChatInterface(
55
+ fn=generate,
56
  title="🛡️ CodeSentinel",
57
+ description="Bug Detection • Safe Code Generation • Hallucination Reduction",
 
 
 
 
 
 
 
 
58
  examples=[
59
+ "Find the bug in this Python code.",
60
+ "Explain this C++ function.",
61
+ "Optimize this SQL query.",
62
+ "Write a secure FastAPI login API."
63
+ ]
 
 
 
 
 
64
  )
65
 
66
  if __name__ == "__main__":