| import logging |
| from dotenv import load_dotenv |
| from jinja2 import Environment, TemplateNotFound |
| import warnings |
| import os |
| from fastapi import Depends, FastAPI, Response |
| from fastapi.staticfiles import StaticFiles |
| import api.solutions |
| from dependencies import get_prompt_templates, init_dependencies |
| import api.docs |
| import api.requirements |
| from schemas import * |
| from fastapi.middleware.cors import CORSMiddleware |
|
|
| load_dotenv() |
|
|
| logging.basicConfig( |
| level=logging.DEBUG if (os.environ.get( |
| "DEBUG_LOG", "0") == "1") else logging.INFO, |
| format='[%(asctime)s][%(levelname)s][%(filename)s:%(lineno)d]: %(message)s', |
| datefmt='%Y-%m-%d %H:%M:%S' |
| ) |
|
|
| logging.info(f"Set `DEBUG_LOG` env var to 1 to enable debug logging.") |
|
|
| |
| init_dependencies() |
|
|
| warnings.filterwarnings("ignore") |
|
|
| app = FastAPI(title="Requirements Extractor", docs_url="/apidocs") |
| app.add_middleware(CORSMiddleware, allow_credentials=True, allow_headers=[ |
| "*"], allow_methods=["*"], allow_origins=["*"]) |
|
|
|
|
| app.include_router(api.docs.router, prefix="/docs") |
| app.include_router(api.requirements.router, prefix="/requirements") |
| app.include_router(api.solutions.router, prefix="/solutions") |
|
|
| |
|
|
|
|
| @app.get("/prompt/{task}", include_in_schema=True) |
| async def retrieve_prompt(task: str, prompt_env: Environment = Depends(get_prompt_templates)): |
| """Retrieves a prompt for client-side private inference""" |
| try: |
| logging.debug( |
| f"Retrieving template for on device private task {task}.") |
| prompt, filename, _ = prompt_env.loader.get_source( |
| prompt_env, f"private/{task}.txt") |
| return prompt |
| except TemplateNotFound as _: |
| return Response(content="", status_code=404) |
|
|
|
|
| app.mount("/", StaticFiles(directory="static", html=True), name="static") |
|
|