File size: 2,340 Bytes
2c89b88
 
 
 
724ff38
 
 
a1011ad
2c89b88
 
 
5c8f5bf
 
0f54ea3
2c89b88
 
 
 
fe15b8d
 
 
5c8f5bf
 
 
 
 
 
 
 
 
 
 
2c89b88
 
 
 
15ab6a4
534624d
15ab6a4
 
 
 
 
 
 
 
f746ed1
 
15ab6a4
 
2c89b88
 
 
 
b5edbe6
 
a1011ad
b5edbe6
fe15b8d
2c89b88
 
38c555b
b5edbe6
2c89b88
 
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
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from routers.auth import router as auth_router
from routers.analysis import router as analysis_router
from routers.history import router as history_router
from routers.product import router as product_router
import os
import uvicorn
from pathlib import Path
import tensorflow as tf
import tensorflow_hub as hub
from env import PORT


# Define the templates directory
templates = Jinja2Templates(directory="templates")

app = FastAPI()

# Suppress TensorFlow warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # 0=all, 1=no INFO, 2=no WARNING, 3=no ERROR

# Store the model as a state variable in the app
@app.on_event("startup")
async def startup_event():
    # Load model once during startup
    print("Loading TensorFlow model...")
    app.state.detector = hub.load("https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1").signatures['default']
    print("TensorFlow model loaded successfully!")

@app.get("/")
def read_root():
    return RedirectResponse("/api")

# print every request data for request using middleware

@app.middleware("http")
async def log_requests(request: Request, call_next):
    # Store the body content before sending to the next handler
    body_content = await request.body()
    # Create a new request with the consumed body
    request._body = body_content
    response = await call_next(request)
    print(f"Request: {request.method} {request.url}")
    # print(f"Data: {body_content}"[:100])
    # print(f"Headers: {request.headers}")
    return response

@app.get("/api", response_class=HTMLResponse)
async def read_api(request: Request):
    return templates.TemplateResponse("api_docs.html", {"request": request})

app.include_router(analysis_router, prefix="/api/analyze")
app.include_router(auth_router, prefix="/api/auth")
app.include_router(product_router, prefix="/api/product")
app.include_router(history_router, prefix="/api/history")

app.add_event_handler("startup", lambda: print("Starting up..."))

# To run the FastAPI app, use the command: uvicorn main:app --reload
if __name__ == "__main__":
    # run using fastapi directly for development purposes
    uvicorn.run(app, host="0.0.0.0", port=PORT)