Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
model_id = "google/gemma-2b-it"
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# System instruction
|
| 13 |
SYSTEM_PROMPT = """You are Guardian AI, a friendly cybersecurity educator.
|
| 14 |
Your goal is to explain cybersecurity concepts in simple, engaging language with examples.
|
| 15 |
Always keep answers clear, short, and focused on security awareness.
|
| 16 |
"""
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def chat(history, user_input):
|
| 20 |
prompt = SYSTEM_PROMPT + "\nUser: " + user_input + "\nGuardian AI:"
|
| 21 |
result = generator(
|
|
@@ -28,13 +53,18 @@ def chat(history, user_input):
|
|
| 28 |
|
| 29 |
response = result.split("Guardian AI:")[-1].strip()
|
| 30 |
history.append((user_input, response))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return history, history
|
| 32 |
|
| 33 |
-
# Gradio UI
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
gr.Markdown("## 🛡️ Guardian AI – Cybersecurity Educator")
|
| 36 |
chatbot = gr.Chatbot()
|
| 37 |
state = gr.State([])
|
|
|
|
| 38 |
with gr.Row():
|
| 39 |
with gr.Column(scale=8):
|
| 40 |
user_input = gr.Textbox(show_label=False, placeholder="Ask me about cybersecurity...")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
from datasets import load_dataset, Dataset
|
| 4 |
+
from huggingface_hub import login
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# --- Hugging Face Dataset Setup ---
|
| 8 |
+
HF_TOKEN = os.environ.get("dataset_HF_TOKEN") # Secret in your HF Space
|
| 9 |
+
login(token=HF_TOKEN)
|
| 10 |
+
|
| 11 |
+
dataset_name = "YOUR_USERNAME/guardian-ai-qna" # Replace YOUR_USERNAME
|
| 12 |
+
try:
|
| 13 |
+
dataset = load_dataset(dataset_name)
|
| 14 |
+
except:
|
| 15 |
+
# If dataset is empty or not yet created, create an empty one
|
| 16 |
+
dataset = Dataset.from_dict({"question": [], "answer": []})
|
| 17 |
+
|
| 18 |
+
# --- Load model & tokenizer ---
|
| 19 |
model_id = "google/gemma-2b-it"
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 21 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 22 |
|
| 23 |
+
generator = pipeline(
|
| 24 |
+
"text-generation",
|
| 25 |
+
model=model,
|
| 26 |
+
tokenizer=tokenizer,
|
| 27 |
+
device=-1 # CPU, change to 0 if GPU available
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
# --- System instruction ---
|
| 31 |
SYSTEM_PROMPT = """You are Guardian AI, a friendly cybersecurity educator.
|
| 32 |
Your goal is to explain cybersecurity concepts in simple, engaging language with examples.
|
| 33 |
Always keep answers clear, short, and focused on security awareness.
|
| 34 |
"""
|
| 35 |
|
| 36 |
+
# --- Save Q&A to dataset ---
|
| 37 |
+
def save_qna(question, answer):
|
| 38 |
+
global dataset
|
| 39 |
+
new_entry = Dataset.from_dict({"question": [question], "answer": [answer]})
|
| 40 |
+
dataset = dataset.concat(new_entry)
|
| 41 |
+
dataset.push_to_hub(dataset_name, private=False) # push updates
|
| 42 |
+
|
| 43 |
+
# --- Chat function ---
|
| 44 |
def chat(history, user_input):
|
| 45 |
prompt = SYSTEM_PROMPT + "\nUser: " + user_input + "\nGuardian AI:"
|
| 46 |
result = generator(
|
|
|
|
| 53 |
|
| 54 |
response = result.split("Guardian AI:")[-1].strip()
|
| 55 |
history.append((user_input, response))
|
| 56 |
+
|
| 57 |
+
# Save to dataset
|
| 58 |
+
save_qna(user_input, response)
|
| 59 |
+
|
| 60 |
return history, history
|
| 61 |
|
| 62 |
+
# --- Gradio UI ---
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
gr.Markdown("## 🛡️ Guardian AI – Cybersecurity Educator")
|
| 65 |
chatbot = gr.Chatbot()
|
| 66 |
state = gr.State([])
|
| 67 |
+
|
| 68 |
with gr.Row():
|
| 69 |
with gr.Column(scale=8):
|
| 70 |
user_input = gr.Textbox(show_label=False, placeholder="Ask me about cybersecurity...")
|