Spaces:
Build error
Build error
Download util/api.py from JR818/JarvisSearchV2: direct link, hf CLI and curl.
- Browser
- Download file 2.83 kB
-
https://huggingface.co/spaces/JR818/JarvisSearchV2/resolve/main/util/api.py
- Command line
-
hf download hf://spaces/JR818/JarvisSearchV2/util/api.py
-
curl -L -o api.py https://huggingface.co/spaces/JR818/JarvisSearchV2/resolve/main/util/api.py
2.83 kB
| import time | |
| from typing import Any | |
| from util.chatbot import SearchBot | |
| from util.vector_store import VectorStore | |
| from util.logger import logger | |
| class API(): | |
| HF_SPACE="JR818/Jarvis-Backend" | |
| PROJECT='filecoin' | |
| def __init__(self,mode=None) -> None: | |
| self.mode = mode | |
| self.vector_store = VectorStore() | |
| self.search_bot = SearchBot() | |
| logger("Created API") | |
| def auto_complete(self,query): | |
| if not query: | |
| return [] | |
| if self.mode == 'debug': | |
| suggestion = [ | |
| {"name":"Filecoin "+query,"url":"wwww"}, | |
| {"name":"Filecoin "+query,"url":"wwww"}, | |
| {"name":"Filecoin "+query,"url":"wwww"}, | |
| {"name":"Filecoin "+query,"url":"wwww"}, | |
| {"name":"Filecoin "+query,"url":"wwww"} | |
| ] | |
| return suggestion | |
| docs = self.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 search(self,query): | |
| if query == '': | |
| yield [] | |
| yield "Please input a query to search" | |
| return | |
| if self.mode == 'debug': | |
| source = [ | |
| {"name":"Filecoin","url":"wwww","description":"xxxx"}, | |
| {"name":"Filecoin Doc","url":"wwww","description":"xxxx"}, | |
| {"name":"Filecoin Doc2","url":"wwww","description":"xxxx"}, | |
| ] | |
| yield source | |
| text =f"this is a sample answer to {query} " | |
| for i in text: | |
| yield i | |
| time.sleep(0.1) | |
| return | |
| docs = self.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 source | |
| context = "\n".join(context) | |
| # print(f"context {context}") | |
| answer = self.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}) | |