Spaces:
Sleeping
Sleeping
Commit
·
0fd47f9
1
Parent(s):
d3ebdae
adds logdb
Browse files- requirements.txt +1 -0
- validators/database.py +37 -0
requirements.txt
CHANGED
|
@@ -2,3 +2,4 @@ git+https://github.com/macrocosm-os/prompting.git
|
|
| 2 |
aiohttp
|
| 3 |
deprecated
|
| 4 |
aiohttp_apispec>=2.2.3
|
|
|
|
|
|
| 2 |
aiohttp
|
| 3 |
deprecated
|
| 4 |
aiohttp_apispec>=2.2.3
|
| 5 |
+
aiofiles
|
validators/database.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import aiofiles
|
| 4 |
+
import bittensor as bt
|
| 5 |
+
from .streamer import ProcessedStreamResponse
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class LogDatabase:
|
| 9 |
+
def __init__(self, log_database_path: str):
|
| 10 |
+
self.log_database_path = log_database_path
|
| 11 |
+
self.ensure_db_exists(log_database_path)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def ensure_db_exists(self, file_path):
|
| 15 |
+
if not os.path.exists(file_path):
|
| 16 |
+
# Create an empty JSONL file
|
| 17 |
+
with open(file_path, 'w') as file:
|
| 18 |
+
pass
|
| 19 |
+
# TODO: change log to debug
|
| 20 |
+
bt.logging.info(f"File '{file_path}' created.")
|
| 21 |
+
else:
|
| 22 |
+
bt.logging.info(f"File '{file_path}' already exists.")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
async def add_streams_to_db(self, stream_responses: ProcessedStreamResponse):
|
| 26 |
+
bt.logging.info(f"Writing streams to the database...")
|
| 27 |
+
try:
|
| 28 |
+
stream_responses_dict = [dict(stream_response) for stream_response in stream_responses]
|
| 29 |
+
await self.append_dicts_to_file(self.log_database_path, stream_responses_dict)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
bt.logging.error(f"Error while adding streams to the database: {e}")
|
| 32 |
+
raise e
|
| 33 |
+
|
| 34 |
+
async def append_dicts_to_file(self, file_path, dictionaries):
|
| 35 |
+
async with aiofiles.open(file_path, mode='a') as file:
|
| 36 |
+
for dictionary in dictionaries:
|
| 37 |
+
await file.write(json.dumps(dictionary) + '\n')
|