Spaces:
Runtime error
Runtime error
jiarongqiu commited on
Commit ·
b63d6c4
1
Parent(s): 017e7e6
add scheduler
Browse files- .gitignore +1 -0
- service/api.py +15 -0
- service/dataset.py +58 -0
- service/scheduler.py +27 -0
- service/vector_store.py +5 -0
.gitignore
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
*.ipynb
|
| 2 |
script/
|
|
|
|
| 3 |
|
| 4 |
# Byte-compiled / optimized / DLL files
|
| 5 |
__pycache__/
|
|
|
|
| 1 |
*.ipynb
|
| 2 |
script/
|
| 3 |
+
data/
|
| 4 |
|
| 5 |
# Byte-compiled / optimized / DLL files
|
| 6 |
__pycache__/
|
service/api.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
|
|
| 1 |
import time
|
| 2 |
import json
|
|
|
|
| 3 |
from typing import Any
|
| 4 |
from .llm import search_bot
|
| 5 |
from .vector_store import vector_store
|
|
@@ -47,4 +49,17 @@ class API():
|
|
| 47 |
text += i
|
| 48 |
yield i
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
api = API()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import time
|
| 3 |
import json
|
| 4 |
+
import requests
|
| 5 |
from typing import Any
|
| 6 |
from .llm import search_bot
|
| 7 |
from .vector_store import vector_store
|
|
|
|
| 49 |
text += i
|
| 50 |
yield i
|
| 51 |
|
| 52 |
+
def get_filecoin_price(self):
|
| 53 |
+
url="https://api.spacescope.io/v2/price/realtime"
|
| 54 |
+
headers = {
|
| 55 |
+
'authorization': f'Bearer {os.environ["SPACESCOPE_API_KEY"]}'
|
| 56 |
+
}
|
| 57 |
+
data = None
|
| 58 |
+
try:
|
| 59 |
+
response = requests.get(url, headers=headers)
|
| 60 |
+
data = json.loads(response.text)['data']['price']
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(e)
|
| 63 |
+
return data
|
| 64 |
+
|
| 65 |
api = API()
|
service/dataset.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from typing import Any
|
| 4 |
+
import pytz
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from huggingface_hub import CommitScheduler
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
from datasets import load_dataset
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Dataset:
|
| 12 |
+
|
| 13 |
+
DIR = os.path.join(os.path.dirname(__file__),"..","data")
|
| 14 |
+
|
| 15 |
+
def __init__(self):
|
| 16 |
+
self.scheduler = CommitScheduler(
|
| 17 |
+
repo_id="Jarvis-Log",
|
| 18 |
+
repo_type="dataset",
|
| 19 |
+
folder_path=self.DIR,
|
| 20 |
+
path_in_repo="data",
|
| 21 |
+
token=os.getenv("HF_TOKEN")
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
def current_time(self):
|
| 25 |
+
beijing_tz = pytz.timezone('Asia/Shanghai')
|
| 26 |
+
beijing_time = datetime.now(beijing_tz)
|
| 27 |
+
format_time = beijing_time.strftime('%Y-%m-%d %H:%M:%S')
|
| 28 |
+
return format_time
|
| 29 |
+
|
| 30 |
+
def current_date(self):
|
| 31 |
+
beijing_tz = pytz.timezone('Asia/Shanghai')
|
| 32 |
+
beijing_time = datetime.now(beijing_tz)
|
| 33 |
+
format_date = beijing_time.strftime('%Y-%m-%d')
|
| 34 |
+
return format_date
|
| 35 |
+
|
| 36 |
+
def _write_log(self, data, dir_name):
|
| 37 |
+
path = Path(self.DIR,dir_name)
|
| 38 |
+
path.mkdir(parents=True, exist_ok=True)
|
| 39 |
+
path = path/f'{self.current_date()}.json'
|
| 40 |
+
data['datetime'] = self.current_time()
|
| 41 |
+
data = json.dumps(data)+"\n"
|
| 42 |
+
with self.scheduler.lock:
|
| 43 |
+
with path.open("a") as f:
|
| 44 |
+
f.write(data)
|
| 45 |
+
|
| 46 |
+
def write_dataset(self,data):
|
| 47 |
+
self._write_log(data,"dataset")
|
| 48 |
+
|
| 49 |
+
def write_log(self,data):
|
| 50 |
+
self._write_log(data,"log")
|
| 51 |
+
|
| 52 |
+
def __call__(self,data):
|
| 53 |
+
print(f"[{self.current_time()}]{data}")
|
| 54 |
+
|
| 55 |
+
def load_dataset(self):
|
| 56 |
+
dataset = load_dataset("JR818/Jarvis-Log",data_dir="data/dataset",ignore_verifications=True)
|
| 57 |
+
return dataset.with_format("pandas").data
|
| 58 |
+
dataset = Dataset()
|
service/scheduler.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
| 6 |
+
from api import api
|
| 7 |
+
from .vector_store import vector_store
|
| 8 |
+
|
| 9 |
+
class Scheduler:
|
| 10 |
+
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.scheduler = BackgroundScheduler()
|
| 13 |
+
self.jobs = [self.update_filecoin_price]
|
| 14 |
+
|
| 15 |
+
def run(self):
|
| 16 |
+
for job in self.jobs:
|
| 17 |
+
self.scheduler.add_job(job, 'interval', minutes=5)
|
| 18 |
+
self.scheduler.start()
|
| 19 |
+
|
| 20 |
+
def update_filecoin_price(self):
|
| 21 |
+
price = api.get_filecoin_price()
|
| 22 |
+
if price is None:
|
| 23 |
+
return
|
| 24 |
+
text = "The price of filecoin(FIL) is currently ${} USD.".format(price)
|
| 25 |
+
vector_store.upsert(text,"dataset_1")
|
| 26 |
+
|
| 27 |
+
|
service/vector_store.py
CHANGED
|
@@ -141,4 +141,9 @@ class VectorStore(Pinecone):
|
|
| 141 |
for metadata in selected
|
| 142 |
]
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
vector_store = VectorStore()
|
|
|
|
| 141 |
for metadata in selected
|
| 142 |
]
|
| 143 |
|
| 144 |
+
def upsert(self,text,id):
|
| 145 |
+
embed = self.embeddings.embed_query(text)
|
| 146 |
+
vector_store._index.update(id=id,values=embed,set_metadata={"description":text,"text":text})
|
| 147 |
+
print(f"upsert text:{text} with id:{id}")
|
| 148 |
+
|
| 149 |
vector_store = VectorStore()
|