Spaces:
Runtime error
Runtime error
| import cv2 | |
| import torch | |
| import requests | |
| import time | |
| from PIL import Image | |
| from supabase import create_client | |
| from transformers import AutoImageProcessor, AutoModelForImageClassification | |
| import os | |
| # =============================== | |
| # π Supabase Setup | |
| # =============================== | |
| SUPABASE_URL = os.getenv("SUPABASE_URL") | |
| SUPABASE_KEY = os.getenv("SUPABASE_KEY") | |
| supabase = create_client(SUPABASE_URL, SUPABASE_KEY) | |
| # =============================== | |
| # π€ Load NSFW Model (once) | |
| # =============================== | |
| print("π Loading NSFW model...") | |
| model_name = "AdamCodd/vit-base-nsfw-detector" | |
| processor = AutoImageProcessor.from_pretrained(model_name) | |
| model = AutoModelForImageClassification.from_pretrained(model_name) | |
| print("β Model loaded") | |
| # =============================== | |
| # πΌ Image Check | |
| # =============================== | |
| def check_image(url): | |
| try: | |
| image = Image.open(requests.get(url, stream=True).raw).convert("RGB") | |
| inputs = processor(images=image, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probs = torch.softmax(outputs.logits, dim=1) | |
| return "explicit" if probs[0][1] > 0.5 else "safe" | |
| except Exception as e: | |
| print("β Image error:", e) | |
| return "safe" | |
| # =============================== | |
| # π₯ Video Check | |
| # =============================== | |
| def check_video(url, frame_sample_rate=30): | |
| try: | |
| cap = cv2.VideoCapture(url) | |
| frame_count = 0 | |
| while cap.isOpened(): | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| if frame_count % frame_sample_rate == 0: | |
| img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) | |
| inputs = processor(images=img, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probs = torch.softmax(outputs.logits, dim=1) | |
| if probs[0][1] > 0.5: | |
| cap.release() | |
| return "explicit" | |
| frame_count += 1 | |
| cap.release() | |
| return "safe" | |
| except Exception as e: | |
| print("β Video error:", e) | |
| return "safe" | |
| # =============================== | |
| # π MAIN WORKER LOOP | |
| # =============================== | |
| def moderation_worker(): | |
| while True: | |
| try: | |
| print("\nπ Running moderation job...") | |
| # =============================== | |
| # πΌ Process Images | |
| # =============================== | |
| img_posts = supabase.table("posts") \ | |
| .select("*") \ | |
| .limit(10) \ | |
| .execute().data | |
| for post in img_posts: | |
| existing = supabase.table("content_moderation") \ | |
| .select("id") \ | |
| .eq("post_id", post["id"]) \ | |
| .eq("post_type", "image") \ | |
| .execute() | |
| if len(existing.data) == 0: | |
| result = check_image(post["image_url"]) | |
| supabase.table("content_moderation").insert({ | |
| "post_id": post["id"], | |
| "post_type": "image", | |
| "file_url": post["image_url"], | |
| "result": result | |
| }).execute() | |
| print(f"πΌ IMAGE {post['id']} β {result}") | |
| # =============================== | |
| # π₯ Process Videos | |
| # =============================== | |
| vid_posts = supabase.table("trendz") \ | |
| .select("*") \ | |
| .limit(5) \ | |
| .execute().data | |
| for post in vid_posts: | |
| existing = supabase.table("content_moderation") \ | |
| .select("id") \ | |
| .eq("post_id", post["id"]) \ | |
| .eq("post_type", "video") \ | |
| .execute() | |
| if len(existing.data) == 0: | |
| result = check_video(post["video_url"]) | |
| supabase.table("content_moderation").insert({ | |
| "post_id": post["id"], | |
| "post_type": "video", | |
| "file_url": post["video_url"], | |
| "result": result | |
| }).execute() | |
| print(f"π₯ VIDEO {post['id']} β {result}") | |
| except Exception as e: | |
| print("β Worker error:", e) | |
| # β± WAIT 5 MINUTES | |
| print("β³ Sleeping for 5 minutes...\n") | |
| time.sleep(300) | |
| # =============================== | |
| # βΆοΈ AUTO START | |
| # =============================== | |
| if __name__ == "__main__": | |
| print("π₯ Starting NSFW moderation worker...") | |
| moderation_worker() |