datadabllp

Content Moderation System

Jul 19th, 2024
3,964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. import chromadb
  2. from chromadb.config import Settings
  3.  
  4. # Initialize Chroma client
  5. client = chromadb.Client(Settings(
  6.     chroma_db_impl="duckdb+parquet",
  7.     persist_directory="/path/to/persist"
  8. ))
  9.  
  10. # Create a collection for problematic content
  11. collection = client.create_collection(name="problematic_content")
  12.  
  13. # Add some example problematic content
  14. collection.add(
  15.     documents=["hate speech example", "violent content example", "spam content example"],
  16.     metadatas=[{"category": "hate"}, {"category": "violence"}, {"category": "spam"}],
  17.     ids=["1", "2", "3"]
  18. )
  19.  
  20. # Function to check new content
  21. def check_content(new_post):
  22.     results = collection.query(
  23.         query_texts=[new_post],
  24.         n_results=1
  25.     )
  26.    
  27.     if results['distances'][0][0] < 0.5:  # Adjust threshold as needed
  28.         return f"Flagged as potentially {results['metadatas'][0][0]['category']}"
  29.     else:
  30.         return "Content seems appropriate"
  31.  
  32. # Test the system
  33. print(check_content("This is a normal post"))
  34. print(check_content("This post contains hate speech"))
  35.  
Advertisement
Add Comment
Please, Sign In to add comment