Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,12 @@
|
|
| 1 |
import cv2
|
| 2 |
import torch
|
| 3 |
import requests
|
| 4 |
-
import threading
|
| 5 |
import time
|
| 6 |
from PIL import Image
|
| 7 |
-
from fastapi import FastAPI
|
| 8 |
from supabase import create_client
|
| 9 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 10 |
import os
|
| 11 |
|
| 12 |
-
app = FastAPI()
|
| 13 |
-
|
| 14 |
# ===============================
|
| 15 |
# π Supabase Setup
|
| 16 |
# ===============================
|
|
@@ -22,11 +18,14 @@ supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
|
|
| 22 |
# ===============================
|
| 23 |
# π€ Load NSFW Model (once)
|
| 24 |
# ===============================
|
| 25 |
-
|
| 26 |
|
|
|
|
| 27 |
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 28 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 29 |
|
|
|
|
|
|
|
| 30 |
# ===============================
|
| 31 |
# πΌ Image Check
|
| 32 |
# ===============================
|
|
@@ -42,7 +41,7 @@ def check_image(url):
|
|
| 42 |
return "explicit" if probs[0][1] > 0.5 else "safe"
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
-
print("Image error:", e)
|
| 46 |
return "safe"
|
| 47 |
|
| 48 |
# ===============================
|
|
@@ -77,19 +76,19 @@ def check_video(url, frame_sample_rate=30):
|
|
| 77 |
return "safe"
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
-
print("Video error:", e)
|
| 81 |
return "safe"
|
| 82 |
|
| 83 |
# ===============================
|
| 84 |
-
# π
|
| 85 |
# ===============================
|
| 86 |
def moderation_worker():
|
| 87 |
while True:
|
| 88 |
try:
|
| 89 |
-
print("π Running moderation job...")
|
| 90 |
|
| 91 |
# ===============================
|
| 92 |
-
# πΌ Process Images
|
| 93 |
# ===============================
|
| 94 |
img_posts = supabase.table("posts") \
|
| 95 |
.select("*") \
|
|
@@ -113,10 +112,10 @@ def moderation_worker():
|
|
| 113 |
"result": result
|
| 114 |
}).execute()
|
| 115 |
|
| 116 |
-
print(f"
|
| 117 |
|
| 118 |
# ===============================
|
| 119 |
-
# π₯ Process Videos
|
| 120 |
# ===============================
|
| 121 |
vid_posts = supabase.table("trendz") \
|
| 122 |
.select("*") \
|
|
@@ -145,23 +144,14 @@ def moderation_worker():
|
|
| 145 |
except Exception as e:
|
| 146 |
print("β Worker error:", e)
|
| 147 |
|
| 148 |
-
# β±
|
|
|
|
| 149 |
time.sleep(300)
|
| 150 |
|
| 151 |
-
# ===============================
|
| 152 |
-
# π START WORKER ON STARTUP
|
| 153 |
-
# ===============================
|
| 154 |
-
@app.on_event("startup")
|
| 155 |
-
def start_worker():
|
| 156 |
-
thread = threading.Thread(target=moderation_worker, daemon=True)
|
| 157 |
-
thread.start()
|
| 158 |
|
| 159 |
# ===============================
|
| 160 |
-
#
|
| 161 |
# ===============================
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
"status": "running",
|
| 166 |
-
"message": "NSFW moderation running every 5 minutes"
|
| 167 |
-
}
|
|
|
|
| 1 |
import cv2
|
| 2 |
import torch
|
| 3 |
import requests
|
|
|
|
| 4 |
import time
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
from supabase import create_client
|
| 7 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 8 |
import os
|
| 9 |
|
|
|
|
|
|
|
| 10 |
# ===============================
|
| 11 |
# π Supabase Setup
|
| 12 |
# ===============================
|
|
|
|
| 18 |
# ===============================
|
| 19 |
# π€ Load NSFW Model (once)
|
| 20 |
# ===============================
|
| 21 |
+
print("π Loading NSFW model...")
|
| 22 |
|
| 23 |
+
model_name = "AdamCodd/vit-base-nsfw-detector"
|
| 24 |
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 25 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 26 |
|
| 27 |
+
print("β
Model loaded")
|
| 28 |
+
|
| 29 |
# ===============================
|
| 30 |
# πΌ Image Check
|
| 31 |
# ===============================
|
|
|
|
| 41 |
return "explicit" if probs[0][1] > 0.5 else "safe"
|
| 42 |
|
| 43 |
except Exception as e:
|
| 44 |
+
print("β Image error:", e)
|
| 45 |
return "safe"
|
| 46 |
|
| 47 |
# ===============================
|
|
|
|
| 76 |
return "safe"
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
+
print("β Video error:", e)
|
| 80 |
return "safe"
|
| 81 |
|
| 82 |
# ===============================
|
| 83 |
+
# π MAIN WORKER LOOP
|
| 84 |
# ===============================
|
| 85 |
def moderation_worker():
|
| 86 |
while True:
|
| 87 |
try:
|
| 88 |
+
print("\nπ Running moderation job...")
|
| 89 |
|
| 90 |
# ===============================
|
| 91 |
+
# πΌ Process Images
|
| 92 |
# ===============================
|
| 93 |
img_posts = supabase.table("posts") \
|
| 94 |
.select("*") \
|
|
|
|
| 112 |
"result": result
|
| 113 |
}).execute()
|
| 114 |
|
| 115 |
+
print(f"πΌ IMAGE {post['id']} β {result}")
|
| 116 |
|
| 117 |
# ===============================
|
| 118 |
+
# π₯ Process Videos
|
| 119 |
# ===============================
|
| 120 |
vid_posts = supabase.table("trendz") \
|
| 121 |
.select("*") \
|
|
|
|
| 144 |
except Exception as e:
|
| 145 |
print("β Worker error:", e)
|
| 146 |
|
| 147 |
+
# β± WAIT 5 MINUTES
|
| 148 |
+
print("β³ Sleeping for 5 minutes...\n")
|
| 149 |
time.sleep(300)
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
# ===============================
|
| 153 |
+
# βΆοΈ AUTO START
|
| 154 |
# ===============================
|
| 155 |
+
if __name__ == "__main__":
|
| 156 |
+
print("π₯ Starting NSFW moderation worker...")
|
| 157 |
+
moderation_worker()
|
|
|
|
|
|
|
|
|