Badrivishal commited on
Commit
1cc01b5
·
1 Parent(s): f83ed2e

Added Metrics

Browse files
Files changed (2) hide show
  1. app.py +9 -0
  2. prometheus_helper.py +33 -2
app.py CHANGED
@@ -20,6 +20,7 @@ prometheus_helper = PrometheusHelper()
20
 
21
  # --- Build / load index ---
22
  def build_index():
 
23
  print("Building FAISS index...")
24
  ds = load_dataset("olivierdehaene/xkcd", split="train")
25
  model = SentenceTransformer("all-MiniLM-L6-v2")
@@ -49,6 +50,7 @@ def build_index():
49
  with open(META_FILE, "wb") as f:
50
  pickle.dump(meta, f)
51
 
 
52
  return index, meta
53
 
54
  def get_index():
@@ -80,10 +82,14 @@ def respond(
80
 
81
  prometheus_helper.start_request_timer()
82
  # Embed the query and search FAISS
 
83
  query_vec = embedder.encode([message], convert_to_numpy=True)
84
  D, I = index.search(query_vec, 5)
85
  candidates = [meta[int(i)] for i in I[0]]
86
 
 
 
 
87
  context = "\n".join(
88
  f"[{c['id']}] {c['title']}\nTranscript: {c['transcript']}\nExplanation: {c['explanation']}"
89
  for c in candidates
@@ -111,6 +117,8 @@ EXPLANATION
111
  temperature=0.0, # TODO
112
  )
113
 
 
 
114
  # Be tolerant to slight schema differences
115
  try:
116
  choice = resp.choices[0]
@@ -134,6 +142,7 @@ EXPLANATION
134
  img_url = json.load(url)["img"]
135
  print(f'Got image url: {img_url}')
136
 
 
137
  return [out_text, gr.Image(value=img_url)]
138
  except ValueError:
139
  print("Couldn't parse xkcd ID or get image! That should not happen.")
 
20
 
21
  # --- Build / load index ---
22
  def build_index():
23
+ prometheus_helper.start_index_build_timer()
24
  print("Building FAISS index...")
25
  ds = load_dataset("olivierdehaene/xkcd", split="train")
26
  model = SentenceTransformer("all-MiniLM-L6-v2")
 
50
  with open(META_FILE, "wb") as f:
51
  pickle.dump(meta, f)
52
 
53
+ prometheus_helper.stop_index_build_timer()
54
  return index, meta
55
 
56
  def get_index():
 
82
 
83
  prometheus_helper.start_request_timer()
84
  # Embed the query and search FAISS
85
+ prometheus_helper.start_faiss_index_search_timer()
86
  query_vec = embedder.encode([message], convert_to_numpy=True)
87
  D, I = index.search(query_vec, 5)
88
  candidates = [meta[int(i)] for i in I[0]]
89
 
90
+ prometheus_helper.stop_faiss_index_search_timer()
91
+ prometheus_helper.start_chat_model_call_timer()
92
+
93
  context = "\n".join(
94
  f"[{c['id']}] {c['title']}\nTranscript: {c['transcript']}\nExplanation: {c['explanation']}"
95
  for c in candidates
 
117
  temperature=0.0, # TODO
118
  )
119
 
120
+ prometheus_helper.stop_chat_model_call_timer()
121
+
122
  # Be tolerant to slight schema differences
123
  try:
124
  choice = resp.choices[0]
 
142
  img_url = json.load(url)["img"]
143
  print(f'Got image url: {img_url}')
144
 
145
+ prometheus_helper.record_frequency(int(id))
146
  return [out_text, gr.Image(value=img_url)]
147
  except ValueError:
148
  print("Couldn't parse xkcd ID or get image! That should not happen.")
prometheus_helper.py CHANGED
@@ -1,4 +1,4 @@
1
- from prometheus_client import start_http_server, Counter, Summary
2
  import time
3
 
4
  class PrometheusHelper:
@@ -7,6 +7,27 @@ class PrometheusHelper:
7
  self.successful_requests_counter = Counter('app_successful_requests_total', 'Total number of successful requests')
8
  self.failed_requests_counter = Counter('app_failed_requests_total', 'Total number of failed requests')
9
  self.request_duration_summary = Summary('app_request_duration_seconds', 'Time spent processing request')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def record_request(self, success: bool):
12
  self.request_counter.inc()
@@ -15,6 +36,12 @@ class PrometheusHelper:
15
  else:
16
  self.failed_requests_counter.inc()
17
 
 
 
 
 
 
 
18
  def setup_prometheus(self):
19
  start_http_server(8000)
20
 
@@ -22,4 +49,8 @@ class PrometheusHelper:
22
  self.start_time = time.time()
23
 
24
  def stop_request_timer(self):
25
- self.request_duration_summary.observe(time.time() - self.start_time)
 
 
 
 
 
1
+ from prometheus_client import start_http_server, Counter, Summary, Histogram
2
  import time
3
 
4
  class PrometheusHelper:
 
7
  self.successful_requests_counter = Counter('app_successful_requests_total', 'Total number of successful requests')
8
  self.failed_requests_counter = Counter('app_failed_requests_total', 'Total number of failed requests')
9
  self.request_duration_summary = Summary('app_request_duration_seconds', 'Time spent processing request')
10
+ self.comic_frequency = Counter('comic_frequency', 'Frequency of comics being selected', ['comic_id'])
11
+ self.index_build_start_time = None
12
+ self.index_build_duration = Summary('index_build_duration_seconds', 'Time spent building the index')
13
+ self.faiss_index_search_start_time = None
14
+ self.faiss_index_search_duration = Summary('faiss_index_search_duration_seconds', 'Time spent searching the index')
15
+ self.chat_model_call_start_time = None
16
+ self.chat_model_call_duration = Summary('chat_model_call_duration_seconds', 'Time spent calling the chat model')
17
+ # self.frequency_histogram = Histogram('comic_frequency', 'Frequency of comics being selected')
18
+
19
+
20
+ def start_faiss_index_search_timer(self):
21
+ self.faiss_index_search_start_time = time.time()
22
+
23
+ def stop_faiss_index_search_timer(self):
24
+ self.faiss_index_search_duration.observe(time.time() - self.faiss_index_search_start_time)
25
+
26
+ def start_chat_model_call_timer(self):
27
+ self.chat_model_call_start_time = time.time()
28
+
29
+ def stop_chat_model_call_timer(self):
30
+ self.chat_model_call_duration.observe(time.time() - self.chat_model_call_start_time)
31
 
32
  def record_request(self, success: bool):
33
  self.request_counter.inc()
 
36
  else:
37
  self.failed_requests_counter.inc()
38
 
39
+ def start_index_build_timer(self):
40
+ self.index_build_start_time = time.time()
41
+
42
+ def stop_index_build_timer(self):
43
+ self.index_build_duration.observe(time.time() - self.index_build_start_time)
44
+
45
  def setup_prometheus(self):
46
  start_http_server(8000)
47
 
 
49
  self.start_time = time.time()
50
 
51
  def stop_request_timer(self):
52
+ self.request_duration_summary.observe(time.time() - self.start_time)
53
+
54
+ def record_frequency(self, comic_id: int):
55
+ self.comic_frequency.labels(comic_id=comic_id).inc()
56
+