ffreemt
commited on
Commit
·
67f29a0
1
Parent(s):
3bdc2b8
Update landing html
Browse files
app.py
CHANGED
|
@@ -46,9 +46,14 @@ def fetch_tokens():
|
|
| 46 |
session = requests.Session()
|
| 47 |
|
| 48 |
# 检查并获取 appSession
|
| 49 |
-
if
|
|
|
|
|
|
|
|
|
|
| 50 |
logger.info("Fetching new appSession")
|
| 51 |
-
login_page_response = session.get(
|
|
|
|
|
|
|
| 52 |
if login_page_response.status_code != 200:
|
| 53 |
logger.error("Failed to load login page")
|
| 54 |
return None
|
|
@@ -74,7 +79,10 @@ def fetch_tokens():
|
|
| 74 |
cache["app_session_time"] = datetime.now() # type: ignore
|
| 75 |
|
| 76 |
# 检查并获取 accessToken
|
| 77 |
-
if
|
|
|
|
|
|
|
|
|
|
| 78 |
logger.info("Fetching new accessToken")
|
| 79 |
response = session.get(
|
| 80 |
"https://chat.reka.ai/bff/auth/access_token",
|
|
@@ -95,10 +103,13 @@ def fetch_tokens():
|
|
| 95 |
def landing():
|
| 96 |
return dedent(
|
| 97 |
"""
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
"""
|
| 103 |
)
|
| 104 |
|
|
@@ -115,7 +126,11 @@ def chat_completions():
|
|
| 115 |
},
|
| 116 |
)
|
| 117 |
|
| 118 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
logger.error("Unauthorized access attempt")
|
| 120 |
return Response("Unauthorized", status=401)
|
| 121 |
|
|
@@ -152,7 +167,9 @@ def chat_completions():
|
|
| 152 |
conversation_history.insert(
|
| 153 |
i + 1,
|
| 154 |
{
|
| 155 |
-
"type": "model"
|
|
|
|
|
|
|
| 156 |
"text": "",
|
| 157 |
},
|
| 158 |
)
|
|
@@ -212,7 +229,11 @@ def chat_completions():
|
|
| 212 |
if len(last_four_texts) > 4:
|
| 213 |
last_four_texts.pop(0)
|
| 214 |
|
| 215 |
-
if len(last_four_texts) == 4 and (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
break
|
| 217 |
|
| 218 |
full_content = data["text"]
|
|
@@ -220,7 +241,8 @@ def chat_completions():
|
|
| 220 |
prev_content = full_content
|
| 221 |
|
| 222 |
formatted_data = {
|
| 223 |
-
"id": "chatcmpl-"
|
|
|
|
| 224 |
"object": "chat.completion.chunk",
|
| 225 |
"created": created,
|
| 226 |
"model": model,
|
|
|
|
| 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 |
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",
|
|
|
|
| 103 |
def landing():
|
| 104 |
return dedent(
|
| 105 |
"""
|
| 106 |
+
<p>
|
| 107 |
+
query /hf/v1/chat/completions for a spin, e.g. <br/> curl -XPOST 127.0.0.1:7860/hf/v1/chat/completions -H "Authorization: Bearer Your_AUTHKEY"
|
| 108 |
+
</p>
|
| 109 |
+
<p>
|
| 110 |
+
or hf-space-url e.g.,<br/>
|
| 111 |
+
curl -XPOST https://mikeee-reka.hf.space/hf/v1/chat/completions -H "Authorization: Bearer Your_AUTHKEY" -H "Content-Type: application/json" --data "{\\"model\\": \\"reka-core\\", \\"messages\\": [{\\"role\\": \\"user\\", \\"content\\": \\"Say this is a test!\\"}]}"
|
| 112 |
+
</p>
|
| 113 |
"""
|
| 114 |
)
|
| 115 |
|
|
|
|
| 126 |
},
|
| 127 |
)
|
| 128 |
|
| 129 |
+
if (
|
| 130 |
+
request.method != "POST"
|
| 131 |
+
or request.path != "/hf/v1/chat/completions"
|
| 132 |
+
or request.headers.get("Authorization") != f"Bearer {AUTHKEY}"
|
| 133 |
+
):
|
| 134 |
logger.error("Unauthorized access attempt")
|
| 135 |
return Response("Unauthorized", status=401)
|
| 136 |
|
|
|
|
| 167 |
conversation_history.insert(
|
| 168 |
i + 1,
|
| 169 |
{
|
| 170 |
+
"type": "model"
|
| 171 |
+
if conversation_history[i]["type"] == "human"
|
| 172 |
+
else "human",
|
| 173 |
"text": "",
|
| 174 |
},
|
| 175 |
)
|
|
|
|
| 229 |
if len(last_four_texts) > 4:
|
| 230 |
last_four_texts.pop(0)
|
| 231 |
|
| 232 |
+
if len(last_four_texts) == 4 and (
|
| 233 |
+
len(last_four_texts[3]) < len(last_four_texts[2])
|
| 234 |
+
or last_four_texts[3].endswith("<sep")
|
| 235 |
+
or last_four_texts[3].endswith("<")
|
| 236 |
+
):
|
| 237 |
break
|
| 238 |
|
| 239 |
full_content = data["text"]
|
|
|
|
| 241 |
prev_content = full_content
|
| 242 |
|
| 243 |
formatted_data = {
|
| 244 |
+
"id": "chatcmpl-"
|
| 245 |
+
+ "".join([str(time.time()), str(hash(new_content))]),
|
| 246 |
"object": "chat.completion.chunk",
|
| 247 |
"created": created,
|
| 248 |
"model": model,
|