| 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 |
|
|
| |
| result = analyzer.analyze_video_url(url, max_duration=duration) |
|
|
| if 'error' in result: |
| return jsonify({'error': result['error']}), 400 |
|
|
| |
| probabilities = result['all_probabilities'] |
| accents = [analyzer.accent_display_names.get(acc, acc.title()) |
| for acc in probabilities.keys()] |
| probs = list(probabilities.values()) |
|
|
| |
| 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' |
| } |
| } |
|
|
| |
| 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' |
| } |
| } |
|
|
| |
| 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) |