2023-03-17 15:55:15 +00:00
|
|
|
import re
|
2023-04-08 05:10:16 +00:00
|
|
|
from multiprocessing import Process
|
2023-04-07 12:07:19 +00:00
|
|
|
from tempfile import NamedTemporaryFile
|
2023-04-08 05:10:16 +00:00
|
|
|
from typing import List, TypedDict
|
2023-03-17 15:55:15 +00:00
|
|
|
|
2023-04-03 07:43:34 +00:00
|
|
|
import uvicorn
|
2023-04-07 12:07:19 +00:00
|
|
|
from fastapi import FastAPI, Request, UploadFile
|
|
|
|
from fastapi.responses import HTMLResponse
|
2023-04-05 03:37:20 +00:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2023-04-08 05:10:16 +00:00
|
|
|
from api.container import agent_manager, file_handler, reload_dirs, templates, uploader
|
|
|
|
from api.worker import get_task_result, start_worker, task_execute
|
2023-04-03 07:43:34 +00:00
|
|
|
from env import settings
|
2023-03-17 15:55:15 +00:00
|
|
|
|
|
|
|
app = FastAPI()
|
2023-03-18 12:42:14 +00:00
|
|
|
|
2023-04-07 12:07:19 +00:00
|
|
|
app.mount("/static", StaticFiles(directory=uploader.path), name="static")
|
|
|
|
|
2023-03-17 15:55:15 +00:00
|
|
|
|
2023-04-07 12:07:19 +00:00
|
|
|
class ExecuteRequest(BaseModel):
|
|
|
|
session: str
|
|
|
|
prompt: str
|
2023-03-18 06:05:02 +00:00
|
|
|
files: List[str]
|
2023-03-17 15:55:15 +00:00
|
|
|
|
|
|
|
|
2023-04-07 12:07:19 +00:00
|
|
|
class ExecuteResponse(TypedDict):
|
|
|
|
answer: str
|
2023-03-18 06:05:02 +00:00
|
|
|
files: List[str]
|
2023-03-17 15:55:15 +00:00
|
|
|
|
|
|
|
|
2023-04-07 12:07:19 +00:00
|
|
|
@app.get("/", response_class=HTMLResponse)
|
|
|
|
async def index(request: Request):
|
|
|
|
return templates.TemplateResponse("index.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/dashboard", response_class=HTMLResponse)
|
|
|
|
async def dashboard(request: Request):
|
|
|
|
return templates.TemplateResponse("dashboard.html", {"request": request})
|
2023-03-17 15:55:15 +00:00
|
|
|
|
|
|
|
|
2023-04-07 12:07:19 +00:00
|
|
|
@app.post("/upload")
|
|
|
|
async def create_upload_file(files: List[UploadFile]):
|
|
|
|
urls = []
|
|
|
|
for file in files:
|
|
|
|
extension = "." + file.filename.split(".")[-1]
|
|
|
|
with NamedTemporaryFile(suffix=extension) as tmp_file:
|
|
|
|
tmp_file.write(file.file.read())
|
|
|
|
tmp_file.flush()
|
|
|
|
urls.append(uploader.upload(tmp_file.name))
|
|
|
|
return {"urls": urls}
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/execute")
|
|
|
|
async def execute(request: ExecuteRequest) -> ExecuteResponse:
|
|
|
|
query = request.prompt
|
2023-03-17 15:55:15 +00:00
|
|
|
files = request.files
|
2023-04-07 12:07:19 +00:00
|
|
|
session = request.session
|
2023-03-17 15:55:15 +00:00
|
|
|
|
2023-04-08 05:10:16 +00:00
|
|
|
executor = agent_manager.create_executor(session)
|
2023-03-21 12:20:15 +00:00
|
|
|
|
|
|
|
promptedQuery = "\n".join([file_handler.handle(file) for file in files])
|
2023-03-18 06:05:02 +00:00
|
|
|
promptedQuery += query
|
2023-03-17 15:55:15 +00:00
|
|
|
|
|
|
|
try:
|
2023-03-21 12:20:15 +00:00
|
|
|
res = executor({"input": promptedQuery})
|
2023-03-17 15:55:15 +00:00
|
|
|
except Exception as e:
|
2023-04-07 12:07:19 +00:00
|
|
|
return {"answer": str(e), "files": []}
|
2023-04-05 03:37:20 +00:00
|
|
|
|
2023-04-08 05:10:16 +00:00
|
|
|
files = re.findall(r"\[file://\S*\]", res["output"])
|
2023-04-07 12:07:19 +00:00
|
|
|
files = [file[1:-1] for file in files]
|
2023-03-17 15:55:15 +00:00
|
|
|
|
|
|
|
return {
|
2023-04-07 12:07:19 +00:00
|
|
|
"answer": res["output"],
|
2023-04-05 03:37:20 +00:00
|
|
|
"files": [uploader.upload(file) for file in files],
|
2023-03-17 15:55:15 +00:00
|
|
|
}
|
2023-03-23 07:33:45 +00:00
|
|
|
|
|
|
|
|
2023-04-08 05:10:16 +00:00
|
|
|
@app.post("/api/execute/async")
|
|
|
|
async def execute_async(request: ExecuteRequest):
|
|
|
|
query = request.prompt
|
|
|
|
files = request.files
|
|
|
|
session = request.session
|
|
|
|
|
|
|
|
promptedQuery = "\n".join([file_handler.handle(file) for file in files])
|
|
|
|
promptedQuery += query
|
|
|
|
|
|
|
|
execution = task_execute.delay(session, promptedQuery)
|
|
|
|
return {"id": execution.id}
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/execute/async/{execution_id}")
|
|
|
|
async def execute_async(execution_id: str):
|
|
|
|
execution = get_task_result(execution_id)
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
if execution.status == "SUCCESS" and execution.result:
|
|
|
|
output = execution.result.get("output", "")
|
|
|
|
files = re.findall(r"\[file://\S*\]", output)
|
|
|
|
files = [file[1:-1] for file in files]
|
|
|
|
result = {
|
|
|
|
"answer": output,
|
|
|
|
"files": [uploader.upload(file) for file in files],
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"status": execution.status,
|
|
|
|
"info": execution.info,
|
|
|
|
"result": result,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-23 07:33:45 +00:00
|
|
|
def serve():
|
2023-04-08 05:10:16 +00:00
|
|
|
p = Process(target=start_worker, args=[])
|
|
|
|
p.start()
|
2023-04-07 02:11:24 +00:00
|
|
|
uvicorn.run("api.main:app", host="0.0.0.0", port=settings["EVAL_PORT"])
|
2023-04-07 12:07:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dev():
|
2023-04-08 05:10:16 +00:00
|
|
|
p = Process(target=start_worker, args=[])
|
|
|
|
p.start()
|
2023-04-07 12:07:19 +00:00
|
|
|
uvicorn.run(
|
|
|
|
"api.main:app",
|
|
|
|
host="0.0.0.0",
|
|
|
|
port=settings["EVAL_PORT"],
|
|
|
|
reload=True,
|
2023-04-08 05:10:16 +00:00
|
|
|
reload_dirs=reload_dirs,
|
2023-04-07 12:07:19 +00:00
|
|
|
)
|