ffreemt
commited on
Commit
·
3bdc2b8
1
Parent(s):
68991f5
Update
Browse files
app.py
CHANGED
|
@@ -46,14 +46,9 @@ def fetch_tokens():
|
|
| 46 |
session = requests.Session()
|
| 47 |
|
| 48 |
# 检查并获取 appSession
|
| 49 |
-
if (
|
| 50 |
-
not cache["app_session_time"]
|
| 51 |
-
or datetime.now() - cache["app_session_time"] >= APP_SESSION_VALIDITY
|
| 52 |
-
):
|
| 53 |
logger.info("Fetching new appSession")
|
| 54 |
-
login_page_response = session.get(
|
| 55 |
-
"https://chat.reka.ai/bff/auth/login", allow_redirects=True
|
| 56 |
-
)
|
| 57 |
if login_page_response.status_code != 200:
|
| 58 |
logger.error("Failed to load login page")
|
| 59 |
return None
|
|
@@ -79,10 +74,7 @@ def fetch_tokens():
|
|
| 79 |
cache["app_session_time"] = datetime.now() # type: ignore
|
| 80 |
|
| 81 |
# 检查并获取 accessToken
|
| 82 |
-
if (
|
| 83 |
-
not cache["access_token_time"]
|
| 84 |
-
or datetime.now() - cache["access_token_time"] >= ACCESS_TOKEN_VALIDITY
|
| 85 |
-
):
|
| 86 |
logger.info("Fetching new accessToken")
|
| 87 |
response = session.get(
|
| 88 |
"https://chat.reka.ai/bff/auth/access_token",
|
|
@@ -94,7 +86,7 @@ def fetch_tokens():
|
|
| 94 |
cache["access_token"] = response.json().get("accessToken")
|
| 95 |
cache["access_token_time"] = datetime.now() # type: ignore
|
| 96 |
|
| 97 |
-
y(cache)
|
| 98 |
|
| 99 |
return cache["access_token"]
|
| 100 |
|
|
@@ -123,11 +115,7 @@ def chat_completions():
|
|
| 123 |
},
|
| 124 |
)
|
| 125 |
|
| 126 |
-
if (
|
| 127 |
-
request.method != "POST"
|
| 128 |
-
or request.path != "/hf/v1/chat/completions"
|
| 129 |
-
or request.headers.get("Authorization") != f"Bearer {AUTHKEY}"
|
| 130 |
-
):
|
| 131 |
logger.error("Unauthorized access attempt")
|
| 132 |
return Response("Unauthorized", status=401)
|
| 133 |
|
|
@@ -164,9 +152,7 @@ def chat_completions():
|
|
| 164 |
conversation_history.insert(
|
| 165 |
i + 1,
|
| 166 |
{
|
| 167 |
-
"type": "model"
|
| 168 |
-
if conversation_history[i]["type"] == "human"
|
| 169 |
-
else "human",
|
| 170 |
"text": "",
|
| 171 |
},
|
| 172 |
)
|
|
@@ -226,11 +212,7 @@ def chat_completions():
|
|
| 226 |
if len(last_four_texts) > 4:
|
| 227 |
last_four_texts.pop(0)
|
| 228 |
|
| 229 |
-
if len(last_four_texts) == 4 and (
|
| 230 |
-
len(last_four_texts[3]) < len(last_four_texts[2])
|
| 231 |
-
or last_four_texts[3].endswith("<sep")
|
| 232 |
-
or last_four_texts[3].endswith("<")
|
| 233 |
-
):
|
| 234 |
break
|
| 235 |
|
| 236 |
full_content = data["text"]
|
|
@@ -238,8 +220,7 @@ def chat_completions():
|
|
| 238 |
prev_content = full_content
|
| 239 |
|
| 240 |
formatted_data = {
|
| 241 |
-
"id": "chatcmpl-"
|
| 242 |
-
+ "".join([str(time.time()), str(hash(new_content))]),
|
| 243 |
"object": "chat.completion.chunk",
|
| 244 |
"created": created,
|
| 245 |
"model": model,
|
|
|
|
| 46 |
session = requests.Session()
|
| 47 |
|
| 48 |
# 检查并获取 appSession
|
| 49 |
+
if not cache["app_session_time"] or datetime.now() - cache["app_session_time"] >= APP_SESSION_VALIDITY:
|
|
|
|
|
|
|
|
|
|
| 50 |
logger.info("Fetching new appSession")
|
| 51 |
+
login_page_response = session.get("https://chat.reka.ai/bff/auth/login", allow_redirects=True)
|
|
|
|
|
|
|
| 52 |
if login_page_response.status_code != 200:
|
| 53 |
logger.error("Failed to load login page")
|
| 54 |
return None
|
|
|
|
| 74 |
cache["app_session_time"] = datetime.now() # type: ignore
|
| 75 |
|
| 76 |
# 检查并获取 accessToken
|
| 77 |
+
if not cache["access_token_time"] or datetime.now() - cache["access_token_time"] >= ACCESS_TOKEN_VALIDITY:
|
|
|
|
|
|
|
|
|
|
| 78 |
logger.info("Fetching new accessToken")
|
| 79 |
response = session.get(
|
| 80 |
"https://chat.reka.ai/bff/auth/access_token",
|
|
|
|
| 86 |
cache["access_token"] = response.json().get("accessToken")
|
| 87 |
cache["access_token_time"] = datetime.now() # type: ignore
|
| 88 |
|
| 89 |
+
# y(cache)
|
| 90 |
|
| 91 |
return cache["access_token"]
|
| 92 |
|
|
|
|
| 115 |
},
|
| 116 |
)
|
| 117 |
|
| 118 |
+
if request.method != "POST" or request.path != "/hf/v1/chat/completions" or request.headers.get("Authorization") != f"Bearer {AUTHKEY}":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
logger.error("Unauthorized access attempt")
|
| 120 |
return Response("Unauthorized", status=401)
|
| 121 |
|
|
|
|
| 152 |
conversation_history.insert(
|
| 153 |
i + 1,
|
| 154 |
{
|
| 155 |
+
"type": "model" if conversation_history[i]["type"] == "human" else "human",
|
|
|
|
|
|
|
| 156 |
"text": "",
|
| 157 |
},
|
| 158 |
)
|
|
|
|
| 212 |
if len(last_four_texts) > 4:
|
| 213 |
last_four_texts.pop(0)
|
| 214 |
|
| 215 |
+
if len(last_four_texts) == 4 and (len(last_four_texts[3]) < len(last_four_texts[2]) or last_four_texts[3].endswith("<sep") or last_four_texts[3].endswith("<")):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
break
|
| 217 |
|
| 218 |
full_content = data["text"]
|
|
|
|
| 220 |
prev_content = full_content
|
| 221 |
|
| 222 |
formatted_data = {
|
| 223 |
+
"id": "chatcmpl-" + "".join([str(time.time()), str(hash(new_content))]),
|
|
|
|
| 224 |
"object": "chat.completion.chunk",
|
| 225 |
"created": created,
|
| 226 |
"model": model,
|