Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
prompt += f"User: {message}\nAssistant: "
|
38 |
|
39 |
# Get response from the model
|
40 |
response = query_hf(prompt)
|
41 |
|
42 |
-
# Return
|
43 |
-
return
|
44 |
|
45 |
# Create Gradio interface
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
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()
|