Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
from transformers import pipeline
|
| 4 |
-
from typing import List, Dict
|
| 5 |
|
| 6 |
-
# Initialize the
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
def generate_attack(
|
| 10 |
-
prompt: str,
|
| 11 |
-
history: List[Dict[str, str]],
|
| 12 |
-
) -> List[str]:
|
| 13 |
"""
|
| 14 |
Simulates a Blackhat AI scenario by generating attack strategies and potential impacts.
|
| 15 |
-
|
| 16 |
Args:
|
| 17 |
prompt (str): The user's input to the simulator.
|
| 18 |
history (List[Dict]): The user's message history with timestamps.
|
| 19 |
-
|
| 20 |
Returns:
|
| 21 |
List[str]: A list of attack responses from the AI.
|
| 22 |
"""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
for val in history:
|
| 31 |
if "user" in val:
|
| 32 |
messages.append({"role": "user", "content": val["user"]})
|
|
@@ -34,19 +35,27 @@ def generate_attack(
|
|
| 34 |
messages.append({"role": "assistant", "content": val["assistant"]})
|
| 35 |
|
| 36 |
messages.append({"role": "user", "content": prompt})
|
| 37 |
-
|
| 38 |
-
# Use Hugging Face's Inference API to generate responses
|
| 39 |
-
generator = pipeline("text-generation", model="your_huggingface_model_name")
|
| 40 |
-
response = generator(messages[-1]["content"], max_length=100)
|
| 41 |
-
|
| 42 |
-
return [response[0]['generated_text']] # Return the generated text
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
demo = gr.Interface(
|
| 45 |
-
fn=generate_attack,
|
| 46 |
-
inputs=[
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
title="Blackhat AI Simulator",
|
| 49 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from typing import List, Dict
|
| 4 |
|
| 5 |
+
# Initialize the Hugging Face pipeline (make sure to replace with your model name)
|
| 6 |
+
model_name = "your_huggingface_model_name"
|
| 7 |
+
try:
|
| 8 |
+
generator = pipeline("text-generation", model=model_name)
|
| 9 |
+
except Exception as e:
|
| 10 |
+
raise ValueError(f"Error initializing the model '{model_name}': {e}")
|
| 11 |
|
| 12 |
+
def generate_attack(prompt: str, history: List[Dict[str, str]]) -> List[str]:
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
Simulates a Blackhat AI scenario by generating attack strategies and potential impacts.
|
| 15 |
+
|
| 16 |
Args:
|
| 17 |
prompt (str): The user's input to the simulator.
|
| 18 |
history (List[Dict]): The user's message history with timestamps.
|
| 19 |
+
|
| 20 |
Returns:
|
| 21 |
List[str]: A list of attack responses from the AI.
|
| 22 |
"""
|
| 23 |
+
# Validate inputs
|
| 24 |
+
if not prompt.strip():
|
| 25 |
+
return ["Error: Prompt cannot be empty."]
|
| 26 |
+
if not isinstance(history, list) or not all(isinstance(h, dict) for h in history):
|
| 27 |
+
return ["Error: History must be a list of dictionaries."]
|
| 28 |
+
|
| 29 |
+
# Prepare messages for the AI
|
| 30 |
+
messages = [{"role": "system", "content": f"Responding to {prompt}..."}]
|
| 31 |
for val in history:
|
| 32 |
if "user" in val:
|
| 33 |
messages.append({"role": "user", "content": val["user"]})
|
|
|
|
| 35 |
messages.append({"role": "assistant", "content": val["assistant"]})
|
| 36 |
|
| 37 |
messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Generate a response using the Hugging Face model
|
| 40 |
+
try:
|
| 41 |
+
response = generator(messages[-1]["content"], max_length=100, num_return_sequences=1)
|
| 42 |
+
return [response[0]["generated_text"]]
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return [f"Error generating response: {e}"]
|
| 45 |
+
|
| 46 |
+
# Define the Gradio interface
|
| 47 |
demo = gr.Interface(
|
| 48 |
+
fn=generate_attack,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Textbox(label="Prompt", placeholder="Enter your simulation prompt here..."),
|
| 51 |
+
gr.Dataframe(headers=["user", "assistant"], label="Message History", type="array")
|
| 52 |
+
],
|
| 53 |
+
outputs=gr.Textbox(label="Generated Response"),
|
| 54 |
title="Blackhat AI Simulator",
|
| 55 |
+
description=(
|
| 56 |
+
"This simulator generates adversarial scenarios, analyzes attack vectors, "
|
| 57 |
+
"and provides ethical countermeasures. Use responsibly for cybersecurity training and awareness."
|
| 58 |
+
)
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|