Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
@@ -10,14 +11,6 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 10 |
trust_remote_code=True
|
| 11 |
)
|
| 12 |
|
| 13 |
-
print("Loading model...")
|
| 14 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
-
MODEL_ID,
|
| 16 |
-
torch_dtype=torch.float16,
|
| 17 |
-
device_map="auto",
|
| 18 |
-
trust_remote_code=True
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
SYSTEM_PROMPT = """You are CodeSentinel.
|
| 22 |
|
| 23 |
You are an expert AI software engineer.
|
|
@@ -34,7 +27,33 @@ Capabilities:
|
|
| 34 |
Never generate malware or unsafe code.
|
| 35 |
"""
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def generate(message, history):
|
|
|
|
|
|
|
| 38 |
messages = [
|
| 39 |
{
|
| 40 |
"role": "system",
|
|
@@ -78,6 +97,7 @@ def generate(message, history):
|
|
| 78 |
|
| 79 |
return response.strip()
|
| 80 |
|
|
|
|
| 81 |
demo = gr.ChatInterface(
|
| 82 |
fn=generate,
|
| 83 |
title="🛡️ CodeSentinel",
|
|
@@ -91,6 +111,7 @@ demo = gr.ChatInterface(
|
|
| 91 |
],
|
| 92 |
)
|
| 93 |
|
|
|
|
| 94 |
if __name__ == "__main__":
|
| 95 |
demo.launch(
|
| 96 |
server_name="0.0.0.0",
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 11 |
trust_remote_code=True
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
SYSTEM_PROMPT = """You are CodeSentinel.
|
| 15 |
|
| 16 |
You are an expert AI software engineer.
|
|
|
|
| 27 |
Never generate malware or unsafe code.
|
| 28 |
"""
|
| 29 |
|
| 30 |
+
# Global model variable
|
| 31 |
+
model = None
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def load_model():
|
| 35 |
+
"""Load the model only once after GPU allocation."""
|
| 36 |
+
global model
|
| 37 |
+
|
| 38 |
+
if model is None:
|
| 39 |
+
print("Loading model...")
|
| 40 |
+
|
| 41 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 42 |
+
MODEL_ID,
|
| 43 |
+
dtype=torch.float16,
|
| 44 |
+
device_map="auto",
|
| 45 |
+
trust_remote_code=True,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
model.eval()
|
| 49 |
+
|
| 50 |
+
return model
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@spaces.GPU
|
| 54 |
def generate(message, history):
|
| 55 |
+
model = load_model()
|
| 56 |
+
|
| 57 |
messages = [
|
| 58 |
{
|
| 59 |
"role": "system",
|
|
|
|
| 97 |
|
| 98 |
return response.strip()
|
| 99 |
|
| 100 |
+
|
| 101 |
demo = gr.ChatInterface(
|
| 102 |
fn=generate,
|
| 103 |
title="🛡️ CodeSentinel",
|
|
|
|
| 111 |
],
|
| 112 |
)
|
| 113 |
|
| 114 |
+
|
| 115 |
if __name__ == "__main__":
|
| 116 |
demo.launch(
|
| 117 |
server_name="0.0.0.0",
|