File size: 4,120 Bytes
2b704e9
 
85efa8f
 
1230d11
a4b5ff1
02dd59a
a4b5ff1
 
 
e65bba6
2b704e9
85efa8f
a4b5ff1
 
1230d11
 
 
 
a3641a5
85efa8f
 
1230d11
85efa8f
a4b5ff1
a3641a5
 
 
 
 
 
 
85efa8f
 
a3641a5
85efa8f
a4b5ff1
a3641a5
 
 
 
 
 
 
 
 
a4b5ff1
e65bba6
a4b5ff1
 
 
 
 
 
2b704e9
a4b5ff1
2b704e9
 
 
 
1230d11
a4b5ff1
1230d11
 
 
 
 
 
 
a4b5ff1
2b704e9
 
e65bba6
efabf1c
2b704e9
 
 
 
 
 
 
 
 
 
 
e65bba6
2b704e9
 
 
 
 
e65bba6
 
a4b5ff1
2b704e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e65bba6
2b704e9
a4b5ff1
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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')
        
        # Basit yt-dlp ayarları
        ydl_opts = {
            'format': 'bestaudio/best',
            'outtmpl': temp_file.replace('.wav', ''),
            'quiet': True,
            'no_warnings': True,
            'extract_flat': True,
            'noplaylist': True,
            'ignoreerrors': True,
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'wav',
            }]
        }
        
        # İndirme işlemi
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            logger.debug("Starting download...")
            info = ydl.extract_info(url, download=True)
            logger.debug(f"Download info: {info}")
            
            # Dosya adını düzelt
            if os.path.exists(temp_file.replace('.wav', '.webm')):
                os.rename(temp_file.replace('.wav', '.webm'), temp_file)
            elif os.path.exists(temp_file.replace('.wav', '.m4a')):
                os.rename(temp_file.replace('.wav', '.m4a'), temp_file)
            
            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)