Spaces:
Sleeping
Sleeping
csjhonathan
commited on
Commit
·
7d1410d
1
Parent(s):
ee6a3b6
ajusta verificação de conteúdo
Browse files- app.py +85 -22
- requirements.txt +5 -1
app.py
CHANGED
|
@@ -1,66 +1,129 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
content_model = pipeline("image-classification", model="facebook/convnext-base-224")
|
| 6 |
nsfw_model = pipeline("image-classification", model="Falconsai/nsfw_image_detection")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def analyze_image(image):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
all_labels
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
human_keywords = [
|
| 25 |
"human", "person", "people", "man", "woman", "child", "baby", "boy", "girl",
|
| 26 |
-
"face", "portrait", "selfie", "crowd", "family", "couple", "teenager"
|
|
|
|
|
|
|
| 27 |
]
|
| 28 |
is_human = any(keyword in combined_labels for keyword in human_keywords)
|
| 29 |
|
| 30 |
dog_keywords = [
|
| 31 |
"dog", "puppy", "retriever", "labrador", "golden", "beagle", "bulldog",
|
| 32 |
"poodle", "german shepherd", "chihuahua", "terrier", "hound", "mastiff",
|
| 33 |
-
"canine", "pet", "animal", "malamute", "malemute", "alaskan"
|
|
|
|
| 34 |
]
|
| 35 |
is_dog = any(keyword in combined_labels for keyword in dog_keywords)
|
| 36 |
|
| 37 |
violence_keywords = [
|
| 38 |
"blood", "wound", "injury", "hurt", "pain", "fight", "violence", "weapon",
|
| 39 |
-
"knife", "gun", "attack", "aggression", "conflict", "battle", "war"
|
|
|
|
| 40 |
]
|
| 41 |
|
| 42 |
suffering_keywords = [
|
| 43 |
"sad", "crying", "tears", "depressed", "miserable", "suffering", "pain",
|
| 44 |
"distress", "anguish", "grief", "mourning", "funeral", "death", "dead",
|
| 45 |
-
"dying", "illness", "sick", "injured", "abandoned", "neglected"
|
|
|
|
| 46 |
]
|
| 47 |
|
| 48 |
abuse_keywords = [
|
| 49 |
"abuse", "mistreatment", "cruelty", "torture", "beaten", "chained",
|
| 50 |
-
"caged", "starving", "malnourished", "neglected", "abandoned"
|
|
|
|
| 51 |
]
|
| 52 |
|
| 53 |
death_keywords = [
|
| 54 |
"death", "dead", "dying", "corpse", "carcass", "deceased", "lifeless",
|
| 55 |
-
"motionless", "still", "rigid", "pale", "cold"
|
|
|
|
| 56 |
]
|
| 57 |
|
| 58 |
suspicious_keywords = [
|
| 59 |
"lying", "laying", "ground", "floor", "side", "horizontal", "flat",
|
| 60 |
-
"unconscious", "sleeping", "resting", "still", "motionless", "quiet"
|
|
|
|
| 61 |
]
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
violence = any(keyword in combined_labels for keyword in violence_keywords)
|
| 65 |
suffering = any(keyword in combined_labels for keyword in suffering_keywords)
|
| 66 |
abuse = any(keyword in combined_labels for keyword in abuse_keywords)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
import re
|
| 4 |
+
import torch
|
| 5 |
+
import timm
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
eva02_model = timm.create_model('hf_hub:SmilingWolf/wd-eva02-large-tagger-v3', pretrained=True)
|
| 11 |
+
eva02_model.eval()
|
| 12 |
+
|
| 13 |
+
import requests
|
| 14 |
+
tags_response = requests.get('https://huggingface.co/SmilingWolf/wd-eva02-large-tagger-v3/resolve/main/selected_tags.csv')
|
| 15 |
+
tags_lines = tags_response.text.strip().split('\n')
|
| 16 |
+
eva02_tags = [line.split(',')[1] for line in tags_lines[1:]] # Skip header
|
| 17 |
+
|
| 18 |
+
print(f"Modelo EVA02 carregado com {len(eva02_tags)} tags")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Erro ao carregar EVA02: {e}")
|
| 21 |
+
eva02_model = None
|
| 22 |
+
eva02_tags = []
|
| 23 |
|
| 24 |
content_model = pipeline("image-classification", model="facebook/convnext-base-224")
|
| 25 |
nsfw_model = pipeline("image-classification", model="Falconsai/nsfw_image_detection")
|
| 26 |
|
| 27 |
+
def analyze_with_eva02(image):
|
| 28 |
+
if eva02_model is None:
|
| 29 |
+
return [], []
|
| 30 |
+
|
| 31 |
+
image_tensor = torch.from_numpy(np.array(image)).permute(2, 0, 1).float() / 255.0
|
| 32 |
+
image_tensor = torch.nn.functional.interpolate(
|
| 33 |
+
image_tensor.unsqueeze(0),
|
| 34 |
+
size=(448, 448),
|
| 35 |
+
mode='bilinear',
|
| 36 |
+
align_corners=False
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
features = eva02_model(image_tensor)
|
| 41 |
+
probs = torch.sigmoid(features[0])
|
| 42 |
+
|
| 43 |
+
detected_tags = []
|
| 44 |
+
tag_scores = []
|
| 45 |
+
|
| 46 |
+
for i, (tag, prob) in enumerate(zip(eva02_tags, probs)):
|
| 47 |
+
if prob > 0.5:
|
| 48 |
+
detected_tags.append(tag)
|
| 49 |
+
tag_scores.append(float(prob))
|
| 50 |
+
|
| 51 |
+
return detected_tags, tag_scores
|
| 52 |
+
|
| 53 |
def analyze_image(image):
|
| 54 |
+
if eva02_model is not None:
|
| 55 |
+
eva02_tags_detected, eva02_scores = analyze_with_eva02(image)
|
| 56 |
+
combined_labels = " ".join(eva02_tags_detected).lower()
|
| 57 |
+
print(f"EVA02 detectou: {eva02_tags_detected}")
|
| 58 |
+
else:
|
| 59 |
+
content_preds = content_model(image)
|
| 60 |
+
top_content = max(content_preds, key=lambda x: x["score"])
|
| 61 |
+
|
| 62 |
+
nsfw_preds = nsfw_model(image)
|
| 63 |
+
top_nsfw = max(nsfw_preds, key=lambda x: x["score"])
|
| 64 |
+
|
| 65 |
+
all_labels = []
|
| 66 |
+
for pred in content_preds:
|
| 67 |
+
all_labels.append(pred["label"].lower())
|
| 68 |
+
|
| 69 |
+
for pred in nsfw_preds:
|
| 70 |
+
all_labels.append(pred["label"].lower())
|
| 71 |
+
|
| 72 |
+
combined_labels = " ".join(all_labels)
|
| 73 |
|
| 74 |
human_keywords = [
|
| 75 |
"human", "person", "people", "man", "woman", "child", "baby", "boy", "girl",
|
| 76 |
+
"face", "portrait", "selfie", "crowd", "family", "couple", "teenager",
|
| 77 |
+
"1boy", "1girl", "2boys", "2girls", "multiple boys", "multiple girls",
|
| 78 |
+
"male", "female", "adult", "teen", "kid", "toddler", "infant"
|
| 79 |
]
|
| 80 |
is_human = any(keyword in combined_labels for keyword in human_keywords)
|
| 81 |
|
| 82 |
dog_keywords = [
|
| 83 |
"dog", "puppy", "retriever", "labrador", "golden", "beagle", "bulldog",
|
| 84 |
"poodle", "german shepherd", "chihuahua", "terrier", "hound", "mastiff",
|
| 85 |
+
"canine", "pet", "animal", "malamute", "malemute", "alaskan",
|
| 86 |
+
"1dog", "2dogs", "multiple dogs", "doggy", "doggie", "pup"
|
| 87 |
]
|
| 88 |
is_dog = any(keyword in combined_labels for keyword in dog_keywords)
|
| 89 |
|
| 90 |
violence_keywords = [
|
| 91 |
"blood", "wound", "injury", "hurt", "pain", "fight", "violence", "weapon",
|
| 92 |
+
"knife", "gun", "attack", "aggression", "conflict", "battle", "war",
|
| 93 |
+
"bloody", "injured", "wounded", "bleeding", "scar", "bruise", "cut"
|
| 94 |
]
|
| 95 |
|
| 96 |
suffering_keywords = [
|
| 97 |
"sad", "crying", "tears", "depressed", "miserable", "suffering", "pain",
|
| 98 |
"distress", "anguish", "grief", "mourning", "funeral", "death", "dead",
|
| 99 |
+
"dying", "illness", "sick", "injured", "abandoned", "neglected",
|
| 100 |
+
"crying", "tears", "sad", "depressed", "miserable", "grief", "mourning"
|
| 101 |
]
|
| 102 |
|
| 103 |
abuse_keywords = [
|
| 104 |
"abuse", "mistreatment", "cruelty", "torture", "beaten", "chained",
|
| 105 |
+
"caged", "starving", "malnourished", "neglected", "abandoned",
|
| 106 |
+
"chained", "caged", "starving", "malnourished", "abused", "mistreated"
|
| 107 |
]
|
| 108 |
|
| 109 |
death_keywords = [
|
| 110 |
"death", "dead", "dying", "corpse", "carcass", "deceased", "lifeless",
|
| 111 |
+
"motionless", "still", "rigid", "pale", "cold", "skull", "bones",
|
| 112 |
+
"grave", "tombstone", "funeral", "coffin", "burial"
|
| 113 |
]
|
| 114 |
|
| 115 |
suspicious_keywords = [
|
| 116 |
"lying", "laying", "ground", "floor", "side", "horizontal", "flat",
|
| 117 |
+
"unconscious", "sleeping", "resting", "still", "motionless", "quiet",
|
| 118 |
+
"on ground", "on floor", "lying down", "sleeping", "unconscious"
|
| 119 |
]
|
| 120 |
|
| 121 |
+
if eva02_model is not None:
|
| 122 |
+
adult_keywords = ["nsfw", "explicit", "nude", "naked", "sexual", "adult", "mature"]
|
| 123 |
+
adult_content = any(keyword in combined_labels for keyword in adult_keywords)
|
| 124 |
+
else:
|
| 125 |
+
adult_content = top_nsfw["label"].lower() == "nsfw"
|
| 126 |
+
|
| 127 |
violence = any(keyword in combined_labels for keyword in violence_keywords)
|
| 128 |
suffering = any(keyword in combined_labels for keyword in suffering_keywords)
|
| 129 |
abuse = any(keyword in combined_labels for keyword in abuse_keywords)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,7 @@
|
|
| 1 |
transformers
|
| 2 |
torch
|
| 3 |
-
gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
transformers
|
| 2 |
torch
|
| 3 |
+
gradio
|
| 4 |
+
timm
|
| 5 |
+
requests
|
| 6 |
+
pillow
|
| 7 |
+
numpy
|