-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHugging_Chat.py
More file actions
311 lines (246 loc) · 13.1 KB
/
Hugging_Chat.py
File metadata and controls
311 lines (246 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import requests
import json
import os
import base64
from dotenv import load_dotenv
load_dotenv()
class HuggingChat_RE:
def __init__(self, hf_chat: str = os.environ.get("HUGGING_CHAT_ID"), model: str = "meta-llama/Meta-Llama-3-70B-Instruct") -> None:
"""
Initializes an instance of the HuggingChat_RE class.
Parameters:
- hf_chat (str): The Hugging Face chat token.
- model (str): The name or path of the model to be used for the chat. Defaults to "meta-llama/Meta-Llama-3-70B-Instruct".
Returns:
- None: This is a constructor method and does not return anything.
"""
self.hf_chat = hf_chat
self.model = model
self.headers = {
"Cookie": f"hf-chat={self.hf_chat}",
}
self.conversationId = self.find_conversation_id()
self.messageId = self.find_message_id()
def find_conversation_id(self) -> str:
"""
Finds and returns the conversation ID for the Hugging Face chat.
Returns:
- str: The conversation ID retrieved from the server response.
"""
url = "https://huggingface.co/chat/conversation"
payload = {"model": self.model}
response = requests.post(url, json=payload, headers=self.headers).json()
print("\033[92m" + "Initialised Conversation ID:", response['conversationId'] + "\033[0m")
return response['conversationId']
def find_message_id(self) -> str:
"""
Finds and returns the message ID for the Hugging Face chat.
Returns:
- str: The message ID retrieved from the server response.
"""
url = f"https://huggingface.co/chat/conversation/{self.conversationId}/__data.json?x-sveltekit-invalidated=11"
response = requests.get(url, headers=self.headers).json()
print("\033[92m" + "Initialised Message ID:", response['nodes'][1]['data'][3] + "\033[0m")
return response['nodes'][1]['data'][3]
def download_image(self, sha_value, output_filename="downloaded_image.png"):
# Construct the image URL
image_url = f"https://huggingface.co/chat/conversation/{self.conversationId}/output/{sha_value}"
# Define headers with required cookies and user-agent
headers = {
"Cookie": "hf-chat=94bb815b-befa-4f2a-b194-c7fff7c1b012",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
try:
# Send a GET request to the image URL with headers
response = requests.get(image_url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
# Open a file in binary write mode
with open(output_filename, "wb") as file:
# Write the image data to the file
file.write(response.content)
print("\033[92m" + f"Image successfully downloaded and saved as {output_filename}" + "\033[0m")
return output_filename
else:
print(f"Failed to download image. Status code: {response.status_code}")
except requests.RequestException as e:
print(f"An error occurred: {e}")
def generate(self, query: str, web_search: bool = False, filepath: str = None, stream: bool = True, output_filename: str = None) -> str:
"""
Generates a response for the given query using the Hugging Face chat.
Parameters:
- query (str): The text query to generate a response for.
- web_search (bool): Flag for web search. Defaults to False.
- filepath (str): Path to the file. Defaults to None.
- stream (bool): Flag for streaming response. Defaults to True.
Returns:
- str: The complete response.
"""
url = f"https://huggingface.co/chat/conversation/{self.conversationId}"
files_to_send = []
if filepath is not None:
with open(filepath, "rb") as file:
base64_content = base64.b64encode(file.read()).decode("utf-8")
files_to_send = [{
"mime": "application/pdf", # Adjust if necessary
"name": os.path.basename(filepath),
"type": "base64",
"value": base64_content
}]
payload = {
"inputs": query,
"id": self.messageId,
"is_retry": False,
"is_continue": False,
"web_search": web_search,
"files": files_to_send
}
response = requests.post(url, json=payload, headers=self.headers, stream=True)
complete_response = ""
for chunk in response.iter_lines(chunk_size=1, decode_unicode=True):
if chunk:
try:
json_data = json.loads(chunk.strip())
if json_data['type'] == "stream":
if stream: print(json_data['token'], end="", flush=True)
complete_response += json_data['token']
elif json_data['type'] == "tool" and json_data['subtype'] != "result":
print("\033[95m" + "Tools Are Used" + "\033[0m")
if json_data['subtype'] == "call":
print("\033[93m" + "Tools Name:", json_data['call']['name'] + "\033[0m")
print("\033[93m" + "Image Prompt:", json_data['call']['parameters']['prompt'] + "\033[0m", "\n")
elif json_data['subtype'] == "result":
print("\033[93m" + "Tools Result:", json_data['result'] + "\033[0m", "\n")
elif json_data['type'] == "file":
print("\033[95m" + "File Details" + "\033[0m")
print("\033[93m" + "Name:", json_data['name'] + "\033[0m")
print("\033[93m" + "Sha:", json_data['sha'] + "\033[0m")
if output_filename: self.download_image(json_data['sha'], output_filename)
else: self.download_image(json_data['sha'])
except:
continue
return complete_response
# Example Usage
if __name__ == "__main__":
hf_api = HuggingChat_RE(model="CohereForAI/c4ai-command-r-plus")
while True:
query = input("\n> ")
response = hf_api.generate(query, web_search=False)
# response = hf_api.generate(query, web_search=False, filepath=r"C:\Users\sreej\Downloads\about_blank (2).pdf")
# response = hf_api.generate(query, stream=False)
# print(response)
models = {
"meta-llama/Meta-Llama-3-70B-Instruct": "https://huggingface.co/meta-llama/Meta-Llama-3-70B-Instruct",
"HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1": "https://huggingface.co/HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1",
"CohereForAI/c4ai-command-r-plus": "https://huggingface.co/CohereForAI/c4ai-command-r-plus",
"mistralai/Mixtral-8x7B-Instruct-v0.1": "https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1",
"NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO": "https://huggingface.co/NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
"google/gemma-1.1-7b-it": "https://huggingface.co/google/gemma-1.1-7b-it",
"mistralai/Mistral-7B-Instruct-v0.2": "https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2",
"microsoft/Phi-3-mini-4k-instruct": "https://huggingface.co/microsoft/Phi-3-mini-4k-instruct"
}
for name, url in models.items():
print("\n\n" + name)
hf_api = HuggingChat_RE(model=name)
hf_api.generate("Who are you ?")
"******************************************************************"
"""Deprecated v1.0. Lack Support of Tools Support For Cohere. Still works For Other Models"""
"******************************************************************"
# import requests
# import json
# import os
# from dotenv import load_dotenv
# load_dotenv()
# class HuggingChat_RE:
# def __init__(self, hf_chat: str = os.environ.get("HUGGING_CHAT_ID"), model: str = "meta-llama/Meta-Llama-3-70B-Instruct") -> None:
# """
# Initializes an instance of the HuggingChat_RE class.
# Parameters:
# - hf_chat (str): The Hugging Face chat token.
# - model (str): The name or path of the model to be used for the chat. Defaults to "meta-llama/Meta-Llama-3-70B-Instruct".
# Returns:
# - None: This is a constructor method and does not return anything.
# """
# self.hf_chat = hf_chat
# self.model = model
# self.headers = {
# "Cookie": f"hf-chat={self.hf_chat}",
# }
# self.conversationId = self.find_conversation_id()
# self.messageId = self.find_message_id()
# def find_conversation_id(self) -> str:
# """
# Finds and returns the conversation ID for the Hugging Face chat.
# Returns:
# - str: The conversation ID retrieved from the server response.
# """
# url = "https://huggingface.co/chat/conversation"
# payload = {"model": self.model}
# response = requests.post(url, json=payload, headers=self.headers).json()
# print("\033[92m" + "Initialised Conversation ID:", response['conversationId'] + "\033[0m")
# return response['conversationId']
# def find_message_id(self) -> str:
# """
# Finds and returns the message ID for the Hugging Face chat.
# Returns:
# - str: The message ID retrieved from the server response.
# """
# url = f"https://huggingface.co/chat/conversation/{self.conversationId}/__data.json?x-sveltekit-invalidated=11"
# response = requests.get(url, headers=self.headers).json()
# print("\033[92m" + "Initialised Message ID:", response['nodes'][1]['data'][3] + "\033[0m")
# return response['nodes'][1]['data'][3]
# def generate(self, query: str, web_search: bool = False, files=[], stream: bool = True) -> str:
# """
# Generates a response for the given query using the Hugging Face chat.
# Parameters:
# - query (str): The text query to generate a response for.
# - web_search (bool): A flag indicating whether to perform web search in the response generation process. Defaults to False.
# - files (List[str]): A list of file paths to include in the query. Defaults to an empty list.
# - stream (bool): A flag indicating whether to stream the response. Defaults to True.
# Returns:
# - str: The complete response generated by the chat.
# """
# url = f"https://huggingface.co/chat/conversation/{self.conversationId}"
# payload = {
# "inputs": query,
# "id": self.messageId,
# "is_retry": False,
# "is_continue": False,
# "web_search": web_search,
# "files": files
# }
# response = requests.post(url, json=payload, headers=self.headers, stream=True)
# complete_response = ""
# for chunk in response.iter_content(chunk_size=1024):
# if chunk:
# # print(chunk)
# try:
# json_data = json.loads(chunk.decode("utf-8"))
# if json_data['type'] == "stream":
# if stream: print(json_data['token'], end="", flush=True)
# complete_response += json_data['token']
# except:
# continue
# return complete_response
# # Example Usage
# if __name__ == "__main__":
# hf_api = HuggingChat_RE(model="CohereForAI/c4ai-command-r-plus")
# while True:
# query = input("\n> ")
# response = hf_api.generate(query, web_search=False)
# # response = hf_api.generate(query, stream=False)
# # print(response)
# models = {
# "meta-llama/Meta-Llama-3-70B-Instruct": "https://huggingface.co/meta-llama/Meta-Llama-3-70B-Instruct",
# "HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1": "https://huggingface.co/HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1",
# "CohereForAI/c4ai-command-r-plus": "https://huggingface.co/CohereForAI/c4ai-command-r-plus",
# "mistralai/Mixtral-8x7B-Instruct-v0.1": "https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1",
# "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO": "https://huggingface.co/NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
# "google/gemma-1.1-7b-it": "https://huggingface.co/google/gemma-1.1-7b-it",
# "mistralai/Mistral-7B-Instruct-v0.2": "https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2",
# "microsoft/Phi-3-mini-4k-instruct": "https://huggingface.co/microsoft/Phi-3-mini-4k-instruct"
# }
# for name, url in models.items():
# print("\n\n" + name)
# hf_api = HuggingChat_RE(model=name)
# hf_api.generate("Who are you ?")