edited usage
Browse files
app.py
CHANGED
|
@@ -29,19 +29,25 @@ HTML_TEMPLATE = '''
|
|
| 29 |
<div class="container mx-auto px-4 py-8">
|
| 30 |
<h1 class="text-4xl font-bold mb-6 text-center">Modern Moderation API Test</h1>
|
| 31 |
<form id="testForm" class="bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4">
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
<div class="mb-4">
|
| 33 |
<label class="block text-gray-700 dark:text-gray-300 text-sm font-bold mb-2" for="model">Select Model:</label>
|
| 34 |
-
<select class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 dark:text-gray-900 leading-tight focus:outline-none focus:shadow-outline"
|
| 35 |
<option value="unitaryai/detoxify-multilingual" selected>unitaryai/detoxify-multilingual</option>
|
| 36 |
<option value="koalaai/text-moderation">koalaai/text-moderation</option>
|
| 37 |
</select>
|
| 38 |
</div>
|
| 39 |
<div class="mb-4">
|
| 40 |
<label class="block text-gray-700 dark:text-gray-300 text-sm font-bold mb-2" for="input">Text to Analyze:</label>
|
| 41 |
-
<textarea id="input" name="input" rows="4" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 dark:text-gray-900 leading-tight focus:outline-none focus:shadow-outline"
|
| 42 |
</div>
|
| 43 |
<div class="flex items-center justify-between">
|
| 44 |
-
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
|
|
|
|
|
|
|
| 45 |
</div>
|
| 46 |
</form>
|
| 47 |
<div id="results" class="mt-6"></div>
|
|
@@ -49,6 +55,7 @@ HTML_TEMPLATE = '''
|
|
| 49 |
<script>
|
| 50 |
document.getElementById('testForm').addEventListener('submit', async function(event) {
|
| 51 |
event.preventDefault();
|
|
|
|
| 52 |
const model = document.getElementById('model').value;
|
| 53 |
const input = document.getElementById('input').value;
|
| 54 |
try {
|
|
@@ -56,7 +63,7 @@ HTML_TEMPLATE = '''
|
|
| 56 |
method: 'POST',
|
| 57 |
headers: {
|
| 58 |
'Content-Type': 'application/json',
|
| 59 |
-
'Authorization': 'Bearer
|
| 60 |
},
|
| 61 |
body: JSON.stringify({ model: model, input: input })
|
| 62 |
});
|
|
@@ -93,7 +100,6 @@ def transform_predictions(model_choice, prediction_dict):
|
|
| 93 |
OpenAI tarzı response formatı için tahminleri dönüştür.
|
| 94 |
Dönüştürülen alanlar: flagged, categories, category_scores, category_applied_input_types
|
| 95 |
"""
|
| 96 |
-
# OpenAI'nın moderasyon API'sinde kullanılan kategori anahtarları
|
| 97 |
category_keys = [
|
| 98 |
"sexual", "sexual/minors", "harassment", "harassment/threatening",
|
| 99 |
"hate", "hate/threatening", "illicit", "illicit/violent",
|
|
@@ -101,7 +107,6 @@ def transform_predictions(model_choice, prediction_dict):
|
|
| 101 |
"violence", "violence/graphic"
|
| 102 |
]
|
| 103 |
scores = {}
|
| 104 |
-
# Detoxify modeli için örnek eşleştirme:
|
| 105 |
if model_choice == "unitaryai/detoxify-multilingual":
|
| 106 |
scores["sexual"] = prediction_dict.get("sexual_explicit", 0.0)
|
| 107 |
scores["harassment"] = max(prediction_dict.get("identity_attack", 0.0), prediction_dict.get("insult", 0.0))
|
|
@@ -114,12 +119,9 @@ def transform_predictions(model_choice, prediction_dict):
|
|
| 114 |
for key in category_keys:
|
| 115 |
scores[key] = prediction_dict.get(key, 0.0)
|
| 116 |
|
| 117 |
-
|
| 118 |
-
threshold = 0.7
|
| 119 |
bool_categories = {key: (scores[key] > threshold) for key in category_keys}
|
| 120 |
-
# category_applied_input_types: text ile çalışıldığı için
|
| 121 |
cat_applied_input_types = {key: (["text"] if scores[key] > 0 else []) for key in category_keys}
|
| 122 |
-
|
| 123 |
flagged = any(bool_categories.values())
|
| 124 |
|
| 125 |
return flagged, bool_categories, scores, cat_applied_input_types
|
|
@@ -130,7 +132,6 @@ def home():
|
|
| 130 |
|
| 131 |
@app.route('/v1/moderations', methods=['POST'])
|
| 132 |
def moderations():
|
| 133 |
-
# API key doğrulaması (Bearer token)
|
| 134 |
auth_header = request.headers.get('Authorization')
|
| 135 |
if not auth_header or not auth_header.startswith("Bearer "):
|
| 136 |
return jsonify({"error": "Unauthorized"}), 401
|
|
@@ -139,13 +140,10 @@ def moderations():
|
|
| 139 |
return jsonify({"error": "Unauthorized"}), 401
|
| 140 |
|
| 141 |
data = request.get_json()
|
| 142 |
-
|
| 143 |
-
# OpenAI API formatında "input" ya da "texts" kabul edilsin
|
| 144 |
raw_input = data.get('input') or data.get('texts')
|
| 145 |
if raw_input is None:
|
| 146 |
return jsonify({"error": "Invalid input, expected 'input' or 'texts' field"}), 400
|
| 147 |
|
| 148 |
-
# Eğer string ise listeye çevir
|
| 149 |
if isinstance(raw_input, str):
|
| 150 |
texts = [raw_input]
|
| 151 |
elif isinstance(raw_input, list):
|
|
@@ -153,11 +151,9 @@ def moderations():
|
|
| 153 |
else:
|
| 154 |
return jsonify({"error": "Invalid input format, expected string or list of strings"}), 400
|
| 155 |
|
| 156 |
-
# Maksimum 10 öğe
|
| 157 |
if len(texts) > 10:
|
| 158 |
return jsonify({"error": "Too many input items. Maximum 10 allowed."}), 400
|
| 159 |
|
| 160 |
-
# Her bir öğe maksimum 100k karakter olmalı
|
| 161 |
for text in texts:
|
| 162 |
if not isinstance(text, str) or len(text) > 100000:
|
| 163 |
return jsonify({"error": "Each input item must be a string with a maximum of 100k characters."}), 400
|
|
@@ -165,7 +161,6 @@ def moderations():
|
|
| 165 |
results = []
|
| 166 |
model_choice = data.get('model', 'unitaryai/detoxify-multilingual')
|
| 167 |
|
| 168 |
-
# Tahmin ve transform işlemi
|
| 169 |
if model_choice == "koalaai/text-moderation":
|
| 170 |
for text in texts:
|
| 171 |
inputs = koala_tokenizer(text, return_tensors="pt")
|
|
|
|
| 29 |
<div class="container mx-auto px-4 py-8">
|
| 30 |
<h1 class="text-4xl font-bold mb-6 text-center">Modern Moderation API Test</h1>
|
| 31 |
<form id="testForm" class="bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4">
|
| 32 |
+
<div class="mb-4">
|
| 33 |
+
<label class="block text-gray-700 dark:text-gray-300 text-sm font-bold mb-2" for="api_key">API Key:</label>
|
| 34 |
+
<input type="text" id="api_key" name="api_key" required class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 dark:text-gray-900 leading-tight focus:outline-none focus:shadow-outline">
|
| 35 |
+
</div>
|
| 36 |
<div class="mb-4">
|
| 37 |
<label class="block text-gray-700 dark:text-gray-300 text-sm font-bold mb-2" for="model">Select Model:</label>
|
| 38 |
+
<select id="model" name="model" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 dark:text-gray-900 leading-tight focus:outline-none focus:shadow-outline">
|
| 39 |
<option value="unitaryai/detoxify-multilingual" selected>unitaryai/detoxify-multilingual</option>
|
| 40 |
<option value="koalaai/text-moderation">koalaai/text-moderation</option>
|
| 41 |
</select>
|
| 42 |
</div>
|
| 43 |
<div class="mb-4">
|
| 44 |
<label class="block text-gray-700 dark:text-gray-300 text-sm font-bold mb-2" for="input">Text to Analyze:</label>
|
| 45 |
+
<textarea id="input" name="input" rows="4" required class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 dark:text-gray-900 leading-tight focus:outline-none focus:shadow-outline"></textarea>
|
| 46 |
</div>
|
| 47 |
<div class="flex items-center justify-between">
|
| 48 |
+
<button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
|
| 49 |
+
Analyze
|
| 50 |
+
</button>
|
| 51 |
</div>
|
| 52 |
</form>
|
| 53 |
<div id="results" class="mt-6"></div>
|
|
|
|
| 55 |
<script>
|
| 56 |
document.getElementById('testForm').addEventListener('submit', async function(event) {
|
| 57 |
event.preventDefault();
|
| 58 |
+
const apiKey = document.getElementById('api_key').value;
|
| 59 |
const model = document.getElementById('model').value;
|
| 60 |
const input = document.getElementById('input').value;
|
| 61 |
try {
|
|
|
|
| 63 |
method: 'POST',
|
| 64 |
headers: {
|
| 65 |
'Content-Type': 'application/json',
|
| 66 |
+
'Authorization': 'Bearer ' + apiKey
|
| 67 |
},
|
| 68 |
body: JSON.stringify({ model: model, input: input })
|
| 69 |
});
|
|
|
|
| 100 |
OpenAI tarzı response formatı için tahminleri dönüştür.
|
| 101 |
Dönüştürülen alanlar: flagged, categories, category_scores, category_applied_input_types
|
| 102 |
"""
|
|
|
|
| 103 |
category_keys = [
|
| 104 |
"sexual", "sexual/minors", "harassment", "harassment/threatening",
|
| 105 |
"hate", "hate/threatening", "illicit", "illicit/violent",
|
|
|
|
| 107 |
"violence", "violence/graphic"
|
| 108 |
]
|
| 109 |
scores = {}
|
|
|
|
| 110 |
if model_choice == "unitaryai/detoxify-multilingual":
|
| 111 |
scores["sexual"] = prediction_dict.get("sexual_explicit", 0.0)
|
| 112 |
scores["harassment"] = max(prediction_dict.get("identity_attack", 0.0), prediction_dict.get("insult", 0.0))
|
|
|
|
| 119 |
for key in category_keys:
|
| 120 |
scores[key] = prediction_dict.get(key, 0.0)
|
| 121 |
|
| 122 |
+
threshold = 0.5
|
|
|
|
| 123 |
bool_categories = {key: (scores[key] > threshold) for key in category_keys}
|
|
|
|
| 124 |
cat_applied_input_types = {key: (["text"] if scores[key] > 0 else []) for key in category_keys}
|
|
|
|
| 125 |
flagged = any(bool_categories.values())
|
| 126 |
|
| 127 |
return flagged, bool_categories, scores, cat_applied_input_types
|
|
|
|
| 132 |
|
| 133 |
@app.route('/v1/moderations', methods=['POST'])
|
| 134 |
def moderations():
|
|
|
|
| 135 |
auth_header = request.headers.get('Authorization')
|
| 136 |
if not auth_header or not auth_header.startswith("Bearer "):
|
| 137 |
return jsonify({"error": "Unauthorized"}), 401
|
|
|
|
| 140 |
return jsonify({"error": "Unauthorized"}), 401
|
| 141 |
|
| 142 |
data = request.get_json()
|
|
|
|
|
|
|
| 143 |
raw_input = data.get('input') or data.get('texts')
|
| 144 |
if raw_input is None:
|
| 145 |
return jsonify({"error": "Invalid input, expected 'input' or 'texts' field"}), 400
|
| 146 |
|
|
|
|
| 147 |
if isinstance(raw_input, str):
|
| 148 |
texts = [raw_input]
|
| 149 |
elif isinstance(raw_input, list):
|
|
|
|
| 151 |
else:
|
| 152 |
return jsonify({"error": "Invalid input format, expected string or list of strings"}), 400
|
| 153 |
|
|
|
|
| 154 |
if len(texts) > 10:
|
| 155 |
return jsonify({"error": "Too many input items. Maximum 10 allowed."}), 400
|
| 156 |
|
|
|
|
| 157 |
for text in texts:
|
| 158 |
if not isinstance(text, str) or len(text) > 100000:
|
| 159 |
return jsonify({"error": "Each input item must be a string with a maximum of 100k characters."}), 400
|
|
|
|
| 161 |
results = []
|
| 162 |
model_choice = data.get('model', 'unitaryai/detoxify-multilingual')
|
| 163 |
|
|
|
|
| 164 |
if model_choice == "koalaai/text-moderation":
|
| 165 |
for text in texts:
|
| 166 |
inputs = koala_tokenizer(text, return_tensors="pt")
|