Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,5 @@
|
|
1 |
-
import os
|
2 |
-
os.environ['GRADIO_SERVER_NAME'] = "0.0.0.0"
|
3 |
-
os.environ['GRADIO_SERVER_PORT'] = "7860"
|
4 |
-
|
5 |
import gradio as gr
|
|
|
6 |
import torch
|
7 |
import numpy as np
|
8 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
@@ -14,54 +11,33 @@ from datetime import datetime
|
|
14 |
from torch.nn.functional import sigmoid
|
15 |
from collections import Counter
|
16 |
import logging
|
|
|
|
|
17 |
|
18 |
# Set up logging
|
19 |
logging.basicConfig(level=logging.DEBUG)
|
20 |
logger = logging.getLogger(__name__)
|
21 |
|
22 |
-
|
23 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
24 |
-
logger.info(f"Using device: {device}")
|
25 |
|
26 |
# Model initialization with error handling
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
# Tone model
|
45 |
-
tone_model, tone_tokenizer = load_model_and_tokenizer(
|
46 |
-
"SamanthaStorm/tone-tag-multilabel-v1", "tone"
|
47 |
-
)
|
48 |
-
|
49 |
-
# Sentiment model
|
50 |
-
sentiment_model, sentiment_tokenizer = load_model_and_tokenizer(
|
51 |
-
"SamanthaStorm/tether-sentiment", "sentiment"
|
52 |
-
)
|
53 |
-
|
54 |
-
# DARVO model
|
55 |
-
darvo_model, darvo_tokenizer = load_model_and_tokenizer(
|
56 |
-
"SamanthaStorm/tether-darvo-regressor-v1", "darvo"
|
57 |
-
)
|
58 |
-
|
59 |
-
if darvo_model:
|
60 |
-
darvo_model.eval()
|
61 |
|
62 |
-
except Exception as e:
|
63 |
-
logger.error(f"Error during model initialization: {e}")
|
64 |
-
raise
|
65 |
|
66 |
# Constants and Labels
|
67 |
LABELS = [
|
@@ -199,12 +175,16 @@ def get_emotional_tone_tag(text, emotions, sentiment, patterns, abuse_score):
|
|
199 |
logger.error(f"Error in emotional tone analysis: {e}")
|
200 |
return "unknown"
|
201 |
|
|
|
202 |
def compute_abuse_score(matched_scores, sentiment):
|
203 |
-
"""
|
|
|
|
|
204 |
try:
|
205 |
if not matched_scores:
|
206 |
return 0.0
|
207 |
|
|
|
208 |
total_weight = sum(weight for _, _, weight in matched_scores)
|
209 |
if total_weight == 0:
|
210 |
return 0.0
|
@@ -247,6 +227,8 @@ def compute_abuse_score(matched_scores, sentiment):
|
|
247 |
logger.error(f"Error computing abuse score: {e}")
|
248 |
return 0.0
|
249 |
|
|
|
|
|
250 |
def analyze_single_message(text, thresholds):
|
251 |
"""Analyze a single message for abuse patterns"""
|
252 |
logger.debug("\n=== DEBUG START ===")
|
@@ -264,7 +246,7 @@ def analyze_single_message(text, thresholds):
|
|
264 |
|
265 |
# Abuse model inference
|
266 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
267 |
-
inputs = {k: v.to(device) for k, v in inputs.items()}
|
268 |
|
269 |
with torch.no_grad():
|
270 |
outputs = model(**inputs)
|
@@ -347,14 +329,12 @@ def analyze_single_message(text, thresholds):
|
|
347 |
stage = 2 if explicit_abuse or abuse_score > 70 else 1
|
348 |
logger.debug(f"Final stage: {stage}")
|
349 |
|
350 |
-
|
351 |
-
|
352 |
return abuse_score, threshold_labels, matched_scores, {"label": sentiment}, stage, darvo_score, tone_tag
|
353 |
|
354 |
except Exception as e:
|
355 |
logger.error(f"Error in analyze_single_message: {e}")
|
356 |
return 0.0, [], [], {"label": "error"}, 1, 0.0, None
|
357 |
-
|
358 |
def generate_abuse_score_chart(dates, scores, patterns):
|
359 |
"""Generate a timeline chart of abuse scores"""
|
360 |
try:
|
@@ -647,11 +627,14 @@ def create_interface():
|
|
647 |
logger.error(f"Error creating interface: {e}")
|
648 |
raise
|
649 |
|
650 |
-
|
651 |
if __name__ == "__main__":
|
652 |
try:
|
653 |
demo = create_interface()
|
654 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
655 |
except Exception as e:
|
656 |
-
|
657 |
-
raise
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import spaces
|
3 |
import torch
|
4 |
import numpy as np
|
5 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
|
11 |
from torch.nn.functional import sigmoid
|
12 |
from collections import Counter
|
13 |
import logging
|
14 |
+
# Add this after imports
|
15 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
16 |
|
17 |
# Set up logging
|
18 |
logging.basicConfig(level=logging.DEBUG)
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
21 |
+
|
|
|
|
|
22 |
|
23 |
# Model initialization with error handling
|
24 |
+
# Model initialization
|
25 |
+
model_name = "SamanthaStorm/tether-multilabel-v4"
|
26 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
|
27 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
28 |
+
|
29 |
+
# Tone model
|
30 |
+
tone_model = AutoModelForSequenceClassification.from_pretrained("SamanthaStorm/tone-tag-multilabel-v1").to(device)
|
31 |
+
tone_tokenizer = AutoTokenizer.from_pretrained("SamanthaStorm/tone-tag-multilabel-v1", use_fast=False)
|
32 |
+
|
33 |
+
# Sentiment model
|
34 |
+
sentiment_model = AutoModelForSequenceClassification.from_pretrained("SamanthaStorm/tether-sentiment").to(device)
|
35 |
+
sentiment_tokenizer = AutoTokenizer.from_pretrained("SamanthaStorm/tether-sentiment", use_fast=False)
|
36 |
+
|
37 |
+
# DARVO model
|
38 |
+
darvo_model = AutoModelForSequenceClassification.from_pretrained("SamanthaStorm/tether-darvo-regressor-v1").to(device)
|
39 |
+
darvo_tokenizer = AutoTokenizer.from_pretrained("SamanthaStorm/tether-darvo-regressor-v1", use_fast=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
|
|
|
|
|
|
41 |
|
42 |
# Constants and Labels
|
43 |
LABELS = [
|
|
|
175 |
logger.error(f"Error in emotional tone analysis: {e}")
|
176 |
return "unknown"
|
177 |
|
178 |
+
@spaces.GPU
|
179 |
def compute_abuse_score(matched_scores, sentiment):
|
180 |
+
"""
|
181 |
+
Compute abuse score from matched patterns and sentiment
|
182 |
+
"""
|
183 |
try:
|
184 |
if not matched_scores:
|
185 |
return 0.0
|
186 |
|
187 |
+
# Calculate weighted score
|
188 |
total_weight = sum(weight for _, _, weight in matched_scores)
|
189 |
if total_weight == 0:
|
190 |
return 0.0
|
|
|
227 |
logger.error(f"Error computing abuse score: {e}")
|
228 |
return 0.0
|
229 |
|
230 |
+
|
231 |
+
@spaces.GPU
|
232 |
def analyze_single_message(text, thresholds):
|
233 |
"""Analyze a single message for abuse patterns"""
|
234 |
logger.debug("\n=== DEBUG START ===")
|
|
|
246 |
|
247 |
# Abuse model inference
|
248 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
249 |
+
inputs = {k: v.to(device) for k, v in inputs.items()} # Move to GPU
|
250 |
|
251 |
with torch.no_grad():
|
252 |
outputs = model(**inputs)
|
|
|
329 |
stage = 2 if explicit_abuse or abuse_score > 70 else 1
|
330 |
logger.debug(f"Final stage: {stage}")
|
331 |
|
332 |
+
logger.debug("=== DEBUG END ===\n")
|
|
|
333 |
return abuse_score, threshold_labels, matched_scores, {"label": sentiment}, stage, darvo_score, tone_tag
|
334 |
|
335 |
except Exception as e:
|
336 |
logger.error(f"Error in analyze_single_message: {e}")
|
337 |
return 0.0, [], [], {"label": "error"}, 1, 0.0, None
|
|
|
338 |
def generate_abuse_score_chart(dates, scores, patterns):
|
339 |
"""Generate a timeline chart of abuse scores"""
|
340 |
try:
|
|
|
627 |
logger.error(f"Error creating interface: {e}")
|
628 |
raise
|
629 |
|
630 |
+
|
631 |
if __name__ == "__main__":
|
632 |
try:
|
633 |
demo = create_interface()
|
634 |
+
demo.launch(
|
635 |
+
server_name="0.0.0.0",
|
636 |
+
server_port=7860,
|
637 |
+
share=False
|
638 |
+
)
|
639 |
except Exception as e:
|
640 |
+
print(f"Error launching app: {e}")
|
|