Spaces:
Running
Running
:gem: [Feature] HuggingchatStreamer: New get_hf_chat_id, and improve get_conversation_id
Browse files123456784 unmodified lines1314151617181914 unmodified lines343536373839404142434445464748495 unmodified lines555657585960import jsonimport reimport requestsfrom tclogger import loggerfrom transformers import AutoTokenizer4 unmodified linesTOKEN_RESERVED,)from constants.envs import PROXIESfrom constants.networks import REQUESTS_HEADERSfrom messagers.message_outputer import OpenaiStreamOutputer14 unmodified lineslogger.note(f"Prompt Token Count: {token_count}")return token_countdef get_conversation_id(self, preprompt: str = ""):request_url = "https://huggingface.co/chat/conversation"request_body = {"model": self.model_fullname,"preprompt": preprompt,}logger.note(f"> Conversation ID:", end=" ")res = requests.post(request_url,headers=REQUESTS_HEADERS,json=request_body,proxies=PROXIES,timeout=10,5 unmodified lineslogger.warn(f"[{res.status_code}]")raise ValueError("Failed to get conversation ID!")self.conversation_id = conversation_iddef chat_response(self,123456789104 unmodified lines151617181920212223242514 unmodified lines404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384855 unmodified lines9192939495969798import copyimport jsonimport reimport requestsimport uuid# from curl_cffi import requestsfrom tclogger import loggerfrom transformers import AutoTokenizer4 unmodified linesTOKEN_RESERVED,)from constants.envs import PROXIESfrom constants.headers import (REQUESTS_HEADERS,HUGGINGCHAT_POST_HEADERS,HUGGINGCHAT_SETTINGS_POST_DATA,)from messagers.message_outputer import OpenaiStreamOutputer14 unmodified lineslogger.note(f"Prompt Token Count: {token_count}")return token_countdef 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_HEADERSextra_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=request_headers,json=request_body,proxies=PROXIES,timeout=10,5 unmodified lineslogger.warn(f"[{res.status_code}]")raise ValueError("Failed to get conversation ID!")self.conversation_id = conversation_idreturn conversation_iddef chat_response(self,
networks/huggingchat_streamer.py+41-3