File size: 3,359 Bytes
0292df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify, render_template
from video_accent_analyzer import VideoAccentAnalyzer
import plotly
import json
import os

app = Flask(__name__)
analyzer = VideoAccentAnalyzer()

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/api/analyze', methods=['POST'])
def analyze():
    try:
        data = request.json
        url = data.get('url')
        duration = int(data.get('duration', 30))

        if not url:
            return jsonify({'error': 'No URL provided'}), 400

        # Initialize analyzer with display=False to avoid IPython dependency
        result = analyzer.analyze_video_url(url, max_duration=duration)

        if 'error' in result:
            return jsonify({'error': result['error']}), 400

        # Create Plotly figure
        probabilities = result['all_probabilities']
        accents = [analyzer.accent_display_names.get(acc, acc.title())
                  for acc in probabilities.keys()]
        probs = list(probabilities.values())

        # Format detailed results
        accent = result['predicted_accent']
        confidence = result['accent_confidence']
        english_conf = result['english_confidence']

        details = {
            'primary_classification': {
                'accent': analyzer.accent_display_names.get(accent, accent.title()),
                'confidence': f"{confidence:.1f}%",
                'english_confidence': f"{english_conf:.1f}%"
            },
            'audio_analysis': {
                'duration': f"{result['audio_duration']:.1f}s",
                'quality_score': result.get('audio_quality_score', 'N/A'),
                'chunks_analyzed': result.get('chunks_analyzed', 1)
            },
            'assessment': {
                'english_level': 'Strong' if english_conf >= 70 else 'Moderate' if english_conf >= 50 else 'Low',
                'confidence_level': 'High' if confidence >= 70 else 'Moderate' if confidence >= 50 else 'Low'
            }
        }

        # Add visualization data
        plot_data = {
            'data': [{
                'type': 'bar',
                'x': accents,
                'y': probs,
                'text': [f'{p:.1f}%' for p in probs],
                'textposition': 'auto',
                'marker': {
                    'color': ['#4CAF50' if p == max(probs) else '#2196F3'
                             if p >= 20 else '#FFC107' if p >= 10 else '#9E9E9E'
                             for p in probs]
                }
            }],
            'layout': {
                'title': 'Accent Probability Distribution',
                'xaxis': {'title': 'Accent Type'},
                'yaxis': {'title': 'Probability (%)', 'range': [0, 100]},
                'template': 'plotly_white'
            }
        }

        # Combine all results
        response = {
            'details': details,
            'plot': plot_data,
            'raw_results': result
        }

        return jsonify(response)

    except Exception as e:
        return jsonify({'error': str(e)}), 500

@app.route('/api/cleanup', methods=['POST'])
def cleanup():
    try:
        analyzer.cleanup()
        return jsonify({'message': 'Cleanup successful'})
    except Exception as e:
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True)