import gradio as gr from detect import SimpleOfflineAccentClassifier import yt_dlp import os import tempfile import logging # Logging ayarları logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) def download_youtube_audio(url): try: logger.debug(f"Downloading from URL: {url}") # Geçici dosya yolu oluştur temp_dir = tempfile.gettempdir() temp_file = os.path.join(temp_dir, 'temp_audio.wav') ydl_opts = { 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'wav', }], 'outtmpl': temp_file.replace('.wav', ''), 'quiet': True, 'no_warnings': True, 'extract_flat': True, # Sadece ses indir 'noplaylist': True, # Playlist değil 'no_warnings': True, 'ignoreerrors': True } with yt_dlp.YoutubeDL(ydl_opts) as ydl: logger.debug("Starting download...") ydl.download([url]) logger.debug(f"Download complete. File saved to: {temp_file}") if os.path.exists(temp_file): return temp_file else: logger.error("Downloaded file not found") return None except Exception as e: logger.exception(f"Download error: {str(e)}") return None def analyze_audio(audio_file, youtube_url): try: if youtube_url: logger.debug(f"Processing YouTube URL: {youtube_url}") audio_file = download_youtube_audio(youtube_url) if not audio_file: return "Failed to download YouTube audio. Please check the URL and try again." if not audio_file: return "Please upload an audio file or provide a YouTube URL." logger.debug(f"Processing audio file: {audio_file}") classifier = SimpleOfflineAccentClassifier() result = classifier.predict_accent(audio_file) if result is None: return "Audio file processing failed." output = f"Predicted Accent: {result['accent']}\n" output += f"Confidence Score: {result['confidence']:.2%}\n\n" output += "All Probabilities:\n" sorted_probs = sorted( result['all_probabilities'].items(), key=lambda x: x[1], reverse=True ) for accent, prob in sorted_probs: bar = "█" * int(prob * 20) output += f"- {accent}: {prob:.2%} {bar}\n" return output except Exception as e: logger.exception(f"Analysis error: {str(e)}") return f"Error occurred: {str(e)}" # Create Gradio interface with gr.Blocks() as interface: gr.Markdown("# AI Accent Classifier") with gr.Row(): with gr.Column(): audio_input = gr.Audio( label="Upload Audio File", type="filepath" ) youtube_url = gr.Textbox( label="Or enter YouTube URL", placeholder="https://www.youtube.com/watch?v=..." ) classify_btn = gr.Button("Analyze Accent") with gr.Column(): output_text = gr.Markdown( label="Analysis Results", value="Analysis results will appear here..." ) classify_btn.click( fn=analyze_audio, inputs=[audio_input, youtube_url], outputs=output_text ) # Launch the interface interface.launch(debug=True)