Husnain commited on
Commit
2f7e54b
1 Parent(s): d860ba7

🔨 [WIP] Enabling get_message_id

Browse files
Files changed (1) hide show
  1. networks/huggingchat_streamer.py +153 -7
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
 
@@ -25,7 +31,6 @@ class HuggingchatStreamer:
25
  self.model = "mixtral-8x7b"
26
  self.model_fullname = MODEL_MAP[self.model]
27
  self.message_outputer = OpenaiStreamOutputer(model=self.model)
28
- # export HF_ENDPOINT=https://hf-mirror.com
29
  self.tokenizer = AutoTokenizer.from_pretrained(self.model_fullname)
30
 
31
  def count_tokens(self, text):
@@ -34,16 +39,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 +90,86 @@ 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,
@@ -65,7 +180,37 @@ class HuggingchatStreamer:
65
  api_key: str = None,
66
  use_cache: bool = False,
67
  ):
68
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  def chat_return_dict(self, stream_response):
71
  pass
@@ -76,5 +221,6 @@ class HuggingchatStreamer:
76
 
77
  if __name__ == "__main__":
78
  streamer = HuggingchatStreamer(model="mixtral-8x7b")
79
- conversation_id = streamer.get_conversation_id()
80
- # python -m networks.huggingchat_streamer
 
 
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
 
 
31
  self.model = "mixtral-8x7b"
32
  self.model_fullname = MODEL_MAP[self.model]
33
  self.message_outputer = OpenaiStreamOutputer(model=self.model)
 
34
  self.tokenizer = AutoTokenizer.from_pretrained(self.model_fullname)
35
 
36
  def count_tokens(self, text):
 
39
  logger.note(f"Prompt Token Count: {token_count}")
40
  return token_count
41
 
42
+ def get_hf_chat_id(self):
43
+ request_url = "https://huggingface.co/chat/settings"
44
+ request_body = copy.deepcopy(HUGGINGCHAT_SETTINGS_POST_DATA)
45
+ extra_body = {
46
+ "activeModel": self.model_fullname,
47
+ }
48
+ request_body.update(extra_body)
49
+ logger.note(f"> hf-chat ID:", end=" ")
50
+
51
+ res = requests.post(
52
+ request_url,
53
+ headers=HUGGINGCHAT_POST_HEADERS,
54
+ json=request_body,
55
+ proxies=PROXIES,
56
+ timeout=10,
57
+ )
58
+ self.hf_chat_id = res.cookies.get("hf-chat")
59
+ if self.hf_chat_id:
60
+ logger.success(f"[{self.hf_chat_id}]")
61
+ else:
62
+ logger.warn(f"[{res.status_code}]")
63
+ logger.warn(res.text)
64
+ raise ValueError("Failed to get hf-chat ID!")
65
+
66
  def get_conversation_id(self, preprompt: str = ""):
67
  request_url = "https://huggingface.co/chat/conversation"
68
+ request_headers = HUGGINGCHAT_POST_HEADERS
69
+ extra_headers = {
70
+ "Cookie": f"hf-chat={self.hf_chat_id}",
71
+ }
72
+ request_headers.update(extra_headers)
73
  request_body = {
74
  "model": self.model_fullname,
75
  "preprompt": preprompt,
76
  }
77
  logger.note(f"> Conversation ID:", end=" ")
78
+
79
  res = requests.post(
80
  request_url,
81
+ headers=request_headers,
82
  json=request_body,
83
  proxies=PROXIES,
84
  timeout=10,
 
90
  logger.warn(f"[{res.status_code}]")
91
  raise ValueError("Failed to get conversation ID!")
92
  self.conversation_id = conversation_id
93
+ return conversation_id
94
+
95
+ def get_message_id(self):
96
+ request_url = f"https://huggingface.co/chat/conversation/{self.conversation_id}/__data.json?x-sveltekit-invalidated=11"
97
+ request_headers = HUGGINGCHAT_POST_HEADERS
98
+ extra_headers = {
99
+ "Cookie": f"hf-chat={self.hf_chat_id}",
100
+ }
101
+ request_headers.update(extra_headers)
102
+ logger.note(f"> Message ID:", end=" ")
103
+
104
+ message_id = None
105
+ res = requests.post(
106
+ request_url,
107
+ headers=request_headers,
108
+ proxies=PROXIES,
109
+ timeout=10,
110
+ )
111
+ if res.status_code == 200:
112
+ data = res.json()
113
+ # TODO - extract message_id
114
+
115
+ logger.success(f"[{message_id}]")
116
+ else:
117
+ logger.warn(f"[{res.status_code}]")
118
+ raise ValueError("Failed to get conversation ID!")
119
+
120
+ return message_id
121
+
122
+ def log_request(self, url, method="GET"):
123
+ logger.note(f"> {method}:", end=" ")
124
+ logger.mesg(f"{url}", end=" ")
125
+
126
+ def log_response(
127
+ self, res: requests.Response, stream=False, iter_lines=False, verbose=False
128
+ ):
129
+ status_code = res.status_code
130
+ status_code_str = f"[{status_code}]"
131
+
132
+ if status_code == 200:
133
+ logger_func = logger.success
134
+ else:
135
+ logger_func = logger.warn
136
+
137
+ logger_func(status_code_str)
138
+
139
+ logger.enter_quiet(not verbose)
140
+
141
+ if status_code != 200:
142
+ logger_func(res.text)
143
+
144
+ if stream:
145
+ if not iter_lines:
146
+ return
147
+
148
+ for line in res.iter_lines():
149
+ line = line.decode("utf-8")
150
+ line = re.sub(r"^data:\s*", "", line)
151
+ line = line.strip()
152
+ if line:
153
+ try:
154
+ data = json.loads(line, strict=False)
155
+ msg_type = data.get("type")
156
+ if msg_type == "status":
157
+ msg_status = data.get("status")
158
+ elif msg_type == "stream":
159
+ content = data.get("token", "")
160
+ logger_func(content, end="")
161
+ elif msg_type == "finalAnswer":
162
+ full_content = data.get("text")
163
+ logger.success("\n[Finished]")
164
+ break
165
+ else:
166
+ pass
167
+ except Exception as e:
168
+ logger.warn(e)
169
+ else:
170
+ logger_func(res.json())
171
+
172
+ logger.exit_quiet(not verbose)
173
 
174
  def chat_response(
175
  self,
 
180
  api_key: str = None,
181
  use_cache: bool = False,
182
  ):
183
+ self.get_hf_chat_id()
184
+ self.get_conversation_id()
185
+ message_id = self.get_message_id()
186
+
187
+ request_url = f"https://huggingface.co/chat/conversation/{self.conversation_id}"
188
+ request_headers = copy.deepcopy(HUGGINGCHAT_POST_HEADERS)
189
+ extra_headers = {
190
+ "Content-Type": "text/event-stream",
191
+ "Referer": request_url,
192
+ "Cookie": f"hf-chat={self.hf_chat_id}",
193
+ }
194
+ request_headers.update(extra_headers)
195
+ request_body = {
196
+ "files": [],
197
+ "id": message_id,
198
+ "inputs": prompt,
199
+ "is_continue": False,
200
+ "is_retry": False,
201
+ "web_search": False,
202
+ }
203
+ self.log_request(request_url, method="POST")
204
+
205
+ res = requests.post(
206
+ request_url,
207
+ headers=request_headers,
208
+ json=request_body,
209
+ proxies=PROXIES,
210
+ stream=True,
211
+ )
212
+ self.log_response(res, stream=True, iter_lines=True, verbose=True)
213
+ return res
214
 
215
  def chat_return_dict(self, stream_response):
216
  pass
 
221
 
222
  if __name__ == "__main__":
223
  streamer = HuggingchatStreamer(model="mixtral-8x7b")
224
+ prompt = "who are you?"
225
+ streamer.chat_response(prompt=prompt)
226
+ # HF_ENDPOINT=https://hf-mirror.com python -m networks.huggingchat_streamer