PraneetNS commited on
Commit
9ac77ef
·
verified ·
1 Parent(s): 561ea4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -0
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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__":
144
+ demo.launch()