mguven61 commited on
Commit
2b704e9
·
verified ·
1 Parent(s): 735b4bc

Update app.py

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