ksee commited on
Commit
4a74616
1 Parent(s): 9c7155b

Upload db.py

Browse files
Files changed (1) hide show
  1. db.py +64 -0
db.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import datetime
3
+ import firebase_admin
4
+ from firebase_admin import credentials, firestore
5
+
6
+
7
+ class DB:
8
+ def __init__(self, credential_path):
9
+ if not firebase_admin._apps:
10
+ cd = credentials.Certificate(credential_path)
11
+ firebase_admin.initialize_app(cd)
12
+ self.db = firestore.client()
13
+
14
+ def get_next_surveykey(self) -> str:
15
+ collection_ref = self.db.collection('surveykeys')
16
+ docs = collection_ref.stream()
17
+ max_id = max([int(doc.id) for doc in docs], default=0)
18
+ next_id = max_id + 1
19
+ return f"{next_id:04d}"
20
+
21
+ def add_surveykey_document(self) -> str:
22
+ next_document_id = self.get_next_surveykey()
23
+ new_doc_ref = self.db.collection('surveykeys').document(next_document_id)
24
+ new_doc_ref.set({
25
+ 'created': datetime.datetime.now()
26
+ })
27
+ return next_document_id
28
+
29
+ def get_document_ids(self, collection_name):
30
+ collection_ref = self.db.collection(collection_name)
31
+ docs = collection_ref.stream()
32
+ doc_ids = [doc.id for doc in docs]
33
+ return doc_ids
34
+
35
+ def add_survey_responses(self, surveykey: str, model_name: str,
36
+ q1_answer: bool, q1_scam: bool, q1_input: str, q1_reply: str,
37
+ q2_answer: bool, q2_scam: bool, q2_input: str, q2_reply: str,
38
+ q3_answer: bool, q3_scam: bool, q3_input: str, q3_reply: str,
39
+ final_eval: int):
40
+ new_doc_ref = self.db.collection('responses').document(surveykey)
41
+ new_doc_ref.set({
42
+ 'submission_time': datetime.datetime.now(),
43
+ 'model': model_name,
44
+ 'q1_answer': q1_answer,
45
+ 'q1_scam': q1_scam,
46
+ 'q1_input': q1_input,
47
+ 'q1_reply': q1_reply,
48
+ 'q2_answer': q2_answer,
49
+ 'q2_scam': q2_scam,
50
+ 'q2_input': q2_input,
51
+ 'q2_reply': q2_reply,
52
+ 'q3_answer': q3_answer,
53
+ 'q3_scam': q3_scam,
54
+ 'q3_input': q3_input,
55
+ 'q3_reply': q3_reply,
56
+ 'final_eval_score': final_eval
57
+ })
58
+
59
+
60
+
61
+ # cred_path = "scamgpt-j-evaluation-firebase-adminsdk-xt1xg-71792b5117.json"
62
+ # db = DB(cred_path)
63
+ # db.add_survey_responses('0002', 'gpt2', True, 'test text test text', 'test response', False, 'test 2', 'test response 2', True, 'test 3', 'test response 3', 4)
64
+ # print(db.get_next_surveykey())