Spaces:
Sleeping
Sleeping
Edited usage
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from flask import Flask, request, jsonify, render_template_string
|
| 2 |
-
from detoxify import Detoxify
|
| 3 |
import os
|
| 4 |
import torch
|
|
|
|
| 5 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
@@ -11,27 +11,23 @@ detoxify_model = Detoxify('multilingual')
|
|
| 11 |
koala_model = AutoModelForSequenceClassification.from_pretrained("KoalaAI/Text-Moderation")
|
| 12 |
koala_tokenizer = AutoTokenizer.from_pretrained("KoalaAI/Text-Moderation")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
API_KEY = os.getenv('API_KEY')
|
| 16 |
|
| 17 |
-
# Modern HTML interface with Tailwind CSS
|
| 18 |
HTML_TEMPLATE = '''
|
| 19 |
<!DOCTYPE html>
|
| 20 |
<html lang="en">
|
| 21 |
<head>
|
| 22 |
<meta charset="UTF-8">
|
| 23 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 24 |
-
<title>Modern
|
| 25 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 26 |
</head>
|
| 27 |
<body class="bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-100">
|
| 28 |
<div class="container mx-auto px-4 py-8">
|
| 29 |
-
<h1 class="text-4xl font-bold mb-6 text-center">Modern
|
| 30 |
<form id="testForm" class="bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4">
|
| 31 |
-
<div class="mb-4">
|
| 32 |
-
<label class="block text-gray-700 dark:text-gray-300 text-sm font-bold mb-2" for="api_key">API Key:</label>
|
| 33 |
-
<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" type="text" id="api_key" name="api_key" required>
|
| 34 |
-
</div>
|
| 35 |
<div class="mb-4">
|
| 36 |
<label class="block text-gray-700 dark:text-gray-300 text-sm font-bold mb-2" for="model">Select Model:</label>
|
| 37 |
<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" id="model" name="model">
|
|
@@ -52,14 +48,16 @@ HTML_TEMPLATE = '''
|
|
| 52 |
<script>
|
| 53 |
document.getElementById('testForm').addEventListener('submit', async function(event) {
|
| 54 |
event.preventDefault();
|
| 55 |
-
const apiKey = document.getElementById('api_key').value;
|
| 56 |
const model = document.getElementById('model').value;
|
| 57 |
const text = document.getElementById('text').value;
|
| 58 |
try {
|
| 59 |
const response = await fetch('/v1/moderations', {
|
| 60 |
method: 'POST',
|
| 61 |
-
headers: {
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
});
|
| 64 |
const data = await response.json();
|
| 65 |
const resultsDiv = document.getElementById('results');
|
|
@@ -88,19 +86,26 @@ HTML_TEMPLATE = '''
|
|
| 88 |
</html>
|
| 89 |
'''
|
| 90 |
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
@app.route('/v1/moderations', methods=['POST'])
|
| 93 |
def moderations():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
data = request.get_json()
|
| 95 |
-
api_key = data.get('api_key')
|
| 96 |
texts = data.get('texts')
|
| 97 |
model_choice = data.get('model', 'unitaryai/detoxify-multilingual')
|
| 98 |
|
| 99 |
-
if api_key != API_KEY:
|
| 100 |
-
return jsonify({"error": "Geçersiz API anahtarı"}), 401
|
| 101 |
-
|
| 102 |
if not texts or not isinstance(texts, list):
|
| 103 |
-
return jsonify({"error": "
|
| 104 |
|
| 105 |
results = []
|
| 106 |
if model_choice == "koalaai/text-moderation":
|
|
@@ -109,7 +114,6 @@ def moderations():
|
|
| 109 |
outputs = koala_model(**inputs)
|
| 110 |
logits = outputs.logits
|
| 111 |
probabilities = torch.softmax(logits, dim=-1).squeeze().tolist()
|
| 112 |
-
# Ensure probabilities is a list
|
| 113 |
if isinstance(probabilities, float):
|
| 114 |
probabilities = [probabilities]
|
| 115 |
labels = [koala_model.config.id2label[idx] for idx in range(len(probabilities))]
|
|
@@ -119,7 +123,6 @@ def moderations():
|
|
| 119 |
else:
|
| 120 |
for text in texts:
|
| 121 |
pred = detoxify_model.predict([text])
|
| 122 |
-
# Convert Detoxify output (lists) to a simple dict with float values
|
| 123 |
prediction = {k: v[0] for k, v in pred.items()}
|
| 124 |
results.append({"input": text, "predictions": prediction})
|
| 125 |
response_model = "unitaryai/detoxify-multilingual"
|
|
@@ -131,10 +134,6 @@ def moderations():
|
|
| 131 |
}
|
| 132 |
return jsonify(response_data)
|
| 133 |
|
| 134 |
-
@app.route('/')
|
| 135 |
-
def home():
|
| 136 |
-
return render_template_string(HTML_TEMPLATE)
|
| 137 |
-
|
| 138 |
if __name__ == '__main__':
|
| 139 |
port = int(os.getenv('PORT', 7860))
|
| 140 |
app.run(host='0.0.0.0', port=port, debug=True)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template_string
|
|
|
|
| 2 |
import os
|
| 3 |
import torch
|
| 4 |
+
from detoxify import Detoxify
|
| 5 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
|
|
| 11 |
koala_model = AutoModelForSequenceClassification.from_pretrained("KoalaAI/Text-Moderation")
|
| 12 |
koala_tokenizer = AutoTokenizer.from_pretrained("KoalaAI/Text-Moderation")
|
| 13 |
|
| 14 |
+
# API key from environment variable
|
| 15 |
API_KEY = os.getenv('API_KEY')
|
| 16 |
|
| 17 |
+
# Modern HTML interface with Tailwind CSS
|
| 18 |
HTML_TEMPLATE = '''
|
| 19 |
<!DOCTYPE html>
|
| 20 |
<html lang="en">
|
| 21 |
<head>
|
| 22 |
<meta charset="UTF-8">
|
| 23 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 24 |
+
<title>Modern Moderation API Test</title>
|
| 25 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 26 |
</head>
|
| 27 |
<body class="bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-100">
|
| 28 |
<div class="container mx-auto px-4 py-8">
|
| 29 |
+
<h1 class="text-4xl font-bold mb-6 text-center">Modern Moderation API Test</h1>
|
| 30 |
<form id="testForm" class="bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4">
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
<div class="mb-4">
|
| 32 |
<label class="block text-gray-700 dark:text-gray-300 text-sm font-bold mb-2" for="model">Select Model:</label>
|
| 33 |
<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" id="model" name="model">
|
|
|
|
| 48 |
<script>
|
| 49 |
document.getElementById('testForm').addEventListener('submit', async function(event) {
|
| 50 |
event.preventDefault();
|
|
|
|
| 51 |
const model = document.getElementById('model').value;
|
| 52 |
const text = document.getElementById('text').value;
|
| 53 |
try {
|
| 54 |
const response = await fetch('/v1/moderations', {
|
| 55 |
method: 'POST',
|
| 56 |
+
headers: {
|
| 57 |
+
'Content-Type': 'application/json',
|
| 58 |
+
'Authorization': 'Bearer YOUR_API_KEY' // Replace YOUR_API_KEY with your actual API key
|
| 59 |
+
},
|
| 60 |
+
body: JSON.stringify({ model: model, texts: [text] })
|
| 61 |
});
|
| 62 |
const data = await response.json();
|
| 63 |
const resultsDiv = document.getElementById('results');
|
|
|
|
| 86 |
</html>
|
| 87 |
'''
|
| 88 |
|
| 89 |
+
@app.route('/')
|
| 90 |
+
def home():
|
| 91 |
+
return render_template_string(HTML_TEMPLATE)
|
| 92 |
+
|
| 93 |
@app.route('/v1/moderations', methods=['POST'])
|
| 94 |
def moderations():
|
| 95 |
+
# Retrieve API key from Authorization header in Bearer format
|
| 96 |
+
auth_header = request.headers.get('Authorization')
|
| 97 |
+
if not auth_header or not auth_header.startswith("Bearer "):
|
| 98 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 99 |
+
provided_api_key = auth_header.split(" ")[1]
|
| 100 |
+
if provided_api_key != API_KEY:
|
| 101 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 102 |
+
|
| 103 |
data = request.get_json()
|
|
|
|
| 104 |
texts = data.get('texts')
|
| 105 |
model_choice = data.get('model', 'unitaryai/detoxify-multilingual')
|
| 106 |
|
|
|
|
|
|
|
|
|
|
| 107 |
if not texts or not isinstance(texts, list):
|
| 108 |
+
return jsonify({"error": "Invalid input, expected a list of texts"}), 400
|
| 109 |
|
| 110 |
results = []
|
| 111 |
if model_choice == "koalaai/text-moderation":
|
|
|
|
| 114 |
outputs = koala_model(**inputs)
|
| 115 |
logits = outputs.logits
|
| 116 |
probabilities = torch.softmax(logits, dim=-1).squeeze().tolist()
|
|
|
|
| 117 |
if isinstance(probabilities, float):
|
| 118 |
probabilities = [probabilities]
|
| 119 |
labels = [koala_model.config.id2label[idx] for idx in range(len(probabilities))]
|
|
|
|
| 123 |
else:
|
| 124 |
for text in texts:
|
| 125 |
pred = detoxify_model.predict([text])
|
|
|
|
| 126 |
prediction = {k: v[0] for k, v in pred.items()}
|
| 127 |
results.append({"input": text, "predictions": prediction})
|
| 128 |
response_model = "unitaryai/detoxify-multilingual"
|
|
|
|
| 134 |
}
|
| 135 |
return jsonify(response_data)
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
if __name__ == '__main__':
|
| 138 |
port = int(os.getenv('PORT', 7860))
|
| 139 |
app.run(host='0.0.0.0', port=port, debug=True)
|