Spaces:
Running
Running
:gem: [Feature] HuggingchatStreamer: New get_hf_chat_id, and improve get_conversation_id
Browse files1234565789104 unmodified lines15161716181920212223242514 unmodified lines40414243444546474849505152535455565758596061626364656667686970717273747576777879808146828384855 unmodified lines9192939495969798import copyimport jsonimport reimport requestsimport uuid
# from curl_cffi import requestsfrom tclogger import loggerfrom transformers import AutoTokenizer
4 unmodified lines TOKEN_RESERVED,)from constants.envs import PROXIESfrom constants.networks import REQUESTS_HEADERSfrom constants.headers import ( REQUESTS_HEADERS, HUGGINGCHAT_POST_HEADERS, HUGGINGCHAT_SETTINGS_POST_DATA,)from messagers.message_outputer import OpenaiStreamOutputer
14 unmodified lines logger.note(f"Prompt Token Count: {token_count}") return token_count
def get_hf_chat_id(self): request_url = "https://huggingface.co/chat/settings" request_body = copy.deepcopy(HUGGINGCHAT_SETTINGS_POST_DATA) extra_body = { "activeModel": self.model_fullname, } request_body.update(extra_body) logger.note(f"> hf-chat ID:", end=" ")
res = requests.post( request_url, headers=HUGGINGCHAT_POST_HEADERS, json=request_body, proxies=PROXIES, timeout=10, ) self.hf_chat_id = res.cookies.get("hf-chat") if self.hf_chat_id: logger.success(f"[{self.hf_chat_id}]") else: logger.warn(f"[{res.status_code}]") logger.warn(res.text) raise ValueError("Failed to get hf-chat ID!")
def get_conversation_id(self, preprompt: str = ""): request_url = "https://huggingface.co/chat/conversation" request_headers = HUGGINGCHAT_POST_HEADERS extra_headers = { "Cookie": f"hf-chat={self.hf_chat_id}", } request_headers.update(extra_headers) request_body = { "model": self.model_fullname, "preprompt": preprompt, } logger.note(f"> Conversation ID:", end=" ")
res = requests.post( request_url, headers=REQUESTS_HEADERS, headers=request_headers, json=request_body, proxies=PROXIES, timeout=10,5 unmodified lines logger.warn(f"[{res.status_code}]") raise ValueError("Failed to get conversation ID!") self.conversation_id = conversation_id return conversation_id
def chat_response( self,networks/huggingchat_streamer.py+41-3