|
from flask import Flask, request, jsonify, render_template_string |
|
from detoxify import Detoxify |
|
import os |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
model = Detoxify('multilingual') |
|
|
|
|
|
API_KEY = os.getenv('API_KEY') |
|
|
|
|
|
HTML_TEMPLATE = ''' |
|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>Detoxify API Test</title> |
|
</head> |
|
<body> |
|
<h1>Detoxify Multilingual API Test</h1> |
|
<form id="testForm"> |
|
<label for="api_key">API Key:</label> |
|
<input type="text" id="api_key" name="api_key" required><br><br> |
|
<label for="text">Analiz Edilecek Metin:</label> |
|
<input type="text" id="text" name="text" required><br><br> |
|
<button type="submit">Analiz Et</button> |
|
</form> |
|
<div id="results"></div> |
|
<script> |
|
document.getElementById('testForm').addEventListener('submit', function(event) { |
|
event.preventDefault(); |
|
const apiKey = document.getElementById('api_key').value; |
|
const text = document.getElementById('text').value; |
|
fetch('/predict', { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json' |
|
}, |
|
body: JSON.stringify({api_key: apiKey, texts: [text]}) |
|
}) |
|
.then(response => response.json()) |
|
.then(data => { |
|
const resultsDiv = document.getElementById('results'); |
|
if (data.error) { |
|
resultsDiv.innerHTML = `<p style="color:red;">Hata: ${data.error}</p>`; |
|
} else { |
|
let html = '<h2>Sonuçlar:</h2>'; |
|
for (const [key, value] of Object.entries(data)) { |
|
html += `<p>${key}: ${value[0].toFixed(5)}</p>`; |
|
} |
|
resultsDiv.innerHTML = html; |
|
} |
|
}) |
|
.catch(error => { |
|
console.error('Hata:', error); |
|
}); |
|
}); |
|
</script> |
|
</body> |
|
</html> |
|
''' |
|
|
|
|
|
@app.route('/') |
|
def home(): |
|
return render_template_string(HTML_TEMPLATE) |
|
|
|
|
|
@app.route('/predict', methods=['POST']) |
|
def predict(): |
|
|
|
data = request.get_json() |
|
api_key = data.get('api_key') |
|
texts = data.get('texts') |
|
|
|
|
|
if api_key != API_KEY: |
|
return jsonify({"error": "Geçersiz API anahtarı"}), 401 |
|
|
|
|
|
if not texts or not isinstance(texts, list): |
|
return jsonify({"error": "Geçersiz giriş, metin listesi bekleniyor"}), 400 |
|
|
|
|
|
results = model.predict(texts) |
|
return jsonify(results) |
|
|
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |