CoolFace
Apppublic

ferhatbou/detect_English_language_speaking

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
api.py101 linesDownload Raw Back to root
1from flask import Flask, request, jsonify, render_template2from video_accent_analyzer import VideoAccentAnalyzer3import plotly4import json5import os6 7app = Flask(__name__)8analyzer = VideoAccentAnalyzer()9 10@app.route('/')11def home():12    return render_template('index.html')13 14@app.route('/api/analyze', methods=['POST'])15def analyze():16    try:17        data = request.json18        url = data.get('url')19        duration = int(data.get('duration', 30))20 21        if not url:22            return jsonify({'error': 'No URL provided'}), 40023 24        # Initialize analyzer with display=False to avoid IPython dependency25        result = analyzer.analyze_video_url(url, max_duration=duration)26 27        if 'error' in result:28            return jsonify({'error': result['error']}), 40029 30        # Create Plotly figure31        probabilities = result['all_probabilities']32        accents = [analyzer.accent_display_names.get(acc, acc.title())33                  for acc in probabilities.keys()]34        probs = list(probabilities.values())35 36        # Format detailed results37        accent = result['predicted_accent']38        confidence = result['accent_confidence']39        english_conf = result['english_confidence']40 41        details = {42            'primary_classification': {43                'accent': analyzer.accent_display_names.get(accent, accent.title()),44                'confidence': f"{confidence:.1f}%",45                'english_confidence': f"{english_conf:.1f}%"46            },47            'audio_analysis': {48                'duration': f"{result['audio_duration']:.1f}s",49                'quality_score': result.get('audio_quality_score', 'N/A'),50                'chunks_analyzed': result.get('chunks_analyzed', 1)51            },52            'assessment': {53                'english_level': 'Strong' if english_conf >= 70 else 'Moderate' if english_conf >= 50 else 'Low',54                'confidence_level': 'High' if confidence >= 70 else 'Moderate' if confidence >= 50 else 'Low'55            }56        }57 58        # Add visualization data59        plot_data = {60            'data': [{61                'type': 'bar',62                'x': accents,63                'y': probs,64                'text': [f'{p:.1f}%' for p in probs],65                'textposition': 'auto',66                'marker': {67                    'color': ['#4CAF50' if p == max(probs) else '#2196F3'68                             if p >= 20 else '#FFC107' if p >= 10 else '#9E9E9E'69                             for p in probs]70                }71            }],72            'layout': {73                'title': 'Accent Probability Distribution',74                'xaxis': {'title': 'Accent Type'},75                'yaxis': {'title': 'Probability (%)', 'range': [0, 100]},76                'template': 'plotly_white'77            }78        }79 80        # Combine all results81        response = {82            'details': details,83            'plot': plot_data,84            'raw_results': result85        }86 87        return jsonify(response)88 89    except Exception as e:90        return jsonify({'error': str(e)}), 50091 92@app.route('/api/cleanup', methods=['POST'])93def cleanup():94    try:95        analyzer.cleanup()96        return jsonify({'message': 'Cleanup successful'})97    except Exception as e:98        return jsonify({'error': str(e)}), 50099 100if __name__ == '__main__':101    app.run(debug=True)