File size: 1,889 Bytes
1b57e39
5ef0f8d
e97be0e
c1faac1
 
2ba1434
546fbbe
f6bffda
2ba1434
5ef0f8d
 
040cfa1
1392287
040cfa1
c1faac1
 
 
035141c
 
c1faac1
 
 
 
2c86f91
2ba1434
5ef0f8d
 
 
1392287
040cfa1
9829679
c1faac1
 
 
4e54efb
5ef0f8d
 
f6bffda
e97be0e
 
2ba1434
 
e97be0e
 
 
 
2ba1434
 
e97be0e
 
 
 
 
 
 
035141c
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
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.")

# Initialize global dependencies
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")

# INTERNAL ROUTE TO RETRIEVE PROMPT TEMPLATES FOR PRIVATE COMPUTE


@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")