import gradio as gr import tempfile import os from sip_rtp_pcap_analyzer_with_visualization import SipRtpPcapAnalyzer def analyze_pcap(pcap_file): # Create a temp output directory output_dir = tempfile.mkdtemp() # Save uploaded file to a temp file temp_pcap_path = pcap_file #os.path.join(output_dir, pcap_file.name) #with open(temp_pcap_path, "wb") as f: # f.write(pcap_file.read()) # Run analysis with visualization analyzer = SipRtpPcapAnalyzer( pcap_file=temp_pcap_path, output_dir=output_dir, visualize=True ) analyzer.analyze() # Get the summary text output from io import StringIO import sys old_stdout = sys.stdout sys.stdout = mystdout = StringIO() analyzer.print_results() sys.stdout = old_stdout result_text = mystdout.getvalue() # Prepare outputs html_report_path = os.path.join(output_dir, "sip_rtp_report.html") rtp_graphs = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if f.startswith("rtp_visualization") and f.endswith(".png")] sequence_diagrams = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if f.startswith("sip_sequence") and f.endswith(".png")] dashboard = os.path.join(output_dir, "call_quality_dashboard.png") return result_text, html_report_path, sequence_diagrams, rtp_graphs, dashboard with gr.Blocks(title="SIP & RTP PCAP Analyzer") as demo: gr.Markdown(""" # SIP and RTP Analyzer Upload a PCAP file to analyze SIP calls and RTP streams. Visualizations and call quality metrics will be shown. """) with gr.Row(): pcap_input = gr.File(label="Upload PCAP/PCAPNG File") run_btn = gr.Button("Analyze") result_text = gr.Textbox(label="Analysis Summary", lines=15) html_report = gr.File(label="Download HTML Report") sequence_images = gr.Gallery(label="SIP Sequence Diagrams") rtp_images = gr.Gallery(label="RTP Stream Visualizations") dashboard_image = gr.Image(label="Call Quality Dashboard") run_btn.click( fn=analyze_pcap, inputs=pcap_input, outputs=[result_text, html_report, sequence_images, rtp_images, dashboard_image] ) if __name__ == "__main__": demo.launch()