File size: 6,344 Bytes
2319518
 
 
 
 
 
 
 
3cac10f
2319518
 
 
 
 
 
 
 
6a9dc3a
2319518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522b6de
2319518
 
1e62994
2319518
 
 
 
 
 
 
 
 
 
6bca16b
 
f973cc6
 
2b56a6b
 
f973cc6
2319518
f973cc6
 
2319518
 
 
 
2baed60
2319518
 
 
 
2b56a6b
2319518
 
 
 
 
 
 
 
 
 
 
 
 
47208d1
522b6de
dbadc8c
6a9dc3a
522b6de
 
 
 
 
 
 
 
 
 
b27dd8f
6a9dc3a
47208d1
3cac10f
 
0024d65
 
 
 
 
522b6de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5779080
522b6de
 
 
 
2b17e2a
 
 
7b2782a
2b17e2a
 
 
2319518
 
 
 
6bca16b
b6a7ec0
2319518
 
 
6bca16b
2319518
 
 
2baed60
2319518
 
 
 
 
2ab9e99
2319518
 
 
 
 
 
1e62994
e9644a0
1e62994
 
e9644a0
1e62994
f973cc6
2319518
 
 
 
b6a7ec0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import json
import multiprocessing
import os
from pathlib import Path

import add_qwen_libs  # NOQA
import jsonlines
import uvicorn
from fastapi import FastAPI, Request, HTTPException, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles

from qwen_agent.log import logger
from qwen_agent.utils.utils import get_local_ip
from qwen_server.schema import GlobalConfig
from qwen_server.utils import extract_and_cache_document
from starlette.middleware.sessions import SessionMiddleware

# Read config
with open(Path(__file__).resolve().parent / 'server_config.json', 'r') as f:
    server_config = json.load(f)
    server_config = GlobalConfig(**server_config)

app = FastAPI()

logger.info(get_local_ip())
origins = [
    'http://127.0.0.1:' + str(server_config.server.workstation_port),
    'http://localhost:' + str(server_config.server.workstation_port),
    'http://0.0.0.0:' + str(server_config.server.workstation_port),
    'http://' + get_local_ip() + ':' +
    str(server_config.server.workstation_port),
]
access_token_file = os.path.join(server_config.path.cache_root, 'access_token.jsonl')
app.add_middleware(
    CORSMiddleware,
    # allow_origins=origins,
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

app.mount('/static',
          StaticFiles(directory=server_config.path.code_interpreter_ws),
          name='static')


def update_pop_url(data, cache_file_popup_url, access_token):
    new_line = {'url': data['url'], "access_token": access_token}
    lines = []
    for line in jsonlines.open(cache_file_popup_url):
        if line['access_token'] == access_token and line['url'] != data['url']:
            lines.append(line)
    lines.append(new_line)
    with jsonlines.open(cache_file_popup_url, mode='w') as writer:
        for new_line in lines:
            writer.write(new_line)
    response = 'Update URL'
    return response


def change_checkbox_state(text, cache_file, access_token):
    if not os.path.exists(cache_file):
        return {'result': 'no file'}
    lines = []
    for line in jsonlines.open(cache_file):
        if line['access_token'] == access_token and line['url'] == text[3:]:
            if line['checked']:
                line['checked'] = False
            else:
                line['checked'] = True
        lines.append(line)

    with jsonlines.open(cache_file, mode='w') as writer:
        for new_line in lines:
            writer.write(new_line)

    return {'result': 'changed'}


@app.middleware("http")
async def access_token_auth(request: Request, call_next):
    # print(f"Request URL path: {request.url}")
    access_token: str = request.headers.get("Authorization") or request.query_params.get("access_token") or request.session.get("access_token")
    is_valid = False
    if access_token:
        if os.getenv("ACCESS_TOKEN") == access_token:
            is_valid = True
        else:
            for line in jsonlines.open(access_token_file):
                if line['access_token'] == access_token:
                    is_valid = True
                    break
    if not is_valid:
        return Response(status_code=401, content="the token is not valid")
    request.session.setdefault("access_token", access_token)
    return await call_next(request)


@app.get('/healthz')
async def healthz(request: Request):
    return JSONResponse({"healthz": True})


@app.post('/token/add')
async def add_token(request: Request):
    access_token: str = request.headers.get("Authorization") or request.query_params.get("access_token") or request.session.get("access_token")
    if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
        return Response(status_code=401, content="the token is not valid")
    data = await request.json()
    lines = []
    for line in jsonlines.open(access_token_file):
        lines.append(line)
    lines.append(data)
    with jsonlines.open(access_token_file, mode='w') as writer:
        for new_line in lines:
            writer.write(new_line)
    return JSONResponse({"success": True})


@app.get('/cachedata/{file_name}')
async def cache_data(request: Request, file_name: str):
    access_token: str = request.headers.get("Authorization") or request.query_params.get("access_token") or request.session.get("access_token")
    if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
        return Response(status_code=401, content="the token is not valid")
    cache_file = os.path.join(server_config.path.cache_root, file_name)
    lines = []
    for line in jsonlines.open(cache_file):
        lines.append(line)
    return JSONResponse(lines)


@app.post('/endpoint')
async def web_listening(request: Request):
    data = await request.json()
    msg_type = data['task']
    access_token = request.session.get("access_token")
    cache_file_popup_url = os.path.join(server_config.path.cache_root, 'popup_url.jsonl')
    cache_file = os.path.join(server_config.path.cache_root, 'browse.jsonl')

    if msg_type == 'change_checkbox':
        rsp = change_checkbox_state(data['ckid'], cache_file, access_token)
    elif msg_type == 'cache':
        cache_obj = multiprocessing.Process(
            target=extract_and_cache_document,
            args=(data, cache_file, server_config.path.cache_root, access_token))
        cache_obj.start()
        # rsp = cache_data(data, cache_file)
        rsp = 'caching'
    elif msg_type == 'pop_url':
        # What a misleading name! pop_url actually means add_url. pop is referring to the pop_up ui.
        rsp = update_pop_url(data, cache_file_popup_url, access_token)
    else:
        raise NotImplementedError

    return JSONResponse(content=rsp)


import gradio as gr
# from qwen_server.workstation_server import demo as workstation_app
from qwen_server.assistant_server import demo as assistant_app

# app = gr.mount_gradio_app(app, workstation_app, path="/workstation")
app = gr.mount_gradio_app(app, assistant_app, path="/")
app.add_middleware(SessionMiddleware, secret_key=os.getenv("SECRET_KEY"), max_age=25200)
if __name__ == '__main__':
    uvicorn.run(app='database_server:app',
                host=server_config.server.server_host,
                port=server_config.server.fast_api_port,
                reload=False, workers=1)