File size: 2,082 Bytes
de9ba6f
85efa8f
 
de9ba6f
 
 
 
 
 
 
 
3ed5ff4
de9ba6f
 
85efa8f
de9ba6f
1230d11
de9ba6f
85efa8f
 
a3641a5
 
 
3ed5ff4
de9ba6f
3ed5ff4
9e2e7e2
 
 
 
 
 
 
 
 
 
 
 
85efa8f
 
 
de9ba6f
e65bba6
de9ba6f
 
1230d11
de9ba6f
 
 
e65bba6
efabf1c
de9ba6f
e65bba6
de9ba6f
e65bba6
 
de9ba6f
e65bba6
de9ba6f
4ba2e38
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
from flask import Flask, render_template, request, jsonify
import yt_dlp
import os
from detect import SimpleOfflineAccentClassifier

app = Flask(__name__)
classifier = SimpleOfflineAccentClassifier()

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/analyze', methods=['POST'])
def analyze():
    try:
        video_url = request.form['url']
        
        # YouTube'dan ses indir
        ydl_opts = {
            'format': 'bestaudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'wav',
            }],
            'outtmpl': 'temp_audio',
            'quiet': True,
            'no_warnings': True,
            'extract_flat': True,  # Sadece ses indir
            'noplaylist': True,    # Playlist değil
            'ignoreerrors': True,  # Hataları görmezden gel
            'no_check_certificate': True,  # Sertifika kontrolünü atla
            'prefer_insecure': True,  # Güvenli olmayan bağlantıları tercih et
            'http_headers': {  # Tarayıcı gibi görün
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Language': 'en-us,en;q=0.5',
                'Sec-Fetch-Mode': 'navigate',
            }
        }
        
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.download([video_url])
        
        # Ses dosyasını analiz et
        result = classifier.predict_accent('temp_audio.wav')
        
        # Geçici dosyayı temizle
        if os.path.exists('temp_audio.wav'):
            os.remove('temp_audio.wav')
        
        if result is None:
            return jsonify({'error': 'voice analyze failed.'})
        
        return jsonify(result)
        
    except Exception as e:
        return jsonify({'error': str(e)})

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=7860)