fabioantonini commited on
Commit
9a11f29
·
verified ·
1 Parent(s): da8e00f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -7,13 +7,32 @@ import gradio as gr
7
  from parsers import parse_pcap
8
  from analysis import analyze_calls
9
  from call_flow import create_call_flow_diagram
10
- from llm_utils import create_local_pipeline, get_llm_opinion
11
 
12
- # 1) Initialize your local Hugging Face pipeline (model)
13
- # Adjust model_id to your chosen model on HF.
14
- MODEL_ID = "google/flan-t5-base" # "tiiuae/falcon-7b-instruct"
 
 
 
 
 
 
 
 
 
 
 
 
15
  generator = create_local_pipeline(MODEL_ID)
16
 
 
 
 
 
 
 
 
 
17
  def process_file(pcap_file):
18
  """
19
  This function is called when user clicks the 'Analyze File' button.
@@ -64,16 +83,16 @@ def ask_llm_opinion(calls_data, question):
64
  if not question.strip():
65
  return "Please enter a question."
66
 
67
- # You might want to create a summary of the calls or pass the entire calls_data.
68
- # For brevity, let's just embed a brief mention that calls_data exist:
69
- calls_context = "Below is a representation of the calls found in the PCAP:\n"
70
  for call_id, call_obj in calls_data.items():
71
  calls_context += f"- Call-ID: {call_id}, from_tag: {call_obj.from_tag}, to_tag: {call_obj.to_tag}\n"
72
 
 
73
  prompt = (
74
  f"{calls_context}\n"
75
- f"User's question: {question}\n"
76
- f"Please provide your expert VoIP analysis or advice."
77
  )
78
 
79
  # Query the local pipeline
@@ -87,7 +106,7 @@ def main():
87
  2) LLM Consultation
88
  """
89
  with gr.Blocks() as demo:
90
- gr.Markdown("# VoIP Analyzer\nUpload a PCAP/PCAPNG file for SIP/RTP analysis. Then consult an LLM for further insights.")
91
 
92
  # We keep the calls data in a Gradio State so we can pass it between tabs
93
  calls_state = gr.State({})
 
7
  from parsers import parse_pcap
8
  from analysis import analyze_calls
9
  from call_flow import create_call_flow_diagram
 
10
 
11
+ # We'll adapt llm_utils to load flan-t5-base locally
12
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
13
+
14
+ def create_local_pipeline(model_id="google/flan-t5-base"):
15
+ """
16
+ Create a local pipeline for Flan-T5, which is a seq2seq model.
17
+ This should run within ~16GB RAM for 'flan-t5-base'.
18
+ """
19
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
20
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_id, device_map="auto")
21
+ # Use text2text-generation for T5-based models
22
+ return pipeline("text2text-generation", model=model, tokenizer=tokenizer)
23
+
24
+ # Initialize model pipeline once
25
+ MODEL_ID = "google/flan-t5-base"
26
  generator = create_local_pipeline(MODEL_ID)
27
 
28
+ def get_llm_opinion(prompt, generator):
29
+ """
30
+ Generate text from the local Flan-T5 pipeline.
31
+ We use text2text-generation to handle the seq2seq nature of T5.
32
+ """
33
+ outputs = generator(prompt, max_length=256, do_sample=True, temperature=0.5)
34
+ return outputs[0]["generated_text"]
35
+
36
  def process_file(pcap_file):
37
  """
38
  This function is called when user clicks the 'Analyze File' button.
 
83
  if not question.strip():
84
  return "Please enter a question."
85
 
86
+ # Summarize the calls for context
87
+ calls_context = "Below is a brief description of the calls found in the PCAP:\n"
 
88
  for call_id, call_obj in calls_data.items():
89
  calls_context += f"- Call-ID: {call_id}, from_tag: {call_obj.from_tag}, to_tag: {call_obj.to_tag}\n"
90
 
91
+ # Build a prompt for T5
92
  prompt = (
93
  f"{calls_context}\n"
94
+ f"User's question: {question}\n\n"
95
+ "Please provide your expert VoIP analysis or advice."
96
  )
97
 
98
  # Query the local pipeline
 
106
  2) LLM Consultation
107
  """
108
  with gr.Blocks() as demo:
109
+ gr.Markdown("# VoIP Analyzer\nUpload a PCAP/PCAPNG file for SIP/RTP analysis. Then consult Flan-T5 for further insights.")
110
 
111
  # We keep the calls data in a Gradio State so we can pass it between tabs
112
  calls_state = gr.State({})