File size: 1,716 Bytes
30ef6e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b16615c
30ef6e4
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
import os
import sys
import time
import signal
import io

from fastapi import FastAPI, Request, status, Form, UploadFile
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse, StreamingResponse
import fn
import gradio as gr
from app import demo

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

gr.mount_gradio_app(app, demo, path="/gradio")

@app.post("/ddg")
async def api_ddg(args: dict):
    try:
        results = fn.ddg(args['query'], args['max_results'])

        return {"results": results}
    except Exception as e:
        return {"error": str(e)}

@app.post("/bs4")
async def api_bs4(args: dict):
    try:
        text = fn.bs4(args['url'])

        return {"text": text}
    except Exception as e:
        return {"error": str(e)}

@app.post("/reload")
async def api_reload(args: dict):
    fn.load_vectors()
    return {'status': 0}

@app.post("/upload")
async def api_upload(args: dict):
    fn.upload(args['name'], args['filename'], args['content'])
    return {'status': 0}

@app.post("/delete")
async def api_delete(args: dict):
    fn.delete(args['name'], args['filename'])
    return {'status': 0}

@app.post("/embedding")
async def api_embedding(args: dict):
    fn.embedding()
    fn.load_vectors()
    return {'status': 0}

@app.post("/search")
async def api_search(args: dict):
    result = fn.search(args['name'], args['query'], int(args['num']) if 'num' in args else 3)
    return {'result': result}