Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,98 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
app = Flask(__name__)
|
| 7 |
-
classifier = SimpleOfflineAccentClassifier()
|
| 8 |
-
|
| 9 |
-
@app.route('/')
|
| 10 |
-
def home():
|
| 11 |
-
return render_template('index.html')
|
| 12 |
|
| 13 |
-
|
| 14 |
-
def analyze():
|
| 15 |
try:
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
# YouTube'dan ses indir
|
| 19 |
ydl_opts = {
|
| 20 |
'format': 'bestaudio/best',
|
| 21 |
'postprocessors': [{
|
| 22 |
'key': 'FFmpegExtractAudio',
|
| 23 |
'preferredcodec': 'wav',
|
| 24 |
}],
|
| 25 |
-
'outtmpl': '
|
| 26 |
'quiet': True,
|
| 27 |
'no_warnings': True
|
| 28 |
}
|
| 29 |
|
| 30 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 31 |
-
ydl.download([
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if result is None:
|
| 41 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from detect import SimpleOfflineAccentClassifier
|
| 3 |
import yt_dlp
|
| 4 |
import os
|
| 5 |
+
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
def download_youtube_audio(url):
|
|
|
|
| 8 |
try:
|
| 9 |
+
# Geçici dosya yolu oluştur
|
| 10 |
+
temp_dir = tempfile.gettempdir()
|
| 11 |
+
temp_file = os.path.join(temp_dir, 'temp_audio.wav')
|
| 12 |
|
|
|
|
| 13 |
ydl_opts = {
|
| 14 |
'format': 'bestaudio/best',
|
| 15 |
'postprocessors': [{
|
| 16 |
'key': 'FFmpegExtractAudio',
|
| 17 |
'preferredcodec': 'wav',
|
| 18 |
}],
|
| 19 |
+
'outtmpl': temp_file.replace('.wav', ''),
|
| 20 |
'quiet': True,
|
| 21 |
'no_warnings': True
|
| 22 |
}
|
| 23 |
|
| 24 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 25 |
+
ydl.download([url])
|
| 26 |
|
| 27 |
+
return temp_file
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Download error: {str(e)}")
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
def analyze_audio(audio_file, youtube_url):
|
| 33 |
+
try:
|
| 34 |
+
if youtube_url:
|
| 35 |
+
audio_file = download_youtube_audio(youtube_url)
|
| 36 |
+
if not audio_file:
|
| 37 |
+
return "Failed to download YouTube audio. Please check the URL and try again."
|
| 38 |
|
| 39 |
+
if not audio_file:
|
| 40 |
+
return "Please upload an audio file or provide a YouTube URL."
|
| 41 |
+
|
| 42 |
+
classifier = SimpleOfflineAccentClassifier()
|
| 43 |
+
result = classifier.predict_accent(audio_file)
|
| 44 |
|
| 45 |
if result is None:
|
| 46 |
+
return "Audio file processing failed."
|
| 47 |
+
|
| 48 |
+
output = f"Predicted Accent: {result['accent']}\n"
|
| 49 |
+
output += f"Confidence Score: {result['confidence']:.2%}\n\n"
|
| 50 |
+
output += "All Probabilities:\n"
|
| 51 |
+
|
| 52 |
+
sorted_probs = sorted(
|
| 53 |
+
result['all_probabilities'].items(),
|
| 54 |
+
key=lambda x: x[1],
|
| 55 |
+
reverse=True
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
for accent, prob in sorted_probs:
|
| 59 |
+
bar = "█" * int(prob * 20)
|
| 60 |
+
output += f"- {accent}: {prob:.2%} {bar}\n"
|
| 61 |
+
|
| 62 |
+
return output
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
+
return f"Error occurred: {str(e)}"
|
| 66 |
+
|
| 67 |
+
# Create Gradio interface
|
| 68 |
+
with gr.Blocks() as interface:
|
| 69 |
+
gr.Markdown("# AI Accent Classifier")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column():
|
| 73 |
+
audio_input = gr.Audio(
|
| 74 |
+
label="Upload Audio File",
|
| 75 |
+
type="filepath"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
youtube_url = gr.Textbox(
|
| 79 |
+
label="Or enter YouTube URL",
|
| 80 |
+
placeholder="https://www.youtube.com/watch?v=..."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
classify_btn = gr.Button("Analyze Accent")
|
| 84 |
+
|
| 85 |
+
with gr.Column():
|
| 86 |
+
output_text = gr.Markdown(
|
| 87 |
+
label="Analysis Results",
|
| 88 |
+
value="Analysis results will appear here..."
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
classify_btn.click(
|
| 92 |
+
fn=analyze_audio,
|
| 93 |
+
inputs=[audio_input, youtube_url],
|
| 94 |
+
outputs=output_text
|
| 95 |
+
)
|
| 96 |
|
| 97 |
+
# Launch the interface
|
| 98 |
+
interface.launch()
|