TuTGo commited on
Commit
02e397f
·
verified ·
1 Parent(s): a8d7353

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -45
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  import tensorflow as tf
4
  import sentencepiece as spm
@@ -18,61 +18,72 @@ input_details = interpreter.get_input_details()
18
  output_details = interpreter.get_output_details()
19
  max_len = input_details[0]['shape'][1]
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  class TextRequest(BaseModel):
22
  text: str
23
 
 
24
  @app.post("/predict")
25
  def predict(request: TextRequest):
26
  try:
27
- # Tokenize text
28
- tokens = [0] + sp.encode_as_ids(request.text) + [2]
29
-
30
- if len(tokens) > max_len:
31
- tokens = tokens[:max_len]
32
- tokens[-1] = 2
33
- else:
34
- tokens = tokens + [1] * (max_len - len(tokens))
35
-
36
- # Initialize inputs safely matching the model's expected dtype and shape
37
- for i, detail in enumerate(input_details):
38
- shape = detail['shape']
39
- dtype = detail['dtype']
40
-
41
- # Create a zero array matching the shape
42
- input_data = np.zeros(shape, dtype=dtype)
43
-
44
- # If it expects an input of sequence length, fill it with our tokens/masks
45
- if shape[1] == max_len:
46
- if 'mask' in detail['name'].lower():
47
- # Attention mask: 1 for tokens, 0 for padding
48
- mask = [1] * min(len(sp.encode_as_ids(request.text)) + 2, max_len)
49
- mask += [0] * (max_len - len(mask))
50
- input_data[0] = np.array(mask, dtype=dtype)
51
- elif 'type' in detail['name'].lower():
52
- # Token type ids (all 0s)
53
- pass
54
- else:
55
- # Assume it's input_ids
56
- input_data[0] = np.array(tokens, dtype=dtype)
57
-
58
- interpreter.set_tensor(detail['index'], input_data)
59
-
60
- interpreter.invoke()
61
-
62
- output_data = interpreter.get_tensor(output_details[0]['index'])
63
-
64
- if output_data.shape[-1] == 2:
65
- is_toxic = bool(output_data[0][1] > 0.5)
66
- else:
67
- is_toxic = bool(output_data[0][0] > 0.5)
68
-
69
- return {"is_toxic": is_toxic}
70
  except Exception as e:
71
  error_msg = traceback.format_exc()
72
  print(error_msg)
73
- # We return the exact error to Flutter so we can see what went wrong if it fails again
74
  return {"is_toxic": False, "error": error_msg}
75
 
 
76
  @app.get("/")
77
  def read_root():
78
  return {"status": "Toxic API is running!"}
 
1
+ from fastapi import FastAPI
2
  from pydantic import BaseModel
3
  import tensorflow as tf
4
  import sentencepiece as spm
 
18
  output_details = interpreter.get_output_details()
19
  max_len = input_details[0]['shape'][1]
20
 
21
+
22
+ def run_inference(text: str) -> bool:
23
+ """Run the model on a single text and return True if toxic."""
24
+ tokens = [0] + sp.encode_as_ids(text) + [2]
25
+
26
+ if len(tokens) > max_len:
27
+ tokens = tokens[:max_len]
28
+ tokens[-1] = 2
29
+ else:
30
+ tokens = tokens + [1] * (max_len - len(tokens))
31
+
32
+ for i, detail in enumerate(input_details):
33
+ shape = detail['shape']
34
+ dtype = detail['dtype']
35
+ input_data = np.zeros(shape, dtype=dtype)
36
+
37
+ if shape[1] == max_len:
38
+ if 'mask' in detail['name'].lower():
39
+ mask = [1] * min(len(sp.encode_as_ids(text)) + 2, max_len)
40
+ mask += [0] * (max_len - len(mask))
41
+ input_data[0] = np.array(mask, dtype=dtype)
42
+ elif 'type' in detail['name'].lower():
43
+ pass
44
+ else:
45
+ input_data[0] = np.array(tokens, dtype=dtype)
46
+
47
+ interpreter.set_tensor(detail['index'], input_data)
48
+
49
+ interpreter.invoke()
50
+ output_data = interpreter.get_tensor(output_details[0]['index'])
51
+
52
+ if output_data.shape[-1] == 2:
53
+ return bool(output_data[0][1] > 0.5)
54
+ else:
55
+ return bool(output_data[0][0] > 0.5)
56
+
57
+
58
  class TextRequest(BaseModel):
59
  text: str
60
 
61
+
62
  @app.post("/predict")
63
  def predict(request: TextRequest):
64
  try:
65
+ text = request.text
66
+
67
+ # Test multiple casing variations against the model.
68
+ # If ANY variation is toxic, we flag the whole text.
69
+ variations = set()
70
+ variations.add(text) # Original: "Fuck you"
71
+ variations.add(text.lower()) # Lowercase: "fuck you"
72
+ variations.add(text.upper()) # Uppercase: "FUCK YOU"
73
+ variations.add(text.capitalize()) # Capitalize: "Fuck you"
74
+ variations.add(text.title()) # Title: "Fuck You"
75
+
76
+ for variant in variations:
77
+ if run_inference(variant):
78
+ return {"is_toxic": True}
79
+
80
+ return {"is_toxic": False}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  except Exception as e:
82
  error_msg = traceback.format_exc()
83
  print(error_msg)
 
84
  return {"is_toxic": False, "error": error_msg}
85
 
86
+
87
  @app.get("/")
88
  def read_root():
89
  return {"status": "Toxic API is running!"}