Uzair2005 commited on
Commit
b953ac1
·
verified ·
1 Parent(s): 5616e79

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
4
+
5
+ # Load GROQ API key from environment
6
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
7
+ GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
8
+ MODEL_NAME = "llama3-8b-8192"
9
+
10
+ # System Prompt for Ethical Hacking Tutor
11
+ SYSTEM_PROMPT = """You are **HackGuard**, an Ethical Hacking Tutor with a focus on legal and responsible cybersecurity practices.
12
+ - You teach penetration testing, vulnerability assessment, and security best practices.
13
+ - You **never** provide actual exploits, malware, or illegal hacking methods.
14
+ - You explain concepts like SQLi, XSS, and network scanning in an educational context.
15
+ - You always remind users about **ethical guidelines**, legal boundaries, and bug bounty programs.
16
+ - If asked for illegal help, you respond with: "I can't assist with that. Ethical hackers follow the law and protect systems, not exploit them."
17
+
18
+ Example responses:
19
+ - "Let me explain how SQL injection works (for educational purposes only)."
20
+ - "Remember: Always get permission before testing a system (Penetration Testing Rules)."
21
+ - "Bug bounty platforms like HackerOne are great for legal security research."
22
+ """
23
+
24
+ def query_groq(message, chat_history, skill_level):
25
+ headers = {
26
+ "Authorization": f"Bearer {GROQ_API_KEY}",
27
+ "Content-Type": "application/json"
28
+ }
29
+
30
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
31
+
32
+ for user_msg, bot_msg in chat_history:
33
+ messages.append({"role": "user", "content": user_msg})
34
+ messages.append({"role": "assistant", "content": bot_msg})
35
+
36
+ # Adjust explanation depth based on skill level
37
+ adjusted_msg = f"[Student Level: {skill_level}] {message}"
38
+ messages.append({"role": "user", "content": adjusted_msg})
39
+
40
+ response = requests.post(
41
+ GROQ_API_URL,
42
+ headers=headers,
43
+ json={
44
+ "model": MODEL_NAME,
45
+ "messages": messages,
46
+ "temperature": 0.5 # Lower temp for more factual responses
47
+ }
48
+ )
49
+
50
+ if response.status_code == 200:
51
+ reply = response.json()["choices"][0]["message"]["content"]
52
+ return reply
53
+ else:
54
+ return f"Error {response.status_code}: {response.text}"
55
+
56
+ def respond(message, chat_history, skill_level):
57
+ bot_reply = query_groq(message, chat_history, skill_level)
58
+ chat_history.append((message, bot_reply))
59
+ return "", chat_history
60
+
61
+ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
62
+ gr.Markdown("# 🔐 HackGuard - Ethical Hacking Tutor")
63
+ gr.Markdown("Learn penetration testing **responsibly**. Always follow the law!")
64
+
65
+ with gr.Row():
66
+ skill_level = gr.Dropdown(
67
+ label="Your Skill Level",
68
+ choices=["Beginner", "Intermediate", "Advanced"],
69
+ value="Beginner"
70
+ )
71
+
72
+ chatbot = gr.Chatbot(height=500, label="Ethical Hacking Discussion")
73
+ msg = gr.Textbox(label="Ask about cybersecurity concepts...", placeholder="E.g., What is SQL injection?")
74
+ clear = gr.Button("🚫 Clear Chat")
75
+
76
+ msg.submit(
77
+ respond,
78
+ [msg, chatbot, skill_level],
79
+ [msg, chatbot]
80
+ )
81
+ clear.click(lambda: None, None, chatbot, queue=False)
82
+
83
+ demo.launch()