changed usage
Browse files
app.py
CHANGED
|
@@ -37,8 +37,8 @@ HTML_TEMPLATE = '''
|
|
| 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="
|
| 41 |
-
<input 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" type="submit">Analyze</button>
|
|
@@ -50,7 +50,7 @@ HTML_TEMPLATE = '''
|
|
| 50 |
document.getElementById('testForm').addEventListener('submit', async function(event) {
|
| 51 |
event.preventDefault();
|
| 52 |
const model = document.getElementById('model').value;
|
| 53 |
-
const
|
| 54 |
try {
|
| 55 |
const response = await fetch('/v1/moderations', {
|
| 56 |
method: 'POST',
|
|
@@ -58,7 +58,7 @@ HTML_TEMPLATE = '''
|
|
| 58 |
'Content-Type': 'application/json',
|
| 59 |
'Authorization': 'Bearer YOUR_API_KEY' // Değiştir!
|
| 60 |
},
|
| 61 |
-
body: JSON.stringify({ model: model,
|
| 62 |
});
|
| 63 |
const data = await response.json();
|
| 64 |
const resultsDiv = document.getElementById('results');
|
|
@@ -107,23 +107,19 @@ def transform_predictions(model_choice, prediction_dict):
|
|
| 107 |
scores["harassment"] = max(prediction_dict.get("identity_attack", 0.0), prediction_dict.get("insult", 0.0))
|
| 108 |
scores["hate"] = prediction_dict.get("toxicity", 0.0)
|
| 109 |
scores["violence"] = max(prediction_dict.get("severe_toxicity", 0.0), prediction_dict.get("threat", 0.0))
|
| 110 |
-
# Diğer kategoriler için varsayılan 0 değeri
|
| 111 |
for key in category_keys:
|
| 112 |
if key not in scores:
|
| 113 |
scores[key] = 0.0
|
| 114 |
else:
|
| 115 |
-
# Koala modelinden gelen tahminlerde, label isimleri doğrudan uyumlu olabilir;
|
| 116 |
-
# uyumlu değilse varsayılan 0 değeri ver.
|
| 117 |
for key in category_keys:
|
| 118 |
scores[key] = prediction_dict.get(key, 0.0)
|
| 119 |
|
| 120 |
-
#
|
| 121 |
threshold = 0.7
|
| 122 |
bool_categories = {key: (scores[key] > threshold) for key in category_keys}
|
| 123 |
-
# category_applied_input_types:
|
| 124 |
cat_applied_input_types = {key: (["text"] if scores[key] > 0 else []) for key in category_keys}
|
| 125 |
|
| 126 |
-
# Flagged: herhangi bir kategori eşik değerinin üzerinde ise True
|
| 127 |
flagged = any(bool_categories.values())
|
| 128 |
|
| 129 |
return flagged, bool_categories, scores, cat_applied_input_types
|
|
@@ -134,7 +130,7 @@ def home():
|
|
| 134 |
|
| 135 |
@app.route('/v1/moderations', methods=['POST'])
|
| 136 |
def moderations():
|
| 137 |
-
#
|
| 138 |
auth_header = request.headers.get('Authorization')
|
| 139 |
if not auth_header or not auth_header.startswith("Bearer "):
|
| 140 |
return jsonify({"error": "Unauthorized"}), 401
|
|
@@ -143,21 +139,39 @@ def moderations():
|
|
| 143 |
return jsonify({"error": "Unauthorized"}), 401
|
| 144 |
|
| 145 |
data = request.get_json()
|
| 146 |
-
texts = data.get('texts')
|
| 147 |
-
model_choice = data.get('model', 'unitaryai/detoxify-multilingual')
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
results = []
|
| 153 |
-
|
|
|
|
|
|
|
| 154 |
if model_choice == "koalaai/text-moderation":
|
| 155 |
for text in texts:
|
| 156 |
inputs = koala_tokenizer(text, return_tensors="pt")
|
| 157 |
outputs = koala_model(**inputs)
|
| 158 |
logits = outputs.logits
|
| 159 |
probabilities = torch.softmax(logits, dim=-1).squeeze().tolist()
|
| 160 |
-
# Eğer tek değer ise listeye çevir
|
| 161 |
if isinstance(probabilities, float):
|
| 162 |
probabilities = [probabilities]
|
| 163 |
labels = [koala_model.config.id2label[idx] for idx in range(len(probabilities))]
|
|
@@ -173,7 +187,6 @@ def moderations():
|
|
| 173 |
else:
|
| 174 |
for text in texts:
|
| 175 |
pred = detoxify_model.predict([text])
|
| 176 |
-
# Detoxify sonuçları liste formatında, tek değer alıyoruz
|
| 177 |
prediction = {k: v[0] for k, v in pred.items()}
|
| 178 |
flagged, bool_categories, scores, cat_applied_input_types = transform_predictions(model_choice, prediction)
|
| 179 |
results.append({
|
|
|
|
| 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" required></textarea>
|
| 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" type="submit">Analyze</button>
|
|
|
|
| 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 {
|
| 55 |
const response = await fetch('/v1/moderations', {
|
| 56 |
method: 'POST',
|
|
|
|
| 58 |
'Content-Type': 'application/json',
|
| 59 |
'Authorization': 'Bearer YOUR_API_KEY' // Değiştir!
|
| 60 |
},
|
| 61 |
+
body: JSON.stringify({ model: model, input: input })
|
| 62 |
});
|
| 63 |
const data = await response.json();
|
| 64 |
const resultsDiv = document.getElementById('results');
|
|
|
|
| 107 |
scores["harassment"] = max(prediction_dict.get("identity_attack", 0.0), prediction_dict.get("insult", 0.0))
|
| 108 |
scores["hate"] = prediction_dict.get("toxicity", 0.0)
|
| 109 |
scores["violence"] = max(prediction_dict.get("severe_toxicity", 0.0), prediction_dict.get("threat", 0.0))
|
|
|
|
| 110 |
for key in category_keys:
|
| 111 |
if key not in scores:
|
| 112 |
scores[key] = 0.0
|
| 113 |
else:
|
|
|
|
|
|
|
| 114 |
for key in category_keys:
|
| 115 |
scores[key] = prediction_dict.get(key, 0.0)
|
| 116 |
|
| 117 |
+
# Eşik değeri: 0.7
|
| 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 |
|
| 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 |
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):
|
| 152 |
+
texts = raw_input
|
| 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
|
| 164 |
|
| 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")
|
| 172 |
outputs = koala_model(**inputs)
|
| 173 |
logits = outputs.logits
|
| 174 |
probabilities = torch.softmax(logits, dim=-1).squeeze().tolist()
|
|
|
|
| 175 |
if isinstance(probabilities, float):
|
| 176 |
probabilities = [probabilities]
|
| 177 |
labels = [koala_model.config.id2label[idx] for idx in range(len(probabilities))]
|
|
|
|
| 187 |
else:
|
| 188 |
for text in texts:
|
| 189 |
pred = detoxify_model.predict([text])
|
|
|
|
| 190 |
prediction = {k: v[0] for k, v in pred.items()}
|
| 191 |
flagged, bool_categories, scores, cat_applied_input_types = transform_predictions(model_choice, prediction)
|
| 192 |
results.append({
|