Spaces:
Running
Running
vlff李飞飞
commited on
Commit
·
b27dd8f
1
Parent(s):
47208d1
add token
Browse files
qwen_server/assistant_server.py
CHANGED
@@ -34,35 +34,12 @@ with open(Path(__file__).resolve().parent / 'js/main.js', 'r') as f:
|
|
34 |
js = f.read()
|
35 |
|
36 |
|
37 |
-
def
|
38 |
-
if not access_token:
|
39 |
-
gr.Info('The access token is not valid, please reset!')
|
40 |
-
return False
|
41 |
-
return True
|
42 |
-
|
43 |
-
|
44 |
-
def initialize(request: gr.Request):
|
45 |
-
set_page_url()
|
46 |
-
# print("Request headers dictionary:", request.headers)
|
47 |
-
params = dict(request.query_params)
|
48 |
-
access_token = params.get("access_token")
|
49 |
-
if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
|
50 |
-
gr.Info('The access token is not valid, please reset!')
|
51 |
-
return ''
|
52 |
-
# print("Query parameters:", dict(request.query_params))
|
53 |
-
return access_token
|
54 |
-
|
55 |
-
|
56 |
-
def add_text(history, text, access_token):
|
57 |
-
if not check_access_token(access_token):
|
58 |
-
return
|
59 |
history = history + [(text, None)]
|
60 |
return history, gr.update(value='', interactive=False)
|
61 |
|
62 |
|
63 |
-
def rm_text(history
|
64 |
-
if not check_access_token(access_token):
|
65 |
-
return
|
66 |
if not history:
|
67 |
gr.Warning('No input content!')
|
68 |
elif not history[-1][1]:
|
@@ -73,7 +50,7 @@ def rm_text(history, access_token):
|
|
73 |
|
74 |
|
75 |
def add_file(history, file):
|
76 |
-
history = history + [((file.name,), None)]
|
77 |
return history
|
78 |
|
79 |
|
@@ -85,6 +62,19 @@ def set_page_url():
|
|
85 |
PAGE_URL.append(lines[-1]['url'])
|
86 |
logger.info('The current access page is: ' + PAGE_URL[-1])
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
def bot(history):
|
89 |
set_page_url()
|
90 |
if not history:
|
@@ -139,9 +129,7 @@ def bot(history):
|
|
139 |
writer.write(new_line)
|
140 |
|
141 |
|
142 |
-
def load_history_session(history
|
143 |
-
if not check_access_token(access_token):
|
144 |
-
return
|
145 |
now_page = None
|
146 |
if not os.path.exists(cache_file):
|
147 |
gr.Info("Please add this page to ChatGPT's Reading List first!")
|
@@ -158,9 +146,7 @@ def load_history_session(history, access_token):
|
|
158 |
return now_page['session']
|
159 |
|
160 |
|
161 |
-
def clear_session(
|
162 |
-
if not check_access_token(access_token):
|
163 |
-
return
|
164 |
if not os.path.exists(cache_file):
|
165 |
return None
|
166 |
now_page = None
|
@@ -203,7 +189,7 @@ with gr.Blocks(css=css, theme='soft') as demo:
|
|
203 |
with gr.Column(scale=1, min_width=0):
|
204 |
re_bt = gr.Button('🔁', elem_classes='bt_small_font')
|
205 |
|
206 |
-
txt_msg = txt.submit(add_text, [chatbot, txt
|
207 |
queue=False).then(bot, chatbot, chatbot)
|
208 |
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
209 |
|
@@ -213,8 +199,8 @@ with gr.Blocks(css=css, theme='soft') as demo:
|
|
213 |
# None, [txt],
|
214 |
# queue=False)
|
215 |
|
216 |
-
clr_bt.click(clear_session,
|
217 |
-
re_txt_msg = re_bt.click(rm_text, [chatbot
|
218 |
queue=False).then(bot, chatbot, chatbot)
|
219 |
re_txt_msg.then(lambda: gr.update(interactive=True),
|
220 |
None, [txt],
|
@@ -222,7 +208,7 @@ with gr.Blocks(css=css, theme='soft') as demo:
|
|
222 |
|
223 |
stop_bt.click(None, None, None, cancels=[txt_msg, re_txt_msg], queue=False)
|
224 |
|
225 |
-
demo.load(initialize, [], [access_token]).then(load_history_session,
|
226 |
demo.queue()
|
227 |
|
228 |
# demo.queue().launch(server_name=server_config.server.server_host, server_port=server_config.server.app_in_browser_port)
|
|
|
34 |
js = f.read()
|
35 |
|
36 |
|
37 |
+
def add_text(history, text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
history = history + [(text, None)]
|
39 |
return history, gr.update(value='', interactive=False)
|
40 |
|
41 |
|
42 |
+
def rm_text(history):
|
|
|
|
|
43 |
if not history:
|
44 |
gr.Warning('No input content!')
|
45 |
elif not history[-1][1]:
|
|
|
50 |
|
51 |
|
52 |
def add_file(history, file):
|
53 |
+
history = history + [((file.name, ), None)]
|
54 |
return history
|
55 |
|
56 |
|
|
|
62 |
PAGE_URL.append(lines[-1]['url'])
|
63 |
logger.info('The current access page is: ' + PAGE_URL[-1])
|
64 |
|
65 |
+
|
66 |
+
def initialize(request: gr.Request):
|
67 |
+
set_page_url()
|
68 |
+
# print("Request headers dictionary:", request.headers)
|
69 |
+
params = dict(request.query_params)
|
70 |
+
access_token = params.get("access_token")
|
71 |
+
# if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
|
72 |
+
# gr.Info('The access token is not valid, please reset!')
|
73 |
+
# return ''
|
74 |
+
# print("Query parameters:", dict(request.query_params))
|
75 |
+
return access_token
|
76 |
+
|
77 |
+
|
78 |
def bot(history):
|
79 |
set_page_url()
|
80 |
if not history:
|
|
|
129 |
writer.write(new_line)
|
130 |
|
131 |
|
132 |
+
def load_history_session(history):
|
|
|
|
|
133 |
now_page = None
|
134 |
if not os.path.exists(cache_file):
|
135 |
gr.Info("Please add this page to ChatGPT's Reading List first!")
|
|
|
146 |
return now_page['session']
|
147 |
|
148 |
|
149 |
+
def clear_session():
|
|
|
|
|
150 |
if not os.path.exists(cache_file):
|
151 |
return None
|
152 |
now_page = None
|
|
|
189 |
with gr.Column(scale=1, min_width=0):
|
190 |
re_bt = gr.Button('🔁', elem_classes='bt_small_font')
|
191 |
|
192 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt],
|
193 |
queue=False).then(bot, chatbot, chatbot)
|
194 |
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
195 |
|
|
|
199 |
# None, [txt],
|
200 |
# queue=False)
|
201 |
|
202 |
+
clr_bt.click(clear_session, None, chatbot, queue=False)
|
203 |
+
re_txt_msg = re_bt.click(rm_text, [chatbot], [chatbot, txt],
|
204 |
queue=False).then(bot, chatbot, chatbot)
|
205 |
re_txt_msg.then(lambda: gr.update(interactive=True),
|
206 |
None, [txt],
|
|
|
208 |
|
209 |
stop_bt.click(None, None, None, cancels=[txt_msg, re_txt_msg], queue=False)
|
210 |
|
211 |
+
demo.load(initialize, [], [access_token]).then(load_history_session, chatbot, chatbot)
|
212 |
demo.queue()
|
213 |
|
214 |
# demo.queue().launch(server_name=server_config.server.server_host, server_port=server_config.server.app_in_browser_port)
|
qwen_server/database_server.py
CHANGED
@@ -16,7 +16,8 @@ from qwen_agent.utils.utils import get_local_ip
|
|
16 |
from qwen_server.schema import GlobalConfig
|
17 |
from qwen_server.utils import extract_and_cache_document
|
18 |
from starlette.middleware.base import BaseHTTPMiddleware
|
19 |
-
from
|
|
|
20 |
|
21 |
# Read config
|
22 |
with open(Path(__file__).resolve().parent / 'server_config.json', 'r') as f:
|
@@ -77,11 +78,11 @@ def change_checkbox_state(text, cache_file):
|
|
77 |
|
78 |
|
79 |
@app.middleware("http")
|
80 |
-
async def
|
|
|
81 |
access_token: str = request.headers.get("Authorization") or request.query_params.get("access_token")
|
82 |
if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
|
83 |
-
|
84 |
-
return Response(status_code=401, headers=headers)
|
85 |
return await call_next(request)
|
86 |
|
87 |
|
|
|
16 |
from qwen_server.schema import GlobalConfig
|
17 |
from qwen_server.utils import extract_and_cache_document
|
18 |
from starlette.middleware.base import BaseHTTPMiddleware
|
19 |
+
from typing import Callable, Coroutine, Any
|
20 |
+
|
21 |
|
22 |
# Read config
|
23 |
with open(Path(__file__).resolve().parent / 'server_config.json', 'r') as f:
|
|
|
78 |
|
79 |
|
80 |
@app.middleware("http")
|
81 |
+
async def add_process_time_header(request: Request, call_next):
|
82 |
+
print(f"Request URL path: {request.url}")
|
83 |
access_token: str = request.headers.get("Authorization") or request.query_params.get("access_token")
|
84 |
if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
|
85 |
+
return Response(status_code=401, content="the token is not valid")
|
|
|
86 |
return await call_next(request)
|
87 |
|
88 |
|