Ali231a commited on
Commit
8d9c3ff
·
verified ·
1 Parent(s): 03bf2e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -9,7 +9,7 @@ HF_API_TOKEN = os.getenv("HF_API_TOKEN")
9
  # Function to query the Hugging Face model
10
  def query_hf(prompt):
11
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
12
- payload = {"inputs": prompt, "parameters": {"max_new_tokens": 300}}
13
  try:
14
  response = requests.post(
15
  f"https://api-inference.huggingface.co/models/{MODEL_NAME}",
@@ -20,36 +20,46 @@ def query_hf(prompt):
20
  data = response.json()
21
  # Handle different response formats
22
  if isinstance(data, list) and "generated_text" in data[0]:
23
- return data[0]["generated_text"]
24
  elif isinstance(data, dict) and "generated_text" in data:
25
- return data["generated_text"]
26
  else:
27
- return str(data) # Fallback to string representation
28
  except Exception as e:
29
  return f"Error querying model: {str(e)}"
30
 
31
  # Chat function for Gradio
32
  def chat_fn(message, history):
33
- # Convert history to a prompt with context
34
- prompt = ""
35
- for user_msg, assistant_msg in history:
36
- prompt += f"User: {user_msg}\nAssistant: {assistant_msg}\n"
 
 
 
 
37
  prompt += f"User: {message}\nAssistant: "
38
 
39
  # Get response from the model
40
  response = query_hf(prompt)
41
 
42
- # Return in messages format
43
- return [{"role": "user", "content": message}, {"role": "assistant", "content": response}]
44
 
45
  # Create Gradio interface
46
- demo = gr.ChatInterface(
47
- fn=chat_fn,
48
- chatbot=gr.Chatbot(type="messages"), # Use messages format
49
- title="Pentest Assistant",
50
- description="Your AI-powered assistant for penetration testing and cybersecurity tasks.",
51
- theme="soft"
52
- )
 
 
 
 
 
 
53
 
54
- # Launch the app
55
  demo.launch()
 
9
  # Function to query the Hugging Face model
10
  def query_hf(prompt):
11
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
12
+ payload = {"inputs": prompt, "parameters": {"max_new_tokens": 300, "return_full_text": False}}
13
  try:
14
  response = requests.post(
15
  f"https://api-inference.huggingface.co/models/{MODEL_NAME}",
 
20
  data = response.json()
21
  # Handle different response formats
22
  if isinstance(data, list) and "generated_text" in data[0]:
23
+ return data[0]["generated_text"].strip()
24
  elif isinstance(data, dict) and "generated_text" in data:
25
+ return data["generated_text"].strip()
26
  else:
27
+ return str(data).strip() # Fallback to string representation
28
  except Exception as e:
29
  return f"Error querying model: {str(e)}"
30
 
31
  # Chat function for Gradio
32
  def chat_fn(message, history):
33
+ # Initialize history if empty
34
+ if not history:
35
+ history = []
36
+
37
+ # Create prompt with history
38
+ prompt = "You are a cybersecurity expert specializing in penetration testing. Provide clear, ethical, and actionable steps.\n"
39
+ for msg in history:
40
+ prompt += f"User: {msg['user']}\nAssistant: {msg['assistant']}\n"
41
  prompt += f"User: {message}\nAssistant: "
42
 
43
  # Get response from the model
44
  response = query_hf(prompt)
45
 
46
+ # Return user and assistant messages as dictionaries
47
+ return {"user": message, "assistant": response}
48
 
49
  # Create Gradio interface
50
+ with gr.Blocks() as demo:
51
+ chatbot = gr.Chatbot(type="messages")
52
+ msg = gr.Textbox(placeholder="Ask about pentesting (e.g., 'How do I scan with Nmap?')")
53
+ clear = gr.Button("Clear Chat")
54
+
55
+ def submit_message(message, chatbot):
56
+ history = chatbot if chatbot else []
57
+ response = chat_fn(message, history)
58
+ history.append(response)
59
+ return history, ""
60
+
61
+ msg.submit(submit_message, [msg, chatbot], [chatbot, msg])
62
+ clear.click(lambda: [], None, chatbot)
63
 
64
+ # Launch the app with custom title and description
65
  demo.launch()