Spaces:
Running
Running
vlff李飞飞
commited on
Commit
•
f973cc6
1
Parent(s):
bb87713
use token
Browse files- browser_qwen/background.js +4 -4
- browser_qwen/background_副本.js +0 -58
- browser_qwen/img/popup.jpg +0 -0
- browser_qwen/img/popup.png +0 -0
- browser_qwen/manifest.json +1 -1
- browser_qwen/src/popup_副本.js +0 -65
- qwen_server/assistant_server.py +26 -27
- qwen_server/database_server.py +9 -5
browser_qwen/background.js
CHANGED
@@ -29,9 +29,9 @@ chrome.runtime.onMessage.addListener(async (msg, sender) => {
|
|
29 |
url = tabs[0].url;
|
30 |
console.log(url);
|
31 |
if (msg.data) {
|
32 |
-
chrome.storage.sync.get(['data'], function(result) {
|
33 |
chrome.storage.sync.set({ data: result.data }, function() {
|
34 |
-
send_data({ 'content' : msg.data, 'query': '', 'url':url, 'task':'cache', 'type':msg.type});
|
35 |
});
|
36 |
});
|
37 |
}
|
@@ -39,9 +39,9 @@ chrome.runtime.onMessage.addListener(async (msg, sender) => {
|
|
39 |
}
|
40 |
if (msg.flag == "open_popup_and_send_url_from_popup"){
|
41 |
if (msg.data) {
|
42 |
-
chrome.storage.sync.get(['data'], function(result) {
|
43 |
chrome.storage.sync.set({ data: result.data }, function() {
|
44 |
-
send_data({ 'url' : msg.data, 'task':'pop_url'});
|
45 |
});
|
46 |
});
|
47 |
}
|
|
|
29 |
url = tabs[0].url;
|
30 |
console.log(url);
|
31 |
if (msg.data) {
|
32 |
+
chrome.storage.sync.get(['data','access_token'], function(result) {
|
33 |
chrome.storage.sync.set({ data: result.data }, function() {
|
34 |
+
send_data({ 'content' : msg.data, 'query': '', 'url': result.access_token + "_" + url, 'task':'cache', 'type':msg.type});
|
35 |
});
|
36 |
});
|
37 |
}
|
|
|
39 |
}
|
40 |
if (msg.flag == "open_popup_and_send_url_from_popup"){
|
41 |
if (msg.data) {
|
42 |
+
chrome.storage.sync.get(['data','access_token'], function(result) {
|
43 |
chrome.storage.sync.set({ data: result.data }, function() {
|
44 |
+
send_data({ 'url' : result.access_token + "_" + msg.data, 'task':'pop_url'});
|
45 |
});
|
46 |
});
|
47 |
}
|
browser_qwen/background_副本.js
DELETED
@@ -1,58 +0,0 @@
|
|
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 = "http://"+result.database_host+":7866/endpoint";
|
8 |
-
} else {
|
9 |
-
database = "http://127.0.0.1:7866/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 |
-
|
25 |
-
chrome.runtime.onMessage.addListener(async (msg, sender) => {
|
26 |
-
if (msg.flag == "open_tab_and_cache_from_content"){
|
27 |
-
var url = "";
|
28 |
-
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
29 |
-
url = tabs[0].url;
|
30 |
-
console.log(url);
|
31 |
-
if (msg.data) {
|
32 |
-
chrome.storage.sync.get(['data'], function(result) {
|
33 |
-
chrome.storage.sync.set({ data: result.data }, function() {
|
34 |
-
send_data({ 'content' : msg.data, 'query': '', 'url':url, 'task':'cache', 'type':msg.type});
|
35 |
-
});
|
36 |
-
});
|
37 |
-
}
|
38 |
-
});
|
39 |
-
}
|
40 |
-
if (msg.flag == "open_popup_and_send_url_from_popup"){
|
41 |
-
if (msg.data) {
|
42 |
-
chrome.storage.sync.get(['data'], function(result) {
|
43 |
-
chrome.storage.sync.set({ data: result.data }, function() {
|
44 |
-
send_data({ 'url' : msg.data, 'task':'pop_url'});
|
45 |
-
});
|
46 |
-
});
|
47 |
-
}
|
48 |
-
}
|
49 |
-
// if (msg.flag == "set_addr"){
|
50 |
-
// if (msg.data) {
|
51 |
-
// chrome.storage.sync.get(['data'], function(result) {
|
52 |
-
// chrome.storage.sync.set({ data: result.data }, function() {
|
53 |
-
// send_data({ 'addr' : msg.data, 'task':'set_addr'});
|
54 |
-
// });
|
55 |
-
// });
|
56 |
-
// }
|
57 |
-
// }
|
58 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
browser_qwen/img/popup.jpg
DELETED
Binary file (6.01 kB)
|
|
browser_qwen/img/popup.png
ADDED
![]() |
browser_qwen/manifest.json
CHANGED
@@ -10,7 +10,7 @@
|
|
10 |
|
11 |
"action": {
|
12 |
"default_popup": "src/popup.html",
|
13 |
-
"default_icon": "img/popup.
|
14 |
"default_title": "BrowserLLMBB"
|
15 |
},
|
16 |
"permissions": [
|
|
|
10 |
|
11 |
"action": {
|
12 |
"default_popup": "src/popup.html",
|
13 |
+
"default_icon": "img/popup.png",
|
14 |
"default_title": "BrowserLLMBB"
|
15 |
},
|
16 |
"permissions": [
|
browser_qwen/src/popup_副本.js
DELETED
@@ -1,65 +0,0 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
4 |
-
// if (msg.flag == 'from_content'){
|
5 |
-
// console.log(msg.rsp);
|
6 |
-
// var sessionContainer = document.getElementById('session');
|
7 |
-
// sessionContainer.innerText = msg.rsp;
|
8 |
-
// sendResponse({ msg: 'Get!' });
|
9 |
-
// }
|
10 |
-
if (msg.flag === 'from_llm'){
|
11 |
-
// var sessionContainer = document.getElementById('session');
|
12 |
-
// // sessionContainer.innerHTML = msg.rsp;
|
13 |
-
// sessionContainer.innerText = msg.rsp;
|
14 |
-
sendResponse({ message: 'Get Response!' });
|
15 |
-
}
|
16 |
-
});
|
17 |
-
|
18 |
-
|
19 |
-
document.addEventListener('DOMContentLoaded', function() {
|
20 |
-
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
|
21 |
-
var currentUrl = tabs[0].url;
|
22 |
-
|
23 |
-
chrome.runtime.sendMessage({ data: currentUrl , close: true , flag: 'open_popup_and_send_url_from_popup'});
|
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 = "http://"+result.database_host+":7863/";
|
33 |
-
} else {
|
34 |
-
popup_url = "http://127.0.0.1:7863/";
|
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 |
-
|
47 |
-
// fetch('../config_host.json')
|
48 |
-
// .then(response => response.json())
|
49 |
-
// .then(data => {
|
50 |
-
// console.log(data);
|
51 |
-
// popup_url = "http://"+data.database_host+":"+data.app_in_browser_port+"/";
|
52 |
-
// console.log(popup_url);
|
53 |
-
// })
|
54 |
-
// .catch(error => console.error('Error:', error));
|
55 |
-
})
|
56 |
-
|
57 |
-
document.getElementById('set_addr').addEventListener('click', function() {
|
58 |
-
var addr = document.getElementById('addr').value;
|
59 |
-
// save config
|
60 |
-
chrome.storage.local.set({database_host: addr}, function() {
|
61 |
-
console.log('database_host is set to ' + addr);
|
62 |
-
// chrome.runtime.sendMessage({ data: addr , close: true , flag: 'set_addr'});
|
63 |
-
document.getElementById('addr').value = '';
|
64 |
-
});
|
65 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qwen_server/assistant_server.py
CHANGED
@@ -26,7 +26,7 @@ mem = Memory(llm=llm, stream=False)
|
|
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 |
|
31 |
with open(Path(__file__).resolve().parent / 'css/main.css', 'r') as f:
|
32 |
css = f.read()
|
@@ -50,33 +50,32 @@ def rm_text(history):
|
|
50 |
|
51 |
|
52 |
def add_file(history, file):
|
53 |
-
history = history + [((file.name,
|
54 |
return history
|
55 |
|
56 |
|
57 |
-
def set_page_url():
|
58 |
-
lines = []
|
59 |
assert os.path.exists(cache_file_popup_url)
|
60 |
for line in jsonlines.open(cache_file_popup_url):
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
|
66 |
def initialize(request: gr.Request):
|
67 |
-
|
68 |
-
|
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:
|
81 |
yield history
|
82 |
else:
|
@@ -86,7 +85,7 @@ def bot(history):
|
|
86 |
gr.Info("Please add this page to LLMBB's Reading List first!")
|
87 |
else:
|
88 |
for line in jsonlines.open(cache_file):
|
89 |
-
if line['url'] == PAGE_URL[-1]:
|
90 |
now_page = line
|
91 |
|
92 |
if not now_page:
|
@@ -120,7 +119,7 @@ def bot(history):
|
|
120 |
now_page['session'] = history
|
121 |
lines = []
|
122 |
for line in jsonlines.open(cache_file):
|
123 |
-
if line['url'] != PAGE_URL[-1]:
|
124 |
lines.append(line)
|
125 |
|
126 |
lines.append(now_page)
|
@@ -129,13 +128,13 @@ def bot(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 LLMBB's Reading List first!")
|
136 |
return []
|
137 |
for line in jsonlines.open(cache_file):
|
138 |
-
if line['url'] == PAGE_URL[-1]:
|
139 |
now_page = line
|
140 |
if not now_page:
|
141 |
gr.Info("Please add this page to LLMBB's Reading List first!")
|
@@ -146,13 +145,13 @@ def load_history_session(history):
|
|
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
|
153 |
lines = []
|
154 |
for line in jsonlines.open(cache_file):
|
155 |
-
if line['url'] == PAGE_URL[-1]:
|
156 |
now_page = line
|
157 |
else:
|
158 |
lines.append(line)
|
@@ -190,7 +189,7 @@ with gr.Blocks(css=css, theme='soft') as demo:
|
|
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 |
|
196 |
# txt_msg_bt = smt_bt.click(add_text, [chatbot, txt], [chatbot, txt],
|
@@ -199,16 +198,16 @@ with gr.Blocks(css=css, theme='soft') as demo:
|
|
199 |
# None, [txt],
|
200 |
# queue=False)
|
201 |
|
202 |
-
clr_bt.click(clear_session,
|
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],
|
207 |
queue=False)
|
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)
|
|
|
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 |
|
31 |
with open(Path(__file__).resolve().parent / 'css/main.css', 'r') as f:
|
32 |
css = f.read()
|
|
|
50 |
|
51 |
|
52 |
def add_file(history, file):
|
53 |
+
history = history + [((file.name,), None)]
|
54 |
return history
|
55 |
|
56 |
|
57 |
+
def set_page_url(access_token):
|
58 |
+
lines = {access_token: []}
|
59 |
assert os.path.exists(cache_file_popup_url)
|
60 |
for line in jsonlines.open(cache_file_popup_url):
|
61 |
+
_access_token = cache_file_popup_url.split("_")[0]
|
62 |
+
if _access_token not in lines:
|
63 |
+
lines[_access_token] = []
|
64 |
+
lines[_access_token].append(line)
|
65 |
+
if access_token not in PAGE_URL:
|
66 |
+
PAGE_URL[access_token] = []
|
67 |
+
PAGE_URL[access_token].append(lines[access_token][-1]['url'])
|
68 |
+
logger.info('The current access page is: ' + PAGE_URL[access_token][-1])
|
69 |
|
70 |
|
71 |
def initialize(request: gr.Request):
|
72 |
+
access_token = request.session["access_token"] or request.query_params["access_token"]
|
73 |
+
set_page_url(access_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
return access_token
|
75 |
|
76 |
|
77 |
+
def bot(history, access_token):
|
78 |
+
set_page_url(access_token)
|
79 |
if not history:
|
80 |
yield history
|
81 |
else:
|
|
|
85 |
gr.Info("Please add this page to LLMBB's Reading List first!")
|
86 |
else:
|
87 |
for line in jsonlines.open(cache_file):
|
88 |
+
if line['url'] == PAGE_URL[access_token][-1]:
|
89 |
now_page = line
|
90 |
|
91 |
if not now_page:
|
|
|
119 |
now_page['session'] = history
|
120 |
lines = []
|
121 |
for line in jsonlines.open(cache_file):
|
122 |
+
if line['url'] != PAGE_URL[access_token][-1]:
|
123 |
lines.append(line)
|
124 |
|
125 |
lines.append(now_page)
|
|
|
128 |
writer.write(new_line)
|
129 |
|
130 |
|
131 |
+
def load_history_session(history, access_token):
|
132 |
now_page = None
|
133 |
if not os.path.exists(cache_file):
|
134 |
gr.Info("Please add this page to LLMBB's Reading List first!")
|
135 |
return []
|
136 |
for line in jsonlines.open(cache_file):
|
137 |
+
if line['url'] == PAGE_URL[access_token][-1]:
|
138 |
now_page = line
|
139 |
if not now_page:
|
140 |
gr.Info("Please add this page to LLMBB's Reading List first!")
|
|
|
145 |
return now_page['session']
|
146 |
|
147 |
|
148 |
+
def clear_session(access_token):
|
149 |
if not os.path.exists(cache_file):
|
150 |
return None
|
151 |
now_page = None
|
152 |
lines = []
|
153 |
for line in jsonlines.open(cache_file):
|
154 |
+
if line['url'] == PAGE_URL[access_token][-1]:
|
155 |
now_page = line
|
156 |
else:
|
157 |
lines.append(line)
|
|
|
189 |
re_bt = gr.Button('🔁', elem_classes='bt_small_font')
|
190 |
|
191 |
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt],
|
192 |
+
queue=False).then(bot, [chatbot, access_token], chatbot)
|
193 |
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
194 |
|
195 |
# txt_msg_bt = smt_bt.click(add_text, [chatbot, txt], [chatbot, txt],
|
|
|
198 |
# None, [txt],
|
199 |
# queue=False)
|
200 |
|
201 |
+
clr_bt.click(clear_session, [access_token], chatbot, queue=False)
|
202 |
re_txt_msg = re_bt.click(rm_text, [chatbot], [chatbot, txt],
|
203 |
+
queue=False).then(bot, [chatbot, access_token], chatbot)
|
204 |
re_txt_msg.then(lambda: gr.update(interactive=True),
|
205 |
None, [txt],
|
206 |
queue=False)
|
207 |
|
208 |
stop_bt.click(None, None, None, cancels=[txt_msg, re_txt_msg], queue=False)
|
209 |
|
210 |
+
demo.load(initialize, [], [access_token]).then(load_history_session, [chatbot, access_token], chatbot)
|
211 |
demo.queue()
|
212 |
|
213 |
# 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
@@ -47,11 +47,15 @@ app.mount('/static',
|
|
47 |
|
48 |
|
49 |
def update_pop_url(data, cache_file_popup_url):
|
|
|
50 |
new_line = {'url': data['url']}
|
51 |
-
|
|
|
|
|
|
|
52 |
with jsonlines.open(cache_file_popup_url, mode='w') as writer:
|
53 |
-
|
54 |
-
|
55 |
response = 'Update URL'
|
56 |
return response
|
57 |
|
@@ -91,7 +95,7 @@ async def healthz(request: Request):
|
|
91 |
|
92 |
|
93 |
@app.get('/cachedata/{file_name}')
|
94 |
-
async def cache_data(file_name: str
|
95 |
cache_file = os.path.join(server_config.path.cache_root, file_name)
|
96 |
lines = []
|
97 |
for line in jsonlines.open(cache_file):
|
@@ -131,7 +135,7 @@ from qwen_server.assistant_server import demo as assistant_app
|
|
131 |
|
132 |
# app = gr.mount_gradio_app(app, workstation_app, path="/workstation")
|
133 |
app = gr.mount_gradio_app(app, assistant_app, path="/")
|
134 |
-
app.add_middleware(SessionMiddleware, secret_key=
|
135 |
if __name__ == '__main__':
|
136 |
uvicorn.run(app='database_server:app',
|
137 |
host=server_config.server.server_host,
|
|
|
47 |
|
48 |
|
49 |
def update_pop_url(data, cache_file_popup_url):
|
50 |
+
_access_token = data['url'].split("_")[0]
|
51 |
new_line = {'url': data['url']}
|
52 |
+
lines = []
|
53 |
+
for line in jsonlines.open(cache_file_popup_url):
|
54 |
+
lines.append(line)
|
55 |
+
lines.append(new_line)
|
56 |
with jsonlines.open(cache_file_popup_url, mode='w') as writer:
|
57 |
+
for new_line in lines:
|
58 |
+
writer.write(new_line)
|
59 |
response = 'Update URL'
|
60 |
return response
|
61 |
|
|
|
95 |
|
96 |
|
97 |
@app.get('/cachedata/{file_name}')
|
98 |
+
async def cache_data(file_name: str):
|
99 |
cache_file = os.path.join(server_config.path.cache_root, file_name)
|
100 |
lines = []
|
101 |
for line in jsonlines.open(cache_file):
|
|
|
135 |
|
136 |
# app = gr.mount_gradio_app(app, workstation_app, path="/workstation")
|
137 |
app = gr.mount_gradio_app(app, assistant_app, path="/")
|
138 |
+
app.add_middleware(SessionMiddleware, secret_key=os.getenv("SECRET_KEY"), max_age=25200)
|
139 |
if __name__ == '__main__':
|
140 |
uvicorn.run(app='database_server:app',
|
141 |
host=server_config.server.server_host,
|