# main.py import os # Disable TensorFlow oneDNN warnings os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" # Disable torch compile warnings and optimizations for CPU-only devices os.environ["TORCH_COMPILE_DISABLE"] = "1" os.environ["TORCHDYNAMO_DISABLE"] = "1" import sys import time from backend import Backend import config def run_output_test_mode(): """ Runs the application in output testing mode for testing modular output guardrails. """ print("\n\n" + "=" * 60) print("\n--- OUTPUT GUARDRAIL TESTING MODE ---") print("šŸ” This mode allows you to test modular output guardrails with manual input") print(" You can enter both a prompt and the LLM's response to test filtering") print("=" * 60) try: # Initialize backend in output test mode app_backend = Backend(output_test_mode=True) except Exception as e: print(f"\nāŒ Error initializing output testing backend: {e}") print(" Make sure you have the presidio libraries installed for PII detection.") sys.exit(1) while True: try: print(f"\n{'='*60}") print("šŸ“ OUTPUT GUARDRAIL TEST") print(f"{'='*60}") # Get prompt from user prompt = input("\nšŸ’­ Enter the input prompt (or 'exit' to quit): ") if prompt.lower() in ["exit", "quit"]: print("\nšŸ‘‹ Exiting output test mode. Goodbye!") break # Get manual output from user print("\nšŸ¤– Enter the LLM output you want to test:") print("(Press Enter twice to finish your input)\n") lines = [] empty_line_count = 0 while True: line = input() if line == "": empty_line_count += 1 if empty_line_count >= 2: break lines.append(line) else: empty_line_count = 0 lines.append(line) manual_output = "\n".join(lines).strip() if not manual_output: print("āŒ No output provided. Please try again.") continue print(f"\nāœ… Testing output ({len(manual_output)} characters) against modular guardrails...\n") # Test the output against guardrails processed_output, is_safe = app_backend.test_output_guardrails(prompt, manual_output) print(f"\n{'='*60}") print("šŸ“Š GUARDRAIL TEST RESULTS") print(f"{'='*60}") if is_safe: print("āœ… Result: OUTPUT APPROVED") print("\nšŸ“„ Final output after guardrail processing:") print(f"'{processed_output}'") if processed_output != manual_output: print(f"\nāš ļø Note: Output was modified by guardrails") print(f" Original length: {len(manual_output)} characters") print(f" Modified length: {len(processed_output)} characters") else: print("šŸ”’ Result: OUTPUT BLOCKED") print(f"\nāŒ Reason: {processed_output}") except KeyboardInterrupt: print("\nšŸ‘‹ Exiting output test mode. Goodbye!") break except Exception as e: print(f"\n\nāŒ An error occurred: {e}") def run_interactive_mode(app_backend: Backend): """ Runs the application in interactive mode, accepting user input. """ print("\n\n" + "=" * 60) print("\n--- INTERACTIVE MODE ---") print("šŸ”’ AI Detection: Finetuned model will scan all prompts for attacks") print("Enter your prompt below. Type 'exit' or 'quit' to end the session.") print("=" * 60) while True: try: prompt = input("\nšŸ‘¤ You: ") if prompt.lower() in ["exit", "quit"]: print("\nšŸ‘‹ Exiting interactive mode. Goodbye!") break response_stream, is_safe, processed_prompt = app_backend.process_request( prompt, stream=True ) if not is_safe: print(f" šŸ”’ System: {response_stream}") continue print("\nšŸ¤– Chatbot (streaming): ", end="") full_response = "" for chunk in response_stream: full_response += chunk print(chunk, end="", flush=True) time.sleep(0.05) print() # For the newline except KeyboardInterrupt: print("\nšŸ‘‹ Exiting interactive mode. Goodbye!") break except Exception as e: print(f"\n\nāŒ An error occurred: {e}") def main(): """ Main entry point. Initializes the backend and runs in the configured mode. """ # Check if we should run in output testing mode if len(sys.argv) > 1 and sys.argv[1] == "output_test": run_output_test_mode() return print("=" * 60) print(" Guardrails System") print(" šŸ”’ AI-powered attack detection with finetuned model") print("=" * 60) try: app_backend = Backend() except Exception as e: print(f"\nāŒ Error initializing backend: {e}") print(" Make sure you have the transformers library installed for AI Detection Mode.") sys.exit(1) run_interactive_mode(app_backend) if __name__ == "__main__": main()