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}