Create model..py
Browse files
model..py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
# Function to load the model
|
| 4 |
+
def load_model(model_name):
|
| 5 |
+
try:
|
| 6 |
+
# Load the model from Hugging Face or local storage (by name)
|
| 7 |
+
model = pipeline("text-classification", model=model_name)
|
| 8 |
+
return model
|
| 9 |
+
except Exception as e:
|
| 10 |
+
print(f"Error loading model: {e}")
|
| 11 |
+
return None
|
| 12 |
+
|
| 13 |
+
# Function to run inference using the selected model
|
| 14 |
+
def run_inference(user_input, selected_model, prompt=None):
|
| 15 |
+
model = load_model(selected_model)
|
| 16 |
+
if model:
|
| 17 |
+
# If a prompt is provided, prepend it to the input text
|
| 18 |
+
if prompt:
|
| 19 |
+
input_text = f"{prompt}\n{user_input}"
|
| 20 |
+
else:
|
| 21 |
+
input_text = user_input
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
# Run inference and check model output
|
| 25 |
+
result = model(input_text)
|
| 26 |
+
|
| 27 |
+
# Assuming the output format is a list of dicts with 'label' field
|
| 28 |
+
return result[0]['label'] if 'label' in result[0] else "Error: No label in output"
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error during inference: {e}"
|
| 31 |
+
else:
|
| 32 |
+
return f"Error: Model '{selected_model}' failed to load."
|
| 33 |
+
|
| 34 |
+
# Example usage
|
| 35 |
+
selected_model = "Canstralian/CySec_Known_Exploit_Analyzer"
|
| 36 |
+
user_input = "Sample exploit description"
|
| 37 |
+
prompt = "Classify the following cybersecurity exploit:"
|
| 38 |
+
|
| 39 |
+
# Run inference
|
| 40 |
+
result = run_inference(user_input, selected_model, prompt)
|
| 41 |
+
print(f"Inference Result: {result}")
|