# Function to detect AI tools like Google Gemini (by process name or window title)
def detect_ai_usage():
ai_tools = ["gemini", "chatgpt", "openai", "bard", "cortex","chrome","ChatGPT"] # Example AI tool names/processes
while True:
try:
# Check active processes for AI tools
for proc in psutil.process_iter(['pid', 'name']):
if any(ai_tool.lower() in proc.info['name'].lower() for ai_tool in ai_tools):
print(f"AI tool detected: {proc.info['name']} with PID {proc.info['pid']}")
screenshot_path = take_screenshot()
send_email("AI Tool Usage Detected", f"An AI tool was detected: {proc.info['name']}.", screenshot_path)
os.remove(screenshot_path)
time.sleep(5) # Check every 5 seconds
except Exception as e:
print(f"Error detecting AI usage: {e}")
time.sleep(30)
Explanation :
This detect_ai_usage function monitors the system's running processes for the presence of specific AI-related tools (e.g., Google Gemini, ChatGPT, etc.) by matching their names. If an AI tool is detected, it sends an alert email and takes a screenshot. Here's a detailed explanation:
Functionality
1. Define AI Tools:
o ai_tools = ["gemini", "chatgpt", "openai", "bard", "cortex", "chrome", "ChatGPT"]:
§ Specifies the names of processes or tools to look for. These names are searched in the active process list.
§ The list can be expanded or customized as needed.
2. Monitor Processes in a Loop:
o A while True loop continuously checks running processes using psutil.
3. Search for AI Tools in Processes:
o psutil.process_iter(['pid', 'name']) iterates over active processes, retrieving each process's name and PID (Process ID).
o For each process, the code checks if any of the ai_tools names appear in the process's name (case-insensitive comparison).
4. Alert on Detection:
o If a match is found:
§ Logs the detected AI tool and its PID: print(f"AI tool detected: {proc.info['name']} with PID {proc.info['pid']}").
§ Captures a screenshot using take_screenshot().
§ Sends an email using send_email, with the tool's name included in the email body and the screenshot attached.
§ Deletes the screenshot after sending the email to free up storage.
5. Handle Errors Gracefully:
o If an error occurs (e.g., insufficient permissions to access certain processes), it logs the error and waits for 30 seconds before retrying.
6. Pause Between Checks:
o The time.sleep(5) introduces a 5-second delay between process scans to reduce CPU usage.