pgurazada1 commited on
Commit
592ad4f
1 Parent(s): e6af5e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -7,6 +7,9 @@ from openai import OpenAI
7
  from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
8
  from langchain_community.vectorstores import Chroma
9
 
 
 
 
10
 
11
  client = OpenAI(
12
  base_url="https://api.endpoints.anyscale.com/v1",
@@ -28,6 +31,19 @@ retriever = vectorstore_persisted.as_retriever(
28
  search_kwargs={'k': 5}
29
  )
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  qna_system_message = """
32
  You are an assistant to a financial services firm who answers user queries on annual reports.
33
  Users will ask questions delimited by triple backticks, that is, ```.
@@ -73,7 +89,21 @@ def predict(user_input):
73
 
74
  except Exception as e:
75
  prediction = e
76
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  return prediction
78
 
79
 
 
7
  from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
8
  from langchain_community.vectorstores import Chroma
9
 
10
+ from huggingface_hub import CommitScheduler
11
+ from pathlib import Path
12
+
13
 
14
  client = OpenAI(
15
  base_url="https://api.endpoints.anyscale.com/v1",
 
31
  search_kwargs={'k': 5}
32
  )
33
 
34
+ # Prepare the logging functionality
35
+
36
+ log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
37
+ log_folder = log_file.parent
38
+
39
+ scheduler = CommitScheduler(
40
+ repo_id="document-qna-chroma-anyscale-docs",
41
+ repo_type="dataset",
42
+ folder_path=log_folder,
43
+ path_in_repo="data",
44
+ every=2
45
+ )
46
+
47
  qna_system_message = """
48
  You are an assistant to a financial services firm who answers user queries on annual reports.
49
  Users will ask questions delimited by triple backticks, that is, ```.
 
89
 
90
  except Exception as e:
91
  prediction = e
92
+
93
+ # While the prediction is made, log both the inputs and outputs to a local log file
94
+ # While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
95
+ # access
96
+
97
+ with scheduler.lock:
98
+ with log_file.open("a") as f:
99
+ f.write(json.dumps(
100
+ {
101
+ 'user_input': user_input,
102
+ 'model_response': prediction
103
+ }
104
+ ))
105
+ f.write("\n")
106
+
107
  return prediction
108
 
109