vlff李飞飞 commited on
Commit
3cac10f
·
1 Parent(s): a7f9822
browser_qwen/background.js CHANGED
@@ -1,24 +1,23 @@
1
  var database;
2
 
3
  function send_data(msg){
4
- chrome.storage.local.get(['database_host'], function(result) {
5
- if (result.database_host) {
6
- console.log('database_host currently is ' + result.database_host);
7
- database = "https://taohongxiu-qwen-agent.hf.space/endpoint";
8
- } else {
9
- database = "https://taohongxiu-qwen-agent.hf.space/endpoint";
 
 
 
 
 
 
 
 
 
 
10
  }
11
- fetch(database, {
12
- method: "POST",
13
- headers: {
14
- "Content-Type": "application/json",
15
- },
16
- body: JSON.stringify(msg),
17
- })
18
- .then((response) => response.json())
19
- .then((data) => {
20
- console.log(data.result);
21
- });
22
  });
23
  }
24
 
 
1
  var database;
2
 
3
  function send_data(msg){
4
+ chrome.storage.local.get(['access_token'], function(result) {
5
+ if (result.access_token) {
6
+ console.log('access_token currently is ' + result.access_token);
7
+ database_url = "https://taohongxiu-qwen-agent.hf.space/endpoint";
8
+ fetch(database_url, {
9
+ method: "POST",
10
+ headers: {
11
+ "Content-Type": "application/json",
12
+ "Authorization": result.access_token
13
+ },
14
+ body: JSON.stringify(msg),
15
+ })
16
+ .then((response) => response.json())
17
+ .then((data) => {
18
+ console.log(data.result);
19
+ });
20
  }
 
 
 
 
 
 
 
 
 
 
 
21
  });
22
  }
23
 
browser_qwen/src/popup.html CHANGED
@@ -116,6 +116,10 @@
116
  <input type="text" id="addr" name="addr" class="input-text" style="display: none;">
117
  <button id="set_addr" class="upload_btn" style="display: none;">Change</button>
118
 
 
 
 
 
119
  <script src="popup.js"></script>
120
  </body>
121
  </html>
 
116
  <input type="text" id="addr" name="addr" class="input-text" style="display: none;">
117
  <button id="set_addr" class="upload_btn" style="display: none;">Change</button>
118
 
119
+ <h3>Customize Token:</h3>
120
+ <input type="text" id="access_token" name="access_token" class="input-text" placeholder="input and chick change button, then reopen this">
121
+ <button id="set_access_token" class="upload_btn">Change</button>
122
+
123
  <script src="popup.js"></script>
124
  </body>
125
  </html>
browser_qwen/src/popup.js CHANGED
@@ -24,23 +24,20 @@ document.addEventListener('DOMContentLoaded', function() {
24
 
25
  });
26
  setTimeout(function() {
27
- // console.log('This message will be logged after 0.5 second');
28
  var popup_url='';
29
- chrome.storage.local.get(['database_host'], function(result) {
30
- if (result.database_host) {
31
- console.log('database_host currently is ' + result.database_host);
32
- popup_url = "https://taohongxiu-qwen-agent.hf.space/";
33
- } else {
34
- popup_url = "https://taohongxiu-qwen-agent.hf.space/";
 
 
 
 
 
35
  }
36
- var iframe = document.createElement('iframe');
37
- iframe.src = popup_url;
38
- iframe.height = '570px';
39
- // iframe.sandbox = 'allow-same-origin allow-scripts';
40
- // iframe.allow = "geolocation *;";
41
- var iframe_area = document.getElementById('iframe_area')
42
- iframe_area.appendChild(iframe);
43
-
44
  });
45
  }, 500);
46
 
@@ -63,3 +60,13 @@ document.getElementById('set_addr').addEventListener('click', function() {
63
  document.getElementById('addr').value = '';
64
  });
65
  })
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  });
26
  setTimeout(function() {
27
+ console.log('This message will be logged after 0.5 second');
28
  var popup_url='';
29
+ chrome.storage.local.get(['access_token'], function(result) {
30
+ if (result.access_token) {
31
+ console.log('access_token currently is ' + result.access_token);
32
+ popup_url = "https://taohongxiu-qwen-agent.hf.space/?access_token"+access_token;
33
+ var iframe = document.createElement('iframe');
34
+ iframe.src = popup_url;
35
+ iframe.height = '570px';
36
+ // iframe.sandbox = 'allow-same-origin allow-scripts';
37
+ // iframe.allow = "geolocation *;";
38
+ var iframe_area = document.getElementById('iframe_area')
39
+ iframe_area.appendChild(iframe);
40
  }
 
 
 
 
 
 
 
 
41
  });
42
  }, 500);
43
 
 
60
  document.getElementById('addr').value = '';
61
  });
62
  })
63
+
64
+ document.getElementById('set_access_token').addEventListener('click', function() {
65
+ var access_token = document.getElementById('access_token').value;
66
+ // save config
67
+ chrome.storage.local.set({access_token: access_token}, function() {
68
+ console.log('access_token is set to ' + access_token);
69
+ // chrome.runtime.sendMessage({ data: access_token , close: true , flag: 'set_access_token'});
70
+ document.getElementById('access_token').value = '';
71
+ });
72
+ })
qwen_server/assistant_server.py CHANGED
@@ -24,8 +24,7 @@ llm = get_chat_model(model=server_config.server.llm,
24
  mem = Memory(llm=llm, stream=False)
25
 
26
  cache_file = os.path.join(server_config.path.cache_root, 'browse.jsonl')
27
- cache_file_popup_url = os.path.join(server_config.path.cache_root,
28
- 'popup_url.jsonl')
29
 
30
  PAGE_URL = []
31
 
@@ -35,12 +34,35 @@ with open(Path(__file__).resolve().parent / 'js/main.js', 'r') as f:
35
  js = f.read()
36
 
37
 
38
- def add_text(history, text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  history = history + [(text, None)]
40
  return history, gr.update(value='', interactive=False)
41
 
42
 
43
- def rm_text(history):
 
 
44
  if not history:
45
  gr.Warning('No input content!')
46
  elif not history[-1][1]:
@@ -51,7 +73,7 @@ def rm_text(history):
51
 
52
 
53
  def add_file(history, file):
54
- history = history + [((file.name, ), None)]
55
  return history
56
 
57
 
@@ -63,7 +85,6 @@ def set_page_url():
63
  PAGE_URL.append(lines[-1]['url'])
64
  logger.info('The current access page is: ' + PAGE_URL[-1])
65
 
66
-
67
  def bot(history):
68
  set_page_url()
69
  if not history:
@@ -72,7 +93,7 @@ def bot(history):
72
  now_page = None
73
  _ref = ''
74
  if not os.path.exists(cache_file):
75
- gr.Info("Please add this page to Qwen's Reading List first!")
76
  else:
77
  for line in jsonlines.open(cache_file):
78
  if line['url'] == PAGE_URL[-1]:
@@ -80,10 +101,10 @@ def bot(history):
80
 
81
  if not now_page:
82
  gr.Info(
83
- "This page has not yet been added to the Qwen's reading list!"
84
  )
85
  elif not now_page['raw']:
86
- gr.Info('Please reopen later, Qwen is analyzing this page...')
87
  else:
88
  _ref_list = mem.get(
89
  history[-1][0], [now_page],
@@ -118,24 +139,28 @@ def bot(history):
118
  writer.write(new_line)
119
 
120
 
121
- def load_history_session(history):
 
 
122
  now_page = None
123
  if not os.path.exists(cache_file):
124
- gr.Info("Please add this page to Qwen's Reading List first!")
125
  return []
126
  for line in jsonlines.open(cache_file):
127
  if line['url'] == PAGE_URL[-1]:
128
  now_page = line
129
  if not now_page:
130
- gr.Info("Please add this page to Qwen's Reading List first!")
131
  return []
132
  if not now_page['raw']:
133
- gr.Info('Please wait, Qwen is analyzing this page...')
134
  return []
135
  return now_page['session']
136
 
137
 
138
- def clear_session():
 
 
139
  if not os.path.exists(cache_file):
140
  return None
141
  now_page = None
@@ -157,6 +182,7 @@ def clear_session():
157
 
158
 
159
  with gr.Blocks(css=css, theme='soft') as demo:
 
160
  chatbot = gr.Chatbot([],
161
  elem_id='chatbot',
162
  height=480,
@@ -166,7 +192,7 @@ with gr.Blocks(css=css, theme='soft') as demo:
166
  with gr.Row():
167
  with gr.Column(scale=7):
168
  txt = gr.Textbox(show_label=False,
169
- placeholder='Chat with Qwen...',
170
  container=False)
171
  # with gr.Column(scale=0.06, min_width=0):
172
  # smt_bt = gr.Button('⏎')
@@ -177,7 +203,7 @@ with gr.Blocks(css=css, theme='soft') as demo:
177
  with gr.Column(scale=1, min_width=0):
178
  re_bt = gr.Button('🔁', elem_classes='bt_small_font')
179
 
180
- txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt],
181
  queue=False).then(bot, chatbot, chatbot)
182
  txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
183
 
@@ -187,8 +213,8 @@ with gr.Blocks(css=css, theme='soft') as demo:
187
  # None, [txt],
188
  # queue=False)
189
 
190
- clr_bt.click(clear_session, None, chatbot, queue=False)
191
- re_txt_msg = re_bt.click(rm_text, [chatbot], [chatbot, txt],
192
  queue=False).then(bot, chatbot, chatbot)
193
  re_txt_msg.then(lambda: gr.update(interactive=True),
194
  None, [txt],
@@ -196,7 +222,7 @@ with gr.Blocks(css=css, theme='soft') as demo:
196
 
197
  stop_bt.click(None, None, None, cancels=[txt_msg, re_txt_msg], queue=False)
198
 
199
- demo.load(set_page_url).then(load_history_session, chatbot, chatbot)
200
  demo.queue()
201
 
202
  # demo.queue().launch(server_name=server_config.server.server_host, server_port=server_config.server.app_in_browser_port)
 
24
  mem = Memory(llm=llm, stream=False)
25
 
26
  cache_file = os.path.join(server_config.path.cache_root, 'browse.jsonl')
27
+ cache_file_popup_url = os.path.join(server_config.path.cache_root, 'popup_url.jsonl')
 
28
 
29
  PAGE_URL = []
30
 
 
34
  js = f.read()
35
 
36
 
37
+ def check_access_token(access_token):
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, access_token):
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
 
74
 
75
  def add_file(history, file):
76
+ history = history + [((file.name,), None)]
77
  return history
78
 
79
 
 
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:
 
93
  now_page = None
94
  _ref = ''
95
  if not os.path.exists(cache_file):
96
+ gr.Info("Please add this page to ChatGPT's Reading List first!")
97
  else:
98
  for line in jsonlines.open(cache_file):
99
  if line['url'] == PAGE_URL[-1]:
 
101
 
102
  if not now_page:
103
  gr.Info(
104
+ "This page has not yet been added to the ChatGPT's reading list!"
105
  )
106
  elif not now_page['raw']:
107
+ gr.Info('Please reopen later, ChatGPT is analyzing this page...')
108
  else:
109
  _ref_list = mem.get(
110
  history[-1][0], [now_page],
 
139
  writer.write(new_line)
140
 
141
 
142
+ def load_history_session(history, access_token):
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!")
148
  return []
149
  for line in jsonlines.open(cache_file):
150
  if line['url'] == PAGE_URL[-1]:
151
  now_page = line
152
  if not now_page:
153
+ gr.Info("Please add this page to ChatGPT's Reading List first!")
154
  return []
155
  if not now_page['raw']:
156
+ gr.Info('Please wait, ChatGPT is analyzing this page...')
157
  return []
158
  return now_page['session']
159
 
160
 
161
+ def clear_session(access_token):
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
 
182
 
183
 
184
  with gr.Blocks(css=css, theme='soft') as demo:
185
+ access_token = gr.State("")
186
  chatbot = gr.Chatbot([],
187
  elem_id='chatbot',
188
  height=480,
 
192
  with gr.Row():
193
  with gr.Column(scale=7):
194
  txt = gr.Textbox(show_label=False,
195
+ placeholder='Chat with ChatGPT...',
196
  container=False)
197
  # with gr.Column(scale=0.06, min_width=0):
198
  # smt_bt = gr.Button('⏎')
 
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, access_token], [chatbot, txt],
207
  queue=False).then(bot, chatbot, chatbot)
208
  txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
209
 
 
213
  # None, [txt],
214
  # queue=False)
215
 
216
+ clr_bt.click(clear_session, [access_token], chatbot, queue=False)
217
+ re_txt_msg = re_bt.click(rm_text, [chatbot, access_token], [chatbot, txt],
218
  queue=False).then(bot, chatbot, chatbot)
219
  re_txt_msg.then(lambda: gr.update(interactive=True),
220
  None, [txt],
 
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, [chatbot, access_token], chatbot)
226
  demo.queue()
227
 
228
  # 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
@@ -6,7 +6,7 @@ from pathlib import Path
6
  import add_qwen_libs # NOQA
7
  import jsonlines
8
  import uvicorn
9
- from fastapi import FastAPI, Request
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from fastapi.responses import JSONResponse
12
  from fastapi.staticfiles import StaticFiles
@@ -15,6 +15,8 @@ from qwen_agent.log import logger
15
  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
 
19
  # Read config
20
  with open(Path(__file__).resolve().parent / 'server_config.json', 'r') as f:
@@ -74,13 +76,37 @@ def change_checkbox_state(text, cache_file):
74
  return {'result': 'changed'}
75
 
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  @app.get('/healthz')
78
  async def healthz(request: Request):
79
  return JSONResponse({"healthz": True})
80
 
81
 
82
  @app.get('/cachedata/{file_name}')
83
- async def cache_data(file_name: str):
 
 
84
  cache_file = os.path.join(server_config.path.cache_root, file_name)
85
  lines = []
86
  for line in jsonlines.open(cache_file):
@@ -91,6 +117,9 @@ async def cache_data(file_name: str):
91
  @app.post('/endpoint')
92
  async def web_listening(request: Request):
93
  data = await request.json()
 
 
 
94
  msg_type = data['task']
95
 
96
  cache_file_popup_url = os.path.join(server_config.path.cache_root, 'popup_url.jsonl')
 
6
  import add_qwen_libs # NOQA
7
  import jsonlines
8
  import uvicorn
9
+ from fastapi import FastAPI, Request, HTTPException, Response
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from fastapi.responses import JSONResponse
12
  from fastapi.staticfiles import StaticFiles
 
15
  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 fastapi.security import HTTPBearer
20
 
21
  # Read config
22
  with open(Path(__file__).resolve().parent / 'server_config.json', 'r') as f:
 
76
  return {'result': 'changed'}
77
 
78
 
79
+ class BasicAuthMiddleware(BaseHTTPMiddleware):
80
+ def __init__(self, app, token: str):
81
+ super().__init__(app)
82
+ self.required_credentials = token
83
+
84
+ async def dispatch(self, request: Request, call_next):
85
+ authorization: str = request.headers.get("Authorization")
86
+ if authorization:
87
+ try:
88
+ schema, credentials = authorization.split()
89
+ if credentials == self.required_credentials:
90
+ return await call_next(request)
91
+ except ValueError:
92
+ pass
93
+
94
+ headers = {'WWW-Authenticate': 'Basic'}
95
+ return Response(status_code=401, headers=headers)
96
+
97
+
98
+ app.add_middleware(BasicAuthMiddleware, token=os.getenv("ACCESS_TOKEN"))
99
+
100
+
101
  @app.get('/healthz')
102
  async def healthz(request: Request):
103
  return JSONResponse({"healthz": True})
104
 
105
 
106
  @app.get('/cachedata/{file_name}')
107
+ async def cache_data(file_name: str, access_token: str):
108
+ if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
109
+ raise HTTPException(401, "the access token is not valid")
110
  cache_file = os.path.join(server_config.path.cache_root, file_name)
111
  lines = []
112
  for line in jsonlines.open(cache_file):
 
117
  @app.post('/endpoint')
118
  async def web_listening(request: Request):
119
  data = await request.json()
120
+ access_token: str = request.headers.get("Authorization")
121
+ if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
122
+ raise HTTPException(401, "the access token is not valid")
123
  msg_type = data['task']
124
 
125
  cache_file_popup_url = os.path.join(server_config.path.cache_root, 'popup_url.jsonl')