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)