CoolFace
Apppublic

GenesisEdge/g4f2

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py39 linesDownload Raw Back to root
1from fastapi import FastAPI, Request2from fastapi.responses import RedirectResponse3import demo4import g4f.api5import g4f.Provider6from g4f.Provider import BackendApi7 8BackendApi.working = True9BackendApi.url = demo.url10BackendApi.headers = demo.headers11 12g4f.Provider.__map__["Feature"] = g4f.Provider.BackendApi\13 14custom_prefix = "/sk-Qs8xHhxForP7eqoYdtVCI4kJEwMThiGOej4jFncxH3bkpBCY"15 16def create_app():17    g4f.debug.logging = True18    g4f.api.AppConfig.gui = False19    g4f.api.AppConfig.demo = False20 21    app = FastAPI()22 23    # 添加路径重写中间件24    @app.middleware("https")25    async def rewrite_path(request: Request, call_next):26        if request.url.path.startswith(custom_prefix + "/"):27            # 移除前缀并保留后续路径28            new_path = request.url.path[len(custom_prefix):]29            request.scope['path'] = new_path  # 修改请求路径30        return await call_next(request)31 32    api = g4f.api.Api(app)33    api.register_routes()34    api.register_authorization()35    api.register_validation_exception_handler()36 37    return app38 39app = create_app()