Eric Botti
commited on
Commit
·
65b412a
1
Parent(s):
2f9cbc8
logging to mongodb
Browse files- requirements.txt +0 -0
- src/app.py +2 -1
- src/data_collection.py +20 -10
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
src/app.py
CHANGED
|
@@ -84,7 +84,8 @@ text-align: center;
|
|
| 84 |
}
|
| 85 |
</style>
|
| 86 |
<div class="footer">
|
| 87 |
-
<p>Created by <a href="https://huggingface.co/ericbotti" target="_blank">Eric Botti</a></p>
|
|
|
|
| 88 |
</div>
|
| 89 |
"""
|
| 90 |
st.markdown(footer, unsafe_allow_html=True)
|
|
|
|
| 84 |
}
|
| 85 |
</style>
|
| 86 |
<div class="footer">
|
| 87 |
+
<p style="margin: 0;">Created by <a href="https://huggingface.co/ericbotti" target="_blank">Eric Botti</a></p>
|
| 88 |
+
<small>Your responses may be collected for research purposes</small>
|
| 89 |
</div>
|
| 90 |
"""
|
| 91 |
st.markdown(footer, unsafe_allow_html=True)
|
src/data_collection.py
CHANGED
|
@@ -7,34 +7,44 @@ import pydantic
|
|
| 7 |
import player
|
| 8 |
import message
|
| 9 |
|
|
|
|
| 10 |
from pydantic import BaseModel
|
| 11 |
|
| 12 |
-
COLLECT_DATA = os.environ.get("COLLECT_DATA", "true").upper() == "TRUE"
|
| 13 |
|
| 14 |
-
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
|
| 19 |
|
| 20 |
def save(log_object: Model):
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
with open(log_file, "a+") as f:
|
| 25 |
f.write(log_object.model_dump_json() + "\n")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
def
|
| 29 |
from game import Game
|
| 30 |
|
| 31 |
if isinstance(log_object, message.AgentMessage):
|
| 32 |
-
|
| 33 |
elif isinstance(log_object, player.Player):
|
| 34 |
-
|
| 35 |
elif isinstance(log_object, Game):
|
| 36 |
-
|
| 37 |
else:
|
| 38 |
raise ValueError(f"Unknown log object type: {type(log_object)}")
|
| 39 |
|
| 40 |
-
return
|
|
|
|
| 7 |
import player
|
| 8 |
import message
|
| 9 |
|
| 10 |
+
from pymongo import MongoClient
|
| 11 |
from pydantic import BaseModel
|
| 12 |
|
|
|
|
| 13 |
|
| 14 |
+
DATA_COLLECTION_MODE = os.environ.get("DATA_COLLECTION_MODE", "JSONL")
|
| 15 |
|
| 16 |
+
MONGODB_CONNECTION_STRING = os.environ.get("MONGODB_CONNECTION_STRING")
|
| 17 |
+
DB_NAME = os.environ.get("MONGODB_NAME")
|
| 18 |
|
| 19 |
+
Model = NewType("Model", BaseModel)
|
| 20 |
|
| 21 |
|
| 22 |
def save(log_object: Model):
|
| 23 |
+
collection = get_collection(log_object)
|
| 24 |
+
|
| 25 |
+
if DATA_COLLECTION_MODE.upper() == "JSONL":
|
| 26 |
+
data_dir = pathlib.Path(__file__).parent.parent / "data"
|
| 27 |
+
log_file = os.path.join(data_dir, f"{collection}.jsonl")
|
| 28 |
|
| 29 |
with open(log_file, "a+") as f:
|
| 30 |
f.write(log_object.model_dump_json() + "\n")
|
| 31 |
|
| 32 |
+
if DATA_COLLECTION_MODE.upper() == "MONGODB":
|
| 33 |
+
client = MongoClient(MONGODB_CONNECTION_STRING)
|
| 34 |
+
db = client[DB_NAME]
|
| 35 |
+
db[collection].insert_one(log_object.model_dump())
|
| 36 |
+
|
| 37 |
|
| 38 |
+
def get_collection(log_object: Model) -> str:
|
| 39 |
from game import Game
|
| 40 |
|
| 41 |
if isinstance(log_object, message.AgentMessage):
|
| 42 |
+
collection = "messages"
|
| 43 |
elif isinstance(log_object, player.Player):
|
| 44 |
+
collection = "players"
|
| 45 |
elif isinstance(log_object, Game):
|
| 46 |
+
collection = "games"
|
| 47 |
else:
|
| 48 |
raise ValueError(f"Unknown log object type: {type(log_object)}")
|
| 49 |
|
| 50 |
+
return collection
|