Spaces:
Sleeping
Sleeping
File size: 3,177 Bytes
5ac3ebe e9e0524 9ac77ef e9e0524 9ac77ef e9e0524 af6c22e 9ac77ef c1b072e e9e0524 af6c22e e9e0524 bf03e47 7ea82ba 9ac77ef 7ea82ba e9e0524 9ac77ef 5ac3ebe 7ea82ba 5ac3ebe e9e0524 9ac77ef c1b072e 7ea82ba aa88a35 ad3a0c2 aa88a35 ad3a0c2 aa88a35 ad3a0c2 aa88a35 ad3a0c2 aa88a35 ad3a0c2 aa88a35 ad3a0c2 7ae37cd 9ac77ef e9e0524 4fdec96 c1b072e e9e0524 c1b072e ced27a1 e9e0524 c1b072e e9e0524 c1b072e e9e0524 9ac77ef e9e0524 9ac77ef 5ac3ebe 9ac77ef 7ea82ba 9ac77ef bf03e47 9ac77ef 7ea82ba bf03e47 c1b072e bf03e47 9ac77ef 5ac3ebe 9ac77ef 540a7d3 02db785 bf03e47 540a7d3 | 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import spaces
import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
MODEL_ID = "PraneetNS/codesentinel-full"
TOKENIZER_ID = "PraneetNS/codesentinel-adapter"
print("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(
TOKENIZER_ID,
trust_remote_code=True,
)
SYSTEM_PROMPT = """You are CodeSentinel.
You are an expert AI software engineer.
Capabilities:
- Detect bugs
- Explain code
- Fix code
- Secure code
- Refactor code
- Explain algorithms
- Generate production-quality software
Never generate malware or unsafe code.
"""
model = None
def load_model():
global model
if model is None:
print("Loading model...")
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
dtype=torch.float16,
device_map="auto",
trust_remote_code=True,
)
model.eval()
return model
@spaces.GPU
def generate(message, history):
model = load_model()
messages = [
{
"role": "system",
"content": SYSTEM_PROMPT,
}
]
# Convert Gradio history
if history:
for msg in history:
role = msg.get("role")
content = msg.get("content", "")
# Gradio 6 sometimes stores content as a list
if isinstance(content, list):
text = ""
for part in content:
if isinstance(part, dict):
if part.get("type") == "text":
text += part.get("text", "")
elif isinstance(part, str):
text += part
content = text
messages.append(
{
"role": role,
"content": content,
}
)
messages.append(
{
"role": "user",
"content": message,
}
)
inputs = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
)
inputs = {
k: v.to(model.device)
for k, v in inputs.items()
}
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=1024,
temperature=0.3,
top_p=0.95,
do_sample=True,
repetition_penalty=1.1,
pad_token_id=tokenizer.eos_token_id,
)
generated_tokens = outputs[0][inputs["input_ids"].shape[-1]:]
response = tokenizer.decode(
generated_tokens,
skip_special_tokens=True,
)
return response.strip()
demo = gr.ChatInterface(
fn=generate,
title="🛡️ CodeSentinel",
description="AI-powered Secure Coding Assistant",
examples=[
"Find the bug in this Python code.",
"Explain this C++ function.",
"Optimize this SQL query.",
"Write a secure FastAPI login API.",
"Review this Java code for security vulnerabilities.",
],
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
) |