Hansimov commited on
Commit
391cdfe
1 Parent(s): 8b09459

:gem: [Feature] HuggingchatStreamer: New get_hf_chat_id, and improve get_conversation_id

Browse files
Files changed (1) hide show
  1. networks/huggingchat_streamer.py +41 -3
networks/huggingchat_streamer.py CHANGED
@@ -1,8 +1,10 @@
 
1
  import json
2
  import re
3
  import requests
 
4
 
5
-
6
  from tclogger import logger
7
  from transformers import AutoTokenizer
8
 
@@ -13,7 +15,11 @@ from constants.models import (
13
  TOKEN_RESERVED,
14
  )
15
  from constants.envs import PROXIES
16
- from constants.networks import REQUESTS_HEADERS
 
 
 
 
17
  from messagers.message_outputer import OpenaiStreamOutputer
18
 
19
 
@@ -34,16 +40,46 @@ class HuggingchatStreamer:
34
  logger.note(f"Prompt Token Count: {token_count}")
35
  return token_count
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  def get_conversation_id(self, preprompt: str = ""):
38
  request_url = "https://huggingface.co/chat/conversation"
 
 
 
 
 
39
  request_body = {
40
  "model": self.model_fullname,
41
  "preprompt": preprompt,
42
  }
43
  logger.note(f"> Conversation ID:", end=" ")
 
44
  res = requests.post(
45
  request_url,
46
- headers=REQUESTS_HEADERS,
47
  json=request_body,
48
  proxies=PROXIES,
49
  timeout=10,
@@ -55,6 +91,8 @@ class HuggingchatStreamer:
55
  logger.warn(f"[{res.status_code}]")
56
  raise ValueError("Failed to get conversation ID!")
57
  self.conversation_id = conversation_id
 
 
58
 
59
  def chat_response(
60
  self,
 
1
+ import copy
2
  import json
3
  import re
4
  import requests
5
+ import uuid
6
 
7
+ # from curl_cffi import requests
8
  from tclogger import logger
9
  from transformers import AutoTokenizer
10
 
 
15
  TOKEN_RESERVED,
16
  )
17
  from constants.envs import PROXIES
18
+ from constants.headers import (
19
+ REQUESTS_HEADERS,
20
+ HUGGINGCHAT_POST_HEADERS,
21
+ HUGGINGCHAT_SETTINGS_POST_DATA,
22
+ )
23
  from messagers.message_outputer import OpenaiStreamOutputer
24
 
25
 
 
40
  logger.note(f"Prompt Token Count: {token_count}")
41
  return token_count
42
 
43
+ def get_hf_chat_id(self):
44
+ request_url = "https://huggingface.co/chat/settings"
45
+ request_body = copy.deepcopy(HUGGINGCHAT_SETTINGS_POST_DATA)
46
+ extra_body = {
47
+ "activeModel": self.model_fullname,
48
+ }
49
+ request_body.update(extra_body)
50
+ logger.note(f"> hf-chat ID:", end=" ")
51
+
52
+ res = requests.post(
53
+ request_url,
54
+ headers=HUGGINGCHAT_POST_HEADERS,
55
+ json=request_body,
56
+ proxies=PROXIES,
57
+ timeout=10,
58
+ )
59
+ self.hf_chat_id = res.cookies.get("hf-chat")
60
+ if self.hf_chat_id:
61
+ logger.success(f"[{self.hf_chat_id}]")
62
+ else:
63
+ logger.warn(f"[{res.status_code}]")
64
+ logger.warn(res.text)
65
+ raise ValueError("Failed to get hf-chat ID!")
66
+
67
  def get_conversation_id(self, preprompt: str = ""):
68
  request_url = "https://huggingface.co/chat/conversation"
69
+ request_headers = HUGGINGCHAT_POST_HEADERS
70
+ extra_headers = {
71
+ "Cookie": f"hf-chat={self.hf_chat_id}",
72
+ }
73
+ request_headers.update(extra_headers)
74
  request_body = {
75
  "model": self.model_fullname,
76
  "preprompt": preprompt,
77
  }
78
  logger.note(f"> Conversation ID:", end=" ")
79
+
80
  res = requests.post(
81
  request_url,
82
+ headers=request_headers,
83
  json=request_body,
84
  proxies=PROXIES,
85
  timeout=10,
 
91
  logger.warn(f"[{res.status_code}]")
92
  raise ValueError("Failed to get conversation ID!")
93
  self.conversation_id = conversation_id
94
+ return conversation_id
95
+
96
 
97
  def chat_response(
98
  self,