File size: 3,073 Bytes
2ec7d5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from flask import Flask, request, jsonify, render_template_string
from detoxify import Detoxify
import os

# Flask uygulamasını başlat
app = Flask(__name__)

# Detoxify Multilingual modelini yükle
model = Detoxify('multilingual')

# API anahtarını environment variable'dan al
API_KEY = os.getenv('API_KEY')

# Test arayüzü için HTML şablonu
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>
'''

# Ana sayfa route'u
@app.route('/')
def home():
    return render_template_string(HTML_TEMPLATE)

# API endpoint'i
@app.route('/predict', methods=['POST'])
def predict():
    # İstekten JSON verisini al
    data = request.get_json()
    api_key = data.get('api_key')
    texts = data.get('texts')
    
    # API anahtarını kontrol et
    if api_key != API_KEY:
        return jsonify({"error": "Geçersiz API anahtarı"}), 401
    
    # Girişin geçerli olduğunu kontrol et
    if not texts or not isinstance(texts, list):
        return jsonify({"error": "Geçersiz giriş, metin listesi bekleniyor"}), 400
    
    # Detoxify modeliyle tahmin yap
    results = model.predict(texts)
    return jsonify(results)

# Sunucuyu çalıştır (HuggingFace Spaces'de bu satır otomatik işlenir)
if __name__ == '__main__':
    app.run(debug=True)