Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,143 +1,66 @@
|
|
| 1 |
-
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
BitsAndBytesConfig,
|
| 9 |
)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 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 |
-
|
| 37 |
-
|
| 38 |
-
SYSTEM_PROMPT = """
|
| 39 |
-
You are CodeSentinel.
|
| 40 |
|
| 41 |
You are an expert AI software engineer.
|
| 42 |
|
| 43 |
Capabilities:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
• Generate clean production-quality code
|
| 52 |
|
| 53 |
Never generate malware or unsafe code.
|
| 54 |
"""
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 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 |
-
|
| 89 |
-
|
| 90 |
-
|
| 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 |
-
|
| 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 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
|
| 115 |
-
|
| 116 |
|
|
|
|
| 117 |
|
| 118 |
demo = gr.ChatInterface(
|
| 119 |
-
fn=
|
| 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 |
-
|
| 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__":
|