import gradio as gr import os from pipeline import AuthenticityDetectionPipeline import traceback # initialize the pipeline on startup try: pipeline = AuthenticityDetectionPipeline(whisper_model_size="base") pipeline_ready = True except Exception as e: pipeline_ready = False pipeline_error = str(e) import traceback print(f"Could not start pipeline: {e}") traceback.print_exc() # build the acoustic features display HTML def build_acoustic_features_display(audio_class): classification = audio_class['classification'] confidence = audio_class['confidence'] cnn_class = audio_class['cnn_classification'] cnn_conf = audio_class['cnn_confidence'] prosody_class = audio_class['prosody_classification'] prosody_conf = audio_class['prosody_confidence'] prosody_scores = audio_class.get('prosody_scores', {}) acoustic_features = audio_class.get('acoustic_features', {}) # color scheme based on classification if classification == 'spontaneous': main_color = '#10b981' bg_color = '#ecfdf5' label = 'SPONTANEOUS' else: main_color = '#f59e0b' bg_color = '#fffbeb' label = 'READ' cnn_color = '#10b981' if cnn_class == 'spontaneous' else '#f59e0b' prosody_color = '#10b981' if prosody_class == 'spontaneous' else '#f59e0b' # build main classification header output = f"""

{label} SPEECH

Combined acoustic analysis confidence: {confidence*100:.1f}%

Analysis Components

CNN Neural Network
{cnn_class.upper()}
{cnn_conf*100:.1f}% confidence
Prosody Analysis
{prosody_class.upper()}
{prosody_conf*100:.1f}% confidence

Prosody Feature Breakdown

""" # feature descriptions feature_info = { 'spectral_variability': {'name': 'Spectral Variability', 'unit': 'Hz', 'description': 'Variation in frequency content over time'}, 'zcr_mean': {'name': 'Zero Crossing Rate', 'unit': 'ratio', 'description': 'Rate of signal sign changes'}, 'energy_level': {'name': 'Energy Level', 'unit': 'RMS', 'description': 'Overall loudness and intensity'}, 'tempo': {'name': 'Speech Tempo', 'unit': 'BPM', 'description': 'Rhythmic pacing of speech'} } # add feature details for key, info in feature_info.items(): if key in prosody_scores: score_data = prosody_scores[key] score = score_data['score'] value = score_data['value'] interp = score_data['interpretation'] unit = info['unit'] bar_color = '#10b981' if score < 0.4 else '#f59e0b' if score > 0.6 else '#6b7280' indicator_position = score * 100 output += f"""
{info['name']}
{info['description']}
{interp}
{value:.3f} {unit}
Spontaneous Read
""" output += "
" # add raw acoustic measurements output += """

Raw Acoustic Measurements

""" if acoustic_features: metrics = [ ('Tempo', f"{acoustic_features.get('tempo', 0):.1f}", 'BPM'), ('Pitch Mean', f"{acoustic_features.get('pitch_mean', 0):.1f}", 'Hz'), ('Energy Mean', f"{acoustic_features.get('energy_mean', 0):.4f}", ''), ('ZCR Mean', f"{acoustic_features.get('zcr_mean', 0):.4f}", ''), ] for name, value, unit in metrics: output += f"""
{value}
{name} {unit}
""" output += """
""" return output # build the transcription display HTML def build_transcription_display(asr): # determine speech rate interpretation if asr['speech_rate'] > 160: rate_color = '#f59e0b' rate_label = 'Fast' rate_desc = 'Above average speaking speed' elif asr['speech_rate'] < 120: rate_color = '#3b82f6' rate_label = 'Slow' rate_desc = 'Below average speaking speed' else: rate_color = '#10b981' rate_label = 'Normal' rate_desc = 'Average conversational pace' output = f"""

Speech Transcription

Detected language: {asr['language'].upper()}

Speech Metrics

{asr['duration']:.1f}
Duration (sec)
{asr['word_count']}
Words
{asr['speech_rate']:.0f}
Words/min
{rate_label}
{rate_desc}

Full Transcription

"{asr['transcription']}"

""" return output # build the speech patterns display HTML def build_speech_patterns_display(asr): output = "" # kopparapu classification section if 'kopparapu_score' in asr: classification = asr['kopparapu_classification'].upper() kop_score = asr['kopparapu_score'] confidence = kop_score if kop_score >= 0.5 else (1 - kop_score) if classification == 'SPONTANEOUS': class_color = '#10b981' class_bg = '#ecfdf5' else: class_color = '#f59e0b' class_bg = '#fffbeb' kf = asr['kopparapu_features'] # interpret linguistic metrics cpw = kf['chars_per_word'] cpw_interp = 'Complex vocabulary' if cpw > 5.5 else 'Simple vocabulary' if cpw < 4.5 else 'Average' wps = kf['words_per_sec'] wps_interp = 'Fast pace' if wps > 3 else 'Slow pace' if wps < 2 else 'Normal pace' fr = kf['filler_rate'] * 100 fr_interp = 'High (spontaneous)' if fr > 5 else 'Low (scripted)' if fr < 2 else 'Moderate' rep = kf['repetition_count'] rep_interp = 'Multiple (thinking aloud)' if rep > 3 else 'None (prepared)' if rep == 0 else 'Few' output += f"""

{classification} SPEECH

Linguistic analysis confidence: {confidence*100:.1f}%

Linguistic Metrics

{cpw:.2f}
Chars/Word
{cpw_interp}
{wps:.2f}
Words/Sec
{wps_interp}
{fr:.1f}%
Filler Rate
{fr_interp}
{rep}
Repetitions
{rep_interp}
""" # filler words section filler_ratio = asr['filler_words']['ratio'] filler_count = asr['filler_words']['count'] if filler_ratio > 0.05: filler_color = '#10b981' filler_label = 'High filler usage' filler_desc = 'Strong indicator of spontaneous speech' elif filler_ratio < 0.02: filler_color = '#f59e0b' filler_label = 'Low filler usage' filler_desc = 'May indicate reading or rehearsed speech' else: filler_color = '#6b7280' filler_label = 'Moderate filler usage' filler_desc = 'Normal conversational pattern' output += f"""

Filler Words

{filler_count}
Filler Words
{filler_ratio*100:.1f}%
Of Speech
{filler_label}
{filler_desc}
""" # pause patterns section pause_var = asr['pause_patterns']['pause_variability'] if pause_var < 0.3: pause_color = '#f59e0b' pause_label = 'Regular pauses' pause_desc = 'Suggests reading at punctuation marks' elif pause_var > 0.6: pause_color = '#10b981' pause_label = 'Irregular pauses' pause_desc = 'Natural thinking breaks indicate spontaneous speech' else: pause_color = '#6b7280' pause_label = 'Moderate variability' pause_desc = 'Mixed pattern' output += f"""

Pause Patterns

{asr['pause_patterns']['num_pauses']}
Total Pauses
{asr['pause_patterns']['avg_pause']:.2f}
Avg Duration
{asr['pause_patterns']['max_pause']:.2f}
Longest Pause
{pause_var:.2f}
Variability
{pause_label}
{pause_desc}
""" return output # build the AI detection display HTML def build_ai_detection_display(text_auth): is_ai = text_auth['ai_detection']['ai_generated'] ai_prob = text_auth['ai_detection']['confidence'] human_prob = 1 - ai_prob if is_ai: main_color = '#ef4444' bg_color = '#fef2f2' label = 'AI-GENERATED LIKELY' desc = 'The text shows patterns consistent with AI-generated content' else: main_color = '#10b981' bg_color = '#ecfdf5' label = 'HUMAN-WRITTEN LIKELY' desc = 'The text shows patterns consistent with human-written content' output = f"""

{label}

{desc}

Confidence Analysis

AI Generated {ai_prob*100:.0f}%
Human Written {human_prob*100:.0f}%
Note: AI detection is probabilistic and should be used as one factor among many in your evaluation.
""" return output # main function to analyze uploaded audio file def analyze_audio_file(audio_file): # check if pipeline is ready if not pipeline_ready: error_msg = pipeline_error if 'pipeline_error' in dir() else "Something went wrong" error_html = f"""

Pipeline not ready

{error_msg}

""" return (error_html, "", "", "", "") # check if audio file was provided if audio_file is None: placeholder_html = """

Please upload an audio file to begin analysis.

""" return (placeholder_html, "", "", "", "") # run analysis try: language_code = None results = pipeline.analyze_audio(audio_file, language=language_code) # extract results from each component audio_class = results['audio_classification'] asr = results['speech_recognition'] text_auth = results['text_authenticity'] final = results['final_assessment'] # color mapping for verdict verdict_color = { "AUTHENTIC": "#10b981", "LIKELY AUTHENTIC": "#3b82f6", "QUESTIONABLE": "#f59e0b", "LIKELY INAUTHENTIC": "#ef4444" } color = verdict_color.get(final['verdict'], '#6b7280') # build overall status display overall_status = f"""

{final['verdict']}

{final['composite_authenticity_score']*100:.0f}%
Authenticity Score
{final['risk_level'].upper()}
Risk Level
{results['processing_time']:.1f}s
Processing Time
{final['recommendation']}
""" # build tab outputs acoustic_output = build_acoustic_features_display(audio_class) transcription_output = build_transcription_display(asr) speech_patterns = build_speech_patterns_display(asr) ai_output = build_ai_detection_display(text_auth) return ( overall_status, acoustic_output, transcription_output, speech_patterns, ai_output, ) except Exception as e: error_html = f"""

Something went wrong

{str(e)}

More info
{traceback.format_exc()}
""" return (error_html, "", "", "", "") # create the gradio interface def create_interface(): # custom CSS for styling custom_css = """ @import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@300;400;500;600;700&display=swap'); .gradio-container { font-family: 'IBM Plex Sans', sans-serif !important; background: white !important; } button.primary, .primary { background: #2563eb !important; color: white !important; border: none !important; font-size: 16px !important; font-weight: 600 !important; padding: 12px 24px !important; border-radius: 8px !important; } """ with gr.Blocks(title="Authenticity Detection System") as demo: # header section gr.HTML(f"""

LEIDEN UNIVERSITY ยท LIACS

Audio Processing & Indexing Project

Detecting AI-Assisted Responses in Online Settings

Multi-Modal Analysis Acoustic + Linguistic
""") # main layout with gr.Row(): with gr.Column(scale=1): gr.HTML("""

Audio Input

Upload or record your audio file

""") # audio input component audio_input = gr.Audio( sources=["upload", "microphone"], type="filepath", label="Audio File", show_label=False ) # analyze button analyze_btn = gr.Button( "Analyze Audio", variant="primary", size="lg" ) # requirements info gr.HTML("""

Requirements

""") with gr.Column(scale=2): gr.HTML("""

Analysis Results

You'll see results here

""") # overall output overall_output = gr.HTML() # results tabs with gr.Tabs() as tabs: with gr.Tab("Acoustic Features"): acoustic_output = gr.HTML() with gr.Tab("Transcription"): transcription_output = gr.HTML() with gr.Tab("Speech Patterns"): speech_output = gr.HTML() with gr.Tab("AI Detection"): ai_output = gr.HTML() # example audio files gr.HTML("""

Try these examples:

""") examples_dir = os.path.join(os.path.dirname(__file__), "examples") gr.Examples( examples=[ [os.path.join(examples_dir, "read1.wav")], [os.path.join(examples_dir, "spontaneous1.wav")] ], inputs=[audio_input], outputs=[ overall_output, acoustic_output, transcription_output, speech_output, ai_output, ], fn=analyze_audio_file, label="", examples_per_page=2, cache_examples=False ) # loading animation function def show_loading(): loading_html = """

Analyzing...

""" loading_tab = """

Processing...

""" return loading_html, loading_tab, loading_tab, loading_tab, loading_tab # connect button to analysis function analyze_btn.click( fn=show_loading, inputs=None, outputs=[ overall_output, acoustic_output, transcription_output, speech_output, ai_output, ], queue=False ).then( fn=analyze_audio_file, inputs=[audio_input], outputs=[ overall_output, acoustic_output, transcription_output, speech_output, ai_output, ] ) # footer gr.HTML(""" """) return demo # run the app when script is executed if __name__ == "__main__": demo = create_interface() demo.launch( server_name="0.0.0.0", server_port=7860, share=False, show_error=True )