Spaces:
Runtime error
Runtime error
Download service/api.py from JR818/JarvisFastAPI: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/spaces/JR818/JarvisFastAPI/resolve/524fd18baf44cf2bf5561fa52a53279dc9a54fbd/service/api.py
- Command line
-
hf download hf://spaces/JR818/JarvisFastAPI@524fd18baf44cf2bf5561fa52a53279dc9a54fbd/service/api.py
-
curl -L -o api.py https://huggingface.co/spaces/JR818/JarvisFastAPI/resolve/524fd18baf44cf2bf5561fa52a53279dc9a54fbd/service/api.py
2.66 kB
| import os | |
| import time | |
| import json | |
| import requests | |
| from typing import Any | |
| from .llm import search_bot | |
| from .vector_store import vector_store | |
| # from .logger import logger | |
| from util.time import TimeUtil | |
| class API(): | |
| def __init__(self) -> None: | |
| pass | |
| def get_suggestion(self,query): | |
| if not query: | |
| return [] | |
| docs = vector_store.marginal_search(query) | |
| res = [] | |
| for doc in docs: | |
| name = doc.metadata.get('title_llm','') | |
| url = doc.metadata.get('source','') | |
| if name == '' or url == '': | |
| continue | |
| res.append({"name":name,"url":url}) | |
| # logger.write_log({"query":query,"api":'auto_complete',"result":res}) | |
| return res | |
| def get_answer(self,query): | |
| docs = vector_store.search(query) | |
| source = [] | |
| context = [] | |
| for doc in docs: | |
| if doc.metadata['score']<0.2: | |
| continue | |
| if 'title_llm' in doc.metadata: | |
| source.append({ | |
| "name":doc.metadata.get('title_llm',''), | |
| "url":doc.metadata.get('source',''), | |
| "description":doc.metadata.get('description_llm','') | |
| }) | |
| context.append(doc.metadata.get('description','')) | |
| # yield json.dumps(source) | |
| context = "\n".join(context) | |
| answer = search_bot(query,context,stream=True) | |
| text = "" | |
| for i in answer: | |
| text += i | |
| yield i | |
| # logger.write_log({ | |
| # "query":query, | |
| # "api":'search', | |
| # "source":source, | |
| # 'answer':text}) | |
| def get_filecoin_price(self): | |
| url="https://api.spacescope.io/v2/price/realtime" | |
| headers = { | |
| 'authorization': f'Bearer {os.environ["SPACESCOPE_API_KEY"]}' | |
| } | |
| data = None | |
| try: | |
| response = requests.get(url, headers=headers) | |
| data = json.loads(response.text)['data']['price'] | |
| except Exception as e: | |
| print(e) | |
| return data | |
| def get_filecoin_deposit(self): | |
| url="https://api.spacescope.io/v2/fvm/defi/all_protocol_flow?start_date={}&end_date={}".format(TimeUtil.get_date(offset=-2),TimeUtil.get_date(offset=-1)) | |
| headers = { | |
| 'authorization': f'Bearer {os.environ["SPACESCOPE_API_KEY"]}' | |
| } | |
| data = None | |
| try: | |
| response = requests.get(url, headers=headers) | |
| data = round(json.loads(response.text)['data'][-1]['total_inflow_fil'],1) | |
| except Exception as e: | |
| print(e) | |
| return data | |
| api = API() |