Update app.py
Browse files
app.py
CHANGED
|
@@ -234,6 +234,14 @@ def clear_session_data(session_id: str):
|
|
| 234 |
user_sessions[session_id]["agent"] = None
|
| 235 |
logger.info(f"Session {session_id[:8]}... cleared")
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
def cleanup_old_sessions():
|
| 238 |
"""Clean up sessions older than 1 hour."""
|
| 239 |
with session_lock:
|
|
@@ -308,7 +316,7 @@ class GradioUI:
|
|
| 308 |
logger.info(f"Using HF_TOKEN from .env file for session {session_id[:8]}...")
|
| 309 |
else:
|
| 310 |
logger.warning(f"No API key found for session {session_id[:8]}...")
|
| 311 |
-
error_msg = "β No API key
|
| 312 |
messages.append(gr.ChatMessage(role="assistant", content=error_msg))
|
| 313 |
yield messages
|
| 314 |
return
|
|
@@ -419,8 +427,10 @@ User Query: """
|
|
| 419 |
yield messages
|
| 420 |
|
| 421 |
# Clear sensitive data from session after interaction (AUTOMATIC)
|
| 422 |
-
|
| 423 |
-
|
|
|
|
|
|
|
| 424 |
|
| 425 |
yield messages
|
| 426 |
except Exception as e:
|
|
@@ -666,7 +676,7 @@ This AI agent specializes in automated vulnerability research and analysis, buil
|
|
| 666 |
# API Key Configuration Section
|
| 667 |
with gr.Accordion("π API Configuration", open=False):
|
| 668 |
gr.Markdown("**Configure your Hugging Face API Key**")
|
| 669 |
-
gr.Markdown("π **Security**: Each user has an independent session. API
|
| 670 |
gr.Markdown("Get your API key from: https://huggingface.co/settings/tokens")
|
| 671 |
|
| 672 |
api_key_input = gr.Textbox(
|
|
@@ -677,7 +687,7 @@ This AI agent specializes in automated vulnerability research and analysis, buil
|
|
| 677 |
)
|
| 678 |
api_key_status = gr.Textbox(
|
| 679 |
label="Status",
|
| 680 |
-
value="β
HF_TOKEN found in .env file. To use a different key, enter it above and click 'Setup API Key'." if os.getenv("HF_TOKEN") else "β οΈ Please enter your Hugging Face API key
|
| 681 |
interactive=False
|
| 682 |
)
|
| 683 |
|
|
@@ -814,7 +824,7 @@ This AI agent specializes in automated vulnerability research and analysis, buil
|
|
| 814 |
# API Key Configuration Section for Mobile
|
| 815 |
with gr.Accordion("π API Configuration", open=False):
|
| 816 |
gr.Markdown("**Configure your Hugging Face API Key**")
|
| 817 |
-
gr.Markdown("π **Security**: Each user has an independent session. API
|
| 818 |
gr.Markdown("Get your API key from: https://huggingface.co/settings/tokens")
|
| 819 |
|
| 820 |
mobile_api_key_input = gr.Textbox(
|
|
@@ -825,7 +835,7 @@ This AI agent specializes in automated vulnerability research and analysis, buil
|
|
| 825 |
)
|
| 826 |
mobile_api_key_status = gr.Textbox(
|
| 827 |
label="Status",
|
| 828 |
-
value="β
HF_TOKEN found in .env file. To use a different key, enter it above and click 'Setup API Key'." if os.getenv("HF_TOKEN") else "β οΈ Please enter your Hugging Face API key
|
| 829 |
interactive=False
|
| 830 |
)
|
| 831 |
|
|
|
|
| 234 |
user_sessions[session_id]["agent"] = None
|
| 235 |
logger.info(f"Session {session_id[:8]}... cleared")
|
| 236 |
|
| 237 |
+
def clear_agent_only(session_id: str):
|
| 238 |
+
"""Clear only the agent, keeping the API key for convenience."""
|
| 239 |
+
with session_lock:
|
| 240 |
+
if session_id in user_sessions:
|
| 241 |
+
if "agent" in user_sessions[session_id]:
|
| 242 |
+
del user_sessions[session_id]["agent"]
|
| 243 |
+
logger.info(f"Session {session_id[:8]}... agent cleared")
|
| 244 |
+
|
| 245 |
def cleanup_old_sessions():
|
| 246 |
"""Clean up sessions older than 1 hour."""
|
| 247 |
with session_lock:
|
|
|
|
| 316 |
logger.info(f"Using HF_TOKEN from .env file for session {session_id[:8]}...")
|
| 317 |
else:
|
| 318 |
logger.warning(f"No API key found for session {session_id[:8]}...")
|
| 319 |
+
error_msg = "β No API key configured for your session. Please enter your Hugging Face API key in the API Configuration section above and click 'Setup API Key'."
|
| 320 |
messages.append(gr.ChatMessage(role="assistant", content=error_msg))
|
| 321 |
yield messages
|
| 322 |
return
|
|
|
|
| 427 |
yield messages
|
| 428 |
|
| 429 |
# Clear sensitive data from session after interaction (AUTOMATIC)
|
| 430 |
+
# Note: We clear the agent but keep the API key for convenience
|
| 431 |
+
if "agent" in session_data:
|
| 432 |
+
del session_data["agent"]
|
| 433 |
+
logger.info(f"Session {session_id[:8]}... agent cleared after interaction")
|
| 434 |
|
| 435 |
yield messages
|
| 436 |
except Exception as e:
|
|
|
|
| 676 |
# API Key Configuration Section
|
| 677 |
with gr.Accordion("π API Configuration", open=False):
|
| 678 |
gr.Markdown("**Configure your Hugging Face API Key**")
|
| 679 |
+
gr.Markdown("π **Security**: Each user has an independent session. Your API key is stored securely and automatically cleared after 1 hour of inactivity.")
|
| 680 |
gr.Markdown("Get your API key from: https://huggingface.co/settings/tokens")
|
| 681 |
|
| 682 |
api_key_input = gr.Textbox(
|
|
|
|
| 687 |
)
|
| 688 |
api_key_status = gr.Textbox(
|
| 689 |
label="Status",
|
| 690 |
+
value="β
HF_TOKEN found in .env file. To use a different key, enter it above and click 'Setup API Key'." if os.getenv("HF_TOKEN") else "β οΈ Please enter your Hugging Face API key above and click 'Setup API Key' to start using the application.",
|
| 691 |
interactive=False
|
| 692 |
)
|
| 693 |
|
|
|
|
| 824 |
# API Key Configuration Section for Mobile
|
| 825 |
with gr.Accordion("π API Configuration", open=False):
|
| 826 |
gr.Markdown("**Configure your Hugging Face API Key**")
|
| 827 |
+
gr.Markdown("π **Security**: Each user has an independent session. Your API key is stored securely and automatically cleared after 1 hour of inactivity.")
|
| 828 |
gr.Markdown("Get your API key from: https://huggingface.co/settings/tokens")
|
| 829 |
|
| 830 |
mobile_api_key_input = gr.Textbox(
|
|
|
|
| 835 |
)
|
| 836 |
mobile_api_key_status = gr.Textbox(
|
| 837 |
label="Status",
|
| 838 |
+
value="β
HF_TOKEN found in .env file. To use a different key, enter it above and click 'Setup API Key'." if os.getenv("HF_TOKEN") else "β οΈ Please enter your Hugging Face API key above and click 'Setup API Key' to start using the application.",
|
| 839 |
interactive=False
|
| 840 |
)
|
| 841 |
|