Spaces:
Sleeping
Sleeping
| import matplotlib.pyplot as plt | |
| import matplotlib.gridspec as gridspec | |
| import librosa.display | |
| import numpy as np | |
| def create_report(audio_data, output_path): | |
| """ | |
| Create a complete forensic PNG report. | |
| Synthetic detection is informational only. | |
| """ | |
| plt.style.use("default") | |
| fig = plt.figure(figsize=(22, 16)) | |
| fig.patch.set_facecolor("white") | |
| fig.suptitle( | |
| f"AUDIO FORENSIC ANALYSIS REPORT\n{audio_data['filename']}", | |
| fontsize=20, | |
| fontweight="bold", | |
| y=0.97 | |
| ) | |
| gs = gridspec.GridSpec( | |
| 5, 4, | |
| figure=fig, | |
| hspace=0.45, | |
| wspace=0.4, | |
| height_ratios=[1.5, 1, 0.8, 0.9, 0.7], | |
| left=0.05, | |
| right=0.95, | |
| top=0.92, | |
| bottom=0.05 | |
| ) | |
| # ============================================================ | |
| # 1. SPECTROGRAM PANEL | |
| # ============================================================ | |
| ax_spec = fig.add_subplot(gs[0, :]) | |
| S_db = audio_data["spectral"]["S_db"] | |
| sr = audio_data["info"]["samplerate"] | |
| hop = audio_data["spectral"]["hop_length"] | |
| img = librosa.display.specshow( | |
| S_db, | |
| sr=sr, | |
| hop_length=hop, | |
| y_axis="hz", | |
| x_axis="time", | |
| cmap="viridis", | |
| ax=ax_spec, | |
| vmin=-80, | |
| vmax=0 | |
| ) | |
| ax_spec.set_title("Spectrogram", fontsize=14, fontweight="bold", pad=10) | |
| ax_spec.grid(True, alpha=0.3, linestyle="--") | |
| cbar = plt.colorbar(img, ax=ax_spec, pad=0.01) | |
| cbar.set_label("Magnitude (dB)", fontsize=10, fontweight="bold") | |
| # ============================================================ | |
| # 2. FILE INFORMATION PANEL | |
| # ============================================================ | |
| ax_info = fig.add_subplot(gs[1, 0:2]) | |
| ax_info.axis("off") | |
| info = audio_data["info"] | |
| t = audio_data["time_stats"] | |
| lines = [ | |
| "FILE INFORMATION", | |
| "β" * 50, | |
| f"Sample Rate: {info['samplerate']:,} Hz", | |
| f"Channels: {info['channels']}", | |
| f"Duration: {info['duration']:.2f} sec", | |
| f"Format: {info['format']} ({info['subtype']})", | |
| f"Frames: {info['frames']:,}", | |
| "", | |
| "TIME ANALYSIS", | |
| "β" * 50, | |
| f"Peak: {t['peak_db']:.2f} dBFS ({t['peak']:.6f})", | |
| f"RMS: {t['rms_db']:.2f} dBFS ({t['rms']:.6f})", | |
| f"Crest Factor: {t['crest_factor_db']:.2f} dB", | |
| f"Noise Floor: {t['noise_floor']:.6f}", | |
| f"Est. SNR: {t['snr_db']:.1f} dB", | |
| f"Zero Cross Rate: {t['zero_crossing_rate']:.4f}", | |
| ] | |
| if audio_data.get("lufs") is not None: | |
| lines += [ | |
| "", | |
| "LOUDNESS", | |
| "β" * 50, | |
| f"Integrated LUFS: {audio_data['lufs']:.2f}" | |
| ] | |
| ax_info.text( | |
| 0.05, 0.95, | |
| "\n".join(lines), | |
| fontsize=10.8, | |
| va="top", | |
| family="monospace", | |
| bbox=dict( | |
| boxstyle="round,pad=1", | |
| facecolor="#E8F4F8", | |
| edgecolor="#0077BE", | |
| linewidth=2 | |
| ) | |
| ) | |
| # ============================================================ | |
| # 3. SPECTRAL STATS PANEL | |
| # ============================================================ | |
| ax_specstats = fig.add_subplot(gs[1, 2:4]) | |
| ax_specstats.axis("off") | |
| spec = audio_data["spectral"] | |
| e = spec["energy_distribution"] | |
| text = [ | |
| "SPECTRAL ANALYSIS", | |
| "β" * 50, | |
| f"Centroid: {spec['spectral_centroid']:.1f} Hz", | |
| f"Bandwidth: {spec['spectral_bandwidth']:.1f} Hz", | |
| f"Flatness: {spec['spectral_flatness']:.4f}", | |
| f"Rolloff Mean: {spec['spectral_rolloff']:.1f} Hz", | |
| "", | |
| "ROLLOFF POINTS", | |
| "β" * 50, | |
| f"85% Energy: {spec['rolloff_85pct']:.1f} Hz", | |
| f"95% Energy: {spec['rolloff_95pct']:.1f} Hz", | |
| f"Highest -60 dB: {spec['highest_freq_minus60db']:.1f} Hz", | |
| "", | |
| "ENERGY DISTRIBUTION", | |
| "β" * 50, | |
| f"< 100 Hz: {e['below_100hz']:.2f}%", | |
| f"100β500 Hz: {e['100_500hz']:.2f}%", | |
| f"500β2k Hz: {e['500_2khz']:.2f}%", | |
| f"2kβ8k Hz: {e['2k_8khz']:.2f}%", | |
| f"8kβ12k Hz: {e['8k_12khz']:.2f}%", | |
| f"12kβ16k Hz: {e['12k_16khz']:.2f}%", | |
| f"> 16k Hz: {e['above_16khz']:.2f}%", | |
| ] | |
| ax_specstats.text( | |
| 0.05, 0.95, | |
| "\n".join(text), | |
| fontsize=10.8, | |
| va="top", | |
| family="monospace", | |
| bbox=dict( | |
| boxstyle="round,pad=1", | |
| facecolor="#FFF4E6", | |
| edgecolor="#FF8C00", | |
| linewidth=2 | |
| ) | |
| ) | |
| # ============================================================ | |
| # 4. ENERGY BAR CHART | |
| # ============================================================ | |
| ax_bar = fig.add_subplot(gs[2, :]) | |
| bands = [ | |
| "<100Hz", "100β500Hz", "500β2kHz", | |
| "2kβ8kHz", "8kβ12kHz", "12kβ16kHz", ">16kHz" | |
| ] | |
| vals = [ | |
| e["below_100hz"], e["100_500hz"], e["500_2khz"], | |
| e["2k_8khz"], e["8k_12khz"], e["12k_16khz"], e["above_16khz"] | |
| ] | |
| colors = ["#2C3E50", "#E74C3C", "#E67E22", | |
| "#F39C12", "#2ECC71", "#3498DB", "#9B59B6"] | |
| bars = ax_bar.bar(bands, vals, color=colors, edgecolor="black", alpha=0.85) | |
| ax_bar.set_ylabel("Energy (%)", fontsize=12, fontweight="bold") | |
| ax_bar.grid(axis="y", alpha=0.35, linestyle="--") | |
| for b, v in zip(bars, vals): | |
| ax_bar.text(b.get_x() + b.get_width()/2, v + 0.3, f"{v:.2f}%", ha="center", fontsize=10) | |
| # ============================================================ | |
| # 5. ISSUES PANEL | |
| # ============================================================ | |
| ax_issues = fig.add_subplot(gs[3, 0:3]) | |
| ax_issues.axis("off") | |
| issues = audio_data["issues"] | |
| issue_lines = ["DETECTED ISSUES", "β" * 80] | |
| if not issues: | |
| issue_lines.append("β No significant issues detected.") | |
| else: | |
| icons = { | |
| "CRITICAL": "π΄", | |
| "HIGH": "π ", | |
| "MEDIUM": "π‘", | |
| "LOW": "π’" | |
| } | |
| for issue, sev, desc in issues: | |
| issue_lines.append(f"{icons.get(sev,'βͺ')} [{sev}] {issue}") | |
| issue_lines.append(f" β {desc}") | |
| if spec["spectral_notches"]: | |
| issue_lines += [ | |
| "", | |
| f"π΅ Spectral Notches: {len(spec['spectral_notches'])}", | |
| ] | |
| for i, n in enumerate(spec["spectral_notches"][:5], 1): | |
| issue_lines.append(f" {i}. {n['freq']:.1f} Hz (Depth {n['depth_db']:.1f} dB)") | |
| ax_issues.text( | |
| 0.05, 0.95, | |
| "\n".join(issue_lines), | |
| fontsize=10.8, | |
| va="top", | |
| family="monospace", | |
| bbox=dict( | |
| boxstyle="round,pad=1.2", | |
| facecolor="#FFE6E6", | |
| edgecolor="#DC143C", | |
| linewidth=2 | |
| ) | |
| ) | |
| # ============================================================ | |
| # 6. QUALITY SCORE PANEL + SYNTHETIC BLOCK | |
| # ============================================================ | |
| ax_score = fig.add_subplot(gs[3, 3]) | |
| ax_score.axis("off") | |
| s = audio_data["score"] | |
| syn = audio_data["synthetic"] | |
| score_lines = [ | |
| "QUALITY ASSESSMENT", | |
| "β" * 28, | |
| f"SCORE: {s['score']}/100", | |
| f"GRADE: {s['grade']}", | |
| f"QUALITY: {s['quality']}", | |
| "", | |
| "RECOMMENDATION:", | |
| s["recommendation"], | |
| "", | |
| "CLEANLINESS SCORE:", | |
| f"{s['cleanliness_score']}/100", | |
| "", | |
| "PROCESSING SEVERITY:", | |
| f"{s['processing_severity']}", | |
| "", | |
| "ISSUE SUMMARY", | |
| "β" * 28, | |
| f"Critical: {s['critical']}", | |
| f"High: {s['high']}", | |
| f"Medium: {s['medium']}", | |
| f"Low: {s['low']}", | |
| ] | |
| score_lines += [ | |
| "", | |
| "βββββββββββββββββββββββ", | |
| " SYNTHETIC VOICE", | |
| "βββββββββββββββββββββββ", | |
| f"Probability : {syn['synthetic_probability']:.2f}", | |
| f"Label : {syn['synthetic_label']}", | |
| "βββββββββββββββββββββββ", | |
| "", | |
| f"Generated: {audio_data['timestamp']}" | |
| ] | |
| ax_score.text( | |
| 0.5, 0.5, | |
| "\n".join(score_lines), | |
| fontsize=11, | |
| ha="center", | |
| va="center", | |
| family="monospace", | |
| bbox=dict( | |
| boxstyle="round,pad=1.4", | |
| facecolor=s["color"], | |
| edgecolor="black", | |
| linewidth=3, | |
| alpha=0.70 | |
| ) | |
| ) | |
| # ============================================================ | |
| # SAVE REPORT | |
| # ============================================================ | |
| plt.savefig(output_path, dpi=300, bbox_inches="tight") | |
| plt.close() | |
| return output_path | |