Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,68 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from unsloth import FastLanguageModel
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
|
| 5 |
+
# Function to load model and tokenizer
|
| 6 |
+
def load_model(hf_token):
|
| 7 |
+
try:
|
| 8 |
+
# Initialize the model using ZeroGPU (to run on CPU in Hugging Face environment)
|
| 9 |
+
model_name = "shukdevdatta123/sql_injection_classifier_DeepSeek_R1_fine_tuned_model"
|
| 10 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 11 |
+
model_name=model_name,
|
| 12 |
+
load_in_4bit=True,
|
| 13 |
+
token=hf_token,
|
| 14 |
+
use_zero=True, # Ensure ZeroGPU usage
|
| 15 |
+
)
|
| 16 |
+
return model, tokenizer
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return None, str(e)
|
| 19 |
|
| 20 |
+
# Function to predict SQL injection
|
| 21 |
+
def predict_sql_injection(query, hf_token):
|
| 22 |
+
model, tokenizer = load_model(hf_token)
|
| 23 |
+
|
| 24 |
+
if model is None:
|
| 25 |
+
return f"Error loading model: {tokenizer}"
|
| 26 |
+
|
| 27 |
+
# Prepare the model for inference
|
| 28 |
+
inference_model = FastLanguageModel.for_inference(model)
|
| 29 |
+
|
| 30 |
+
prompt = f"### Instruction:\nClassify the following SQL query as normal (0) or an injection attack (1).\n\n### Query:\n{query}\n\n### Classification:\n"
|
| 31 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 32 |
+
|
| 33 |
+
# Use the inference model for generation
|
| 34 |
+
outputs = inference_model.generate(
|
| 35 |
+
input_ids=inputs.input_ids,
|
| 36 |
+
attention_mask=inputs.attention_mask,
|
| 37 |
+
max_new_tokens=1000,
|
| 38 |
+
use_cache=True,
|
| 39 |
+
)
|
| 40 |
+
prediction = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 41 |
+
return prediction.split("### Classification:\n")[-1].strip()
|
| 42 |
+
|
| 43 |
+
# Gradio UI
|
| 44 |
+
def classify_sql_injection(query, hf_token):
|
| 45 |
+
if not hf_token:
|
| 46 |
+
return "Please enter your Hugging Face token."
|
| 47 |
+
|
| 48 |
+
if not query:
|
| 49 |
+
return "Please enter a SQL query first."
|
| 50 |
+
|
| 51 |
+
result = predict_sql_injection(query, hf_token)
|
| 52 |
+
return f"Prediction: {result}"
|
| 53 |
+
|
| 54 |
+
# Gradio interface
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
fn=classify_sql_injection,
|
| 57 |
+
inputs=[
|
| 58 |
+
gr.Textbox(label="SQL Query", placeholder="Enter SQL query here..."),
|
| 59 |
+
gr.Textbox(label="Hugging Face Token", type="password")
|
| 60 |
+
],
|
| 61 |
+
outputs="text",
|
| 62 |
+
live=True,
|
| 63 |
+
title="SQL Injection Classifier",
|
| 64 |
+
description="Enter an SQL query and your Hugging Face token to classify whether the query is a normal SQL query or a SQL injection attack."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Launch the Gradio app
|
| 68 |
+
iface.launch()
|