Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Model settings
|
6 |
+
MODEL_NAME = "Canstralian/pentest_ai"
|
7 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
8 |
+
|
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}",
|
16 |
+
headers=headers,
|
17 |
+
json=payload
|
18 |
+
)
|
19 |
+
response.raise_for_status() # Raise an error for bad responses
|
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()
|