Kristofy commited on
Commit
a340368
1 Parent(s): d693380

Added logging with the logging package

Browse files
Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -15,6 +15,9 @@ from sentence_transformers import SentenceTransformer
15
  from peft import PeftModel
16
  from bs4 import BeautifulSoup
17
  import requests
 
 
 
18
 
19
  headers = {
20
  "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit 537.36 (KHTML, like Gecko) Chrome",
@@ -24,7 +27,7 @@ headers = {
24
 
25
 
26
  def google_search(text):
27
- print(f"Google search on: {text}")
28
  try:
29
  site = requests.get(f"https://www.google.com/search?hl=en&q={text}", headers=headers)
30
  main = (
@@ -40,10 +43,10 @@ def google_search(text):
40
 
41
  ans = " \n".join(res)
42
  except Exception as ex:
43
- print(f"Error: {ex}")
44
  ans = ""
45
 
46
- print(f"The result of the google search is: {ans}")
47
 
48
  return ans
49
 
@@ -112,13 +115,13 @@ def get_results_from_pinecone(query, top_k=3, re_rank=True, verbose=True):
112
  return []
113
 
114
  if verbose:
115
- print("Query:", query)
116
 
117
  final_results = []
118
 
119
  if re_rank:
120
  if verbose:
121
- print("Document ID (Hash)\t\tRetrieval Score\tCE Score\tText")
122
 
123
  sentence_combinations = [
124
  [query, result_from_pinecone["metadata"]["text"]] for result_from_pinecone in results_from_pinecone
@@ -135,17 +138,17 @@ def get_results_from_pinecone(query, top_k=3, re_rank=True, verbose=True):
135
  result_from_pinecone = results_from_pinecone[idx]
136
  final_results.append(result_from_pinecone)
137
  if verbose:
138
- print(
139
  f"{result_from_pinecone['id']}\t{result_from_pinecone['score']:.2f}\t{similarity_scores[idx]:.2f}\t{result_from_pinecone['metadata']['text'][:50]}"
140
  )
141
  return final_results
142
 
143
  if verbose:
144
- print("Document ID (Hash)\t\tRetrieval Score\tText")
145
  for result_from_pinecone in results_from_pinecone:
146
  final_results.append(result_from_pinecone)
147
  if verbose:
148
- print(
149
  f"{result_from_pinecone['id']}\t{result_from_pinecone['score']:.2f}\t{result_from_pinecone['metadata']['text'][:50]}"
150
  )
151
 
@@ -268,13 +271,13 @@ def text_to_text_generation(verbose, prompt):
268
  match response_num:
269
  case 0:
270
  prompt = f"[INST] {prompt}\n Lets think step by step. [/INST] {start_template}"
271
- print('Kubectl command prompt:')
272
- print(prompt)
273
  case 1:
274
  if retriever == "semantic_search":
275
  question = prompt
276
- print('Semantic search prompt:')
277
- print(
278
  (
279
  f"You are a helpful kubernetes professional. [INST] Use the following documentation, if it is relevant to answer the question below. [/INST]\nDocumentation: [RETRIEVED_RESULTS_FROM_BOOK] [INST] Answer the following question: {question} [/INST]\nAnswer: \n")
280
 
@@ -295,8 +298,8 @@ def text_to_text_generation(verbose, prompt):
295
  question = prompt
296
  prompt = f"You are a helpful kubernetes professional. [INST] Use the following documentation, if it is relevant to answer the question below. [/INST]\nDocumentation: {retrieved_results} </s>\n<s> [INST] Answer the following question: {prompt} [/INST]\nAnswer: "
297
 
298
- print('Google search prompt:')
299
- print(
300
  (
301
  f"You are a helpful kubernetes professional. [INST] Use the following documentation, if it is relevant to answer the question below. [/INST]\nDocumentation: [RETRIEVED_RESULTS_FROM_GOOGLE] [INST] Answer the following question: {question} [/INST]\nAnswer:\n\n"
302
  )
@@ -312,13 +315,13 @@ def text_to_text_generation(verbose, prompt):
312
  )
313
  else:
314
  prompt = f"[INST] Answer the following question: {prompt} [/INST]\nAnswer: "
315
- print('No retriever question prompt:')
316
- print(prompt)
317
 
318
  case _:
319
  prompt = f"[INST] {prompt} [/INST]"
320
- print('Other question prompt:')
321
- print(prompt)
322
 
323
  return prompt, md
324
 
@@ -350,8 +353,8 @@ def text_to_text_generation(verbose, prompt):
350
 
351
  modes = ["Kubectl command", "Kubernetes related", "Other"]
352
 
353
- print(f'{" Query Start ":-^40}')
354
- print("Classified as: " + modes[response_num])
355
 
356
  modes[response_num] = f"**{modes[response_num]}**"
357
  modes = " / ".join(modes)
@@ -419,16 +422,16 @@ def text_to_text_generation(verbose, prompt):
419
  res_prompt, res_semantic_search_prompt, res_google_search_prompt
420
  )
421
 
422
- print("SEMANTIC BEFORE CLEANUP: ", gen_semantic_search)
423
- print("GOOGLE BEFORE CLEANUP: ", gen_google_search)
424
 
425
 
426
  res_prompt, res_normal = cleanup(*gen_normal)
427
  res_semantic_search_prompt, res_semantic_search = cleanup(*gen_semantic_search)
428
  res_google_search_prompt, res_google_search = cleanup(*gen_google_search)
429
 
430
- print("SEMANTIC AFTER CLEANUP: ", res_semantic_search)
431
- print("GOOGLE AFTER CLEANUP: ", res_google_search)
432
 
433
  if verbose:
434
  return (
 
15
  from peft import PeftModel
16
  from bs4 import BeautifulSoup
17
  import requests
18
+ import logging
19
+
20
+ logging.basicConfig(format='[%(asctime)s] %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.INFO)
21
 
22
  headers = {
23
  "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit 537.36 (KHTML, like Gecko) Chrome",
 
27
 
28
 
29
  def google_search(text):
30
+ logging.info(f"Google search on: {text}")
31
  try:
32
  site = requests.get(f"https://www.google.com/search?hl=en&q={text}", headers=headers)
33
  main = (
 
43
 
44
  ans = " \n".join(res)
45
  except Exception as ex:
46
+ logging.error(f"Error: {ex}")
47
  ans = ""
48
 
49
+ logging.info(f"The result of the google search is: {ans}")
50
 
51
  return ans
52
 
 
115
  return []
116
 
117
  if verbose:
118
+ logging.info("Query:", query)
119
 
120
  final_results = []
121
 
122
  if re_rank:
123
  if verbose:
124
+ logging.info("Document ID (Hash)\t\tRetrieval Score\tCE Score\tText")
125
 
126
  sentence_combinations = [
127
  [query, result_from_pinecone["metadata"]["text"]] for result_from_pinecone in results_from_pinecone
 
138
  result_from_pinecone = results_from_pinecone[idx]
139
  final_results.append(result_from_pinecone)
140
  if verbose:
141
+ logging.info(
142
  f"{result_from_pinecone['id']}\t{result_from_pinecone['score']:.2f}\t{similarity_scores[idx]:.2f}\t{result_from_pinecone['metadata']['text'][:50]}"
143
  )
144
  return final_results
145
 
146
  if verbose:
147
+ logging.info("Document ID (Hash)\t\tRetrieval Score\tText")
148
  for result_from_pinecone in results_from_pinecone:
149
  final_results.append(result_from_pinecone)
150
  if verbose:
151
+ logging.info(
152
  f"{result_from_pinecone['id']}\t{result_from_pinecone['score']:.2f}\t{result_from_pinecone['metadata']['text'][:50]}"
153
  )
154
 
 
271
  match response_num:
272
  case 0:
273
  prompt = f"[INST] {prompt}\n Lets think step by step. [/INST] {start_template}"
274
+ logging.info('Kubectl command prompt:')
275
+ logging.info(prompt)
276
  case 1:
277
  if retriever == "semantic_search":
278
  question = prompt
279
+ logging.info('Semantic search prompt:')
280
+ logging.info(
281
  (
282
  f"You are a helpful kubernetes professional. [INST] Use the following documentation, if it is relevant to answer the question below. [/INST]\nDocumentation: [RETRIEVED_RESULTS_FROM_BOOK] [INST] Answer the following question: {question} [/INST]\nAnswer: \n")
283
 
 
298
  question = prompt
299
  prompt = f"You are a helpful kubernetes professional. [INST] Use the following documentation, if it is relevant to answer the question below. [/INST]\nDocumentation: {retrieved_results} </s>\n<s> [INST] Answer the following question: {prompt} [/INST]\nAnswer: "
300
 
301
+ logging.info('Google search prompt:')
302
+ logging.info(
303
  (
304
  f"You are a helpful kubernetes professional. [INST] Use the following documentation, if it is relevant to answer the question below. [/INST]\nDocumentation: [RETRIEVED_RESULTS_FROM_GOOGLE] [INST] Answer the following question: {question} [/INST]\nAnswer:\n\n"
305
  )
 
315
  )
316
  else:
317
  prompt = f"[INST] Answer the following question: {prompt} [/INST]\nAnswer: "
318
+ logging.info('No retriever question prompt:')
319
+ logging.info(prompt)
320
 
321
  case _:
322
  prompt = f"[INST] {prompt} [/INST]"
323
+ logging.info('Other question prompt:')
324
+ logging.info(prompt)
325
 
326
  return prompt, md
327
 
 
353
 
354
  modes = ["Kubectl command", "Kubernetes related", "Other"]
355
 
356
+ logging.info(f'{" Query Start ":-^40}')
357
+ logging.info("Classified as: " + modes[response_num])
358
 
359
  modes[response_num] = f"**{modes[response_num]}**"
360
  modes = " / ".join(modes)
 
422
  res_prompt, res_semantic_search_prompt, res_google_search_prompt
423
  )
424
 
425
+ logging.info("SEMANTIC BEFORE CLEANUP: ", gen_semantic_search)
426
+ logging.info("GOOGLE BEFORE CLEANUP: ", gen_google_search)
427
 
428
 
429
  res_prompt, res_normal = cleanup(*gen_normal)
430
  res_semantic_search_prompt, res_semantic_search = cleanup(*gen_semantic_search)
431
  res_google_search_prompt, res_google_search = cleanup(*gen_google_search)
432
 
433
+ logging.info("SEMANTIC AFTER CLEANUP: ", res_semantic_search)
434
+ logging.info("GOOGLE AFTER CLEANUP: ", res_google_search)
435
 
436
  if verbose:
437
  return (