Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,50 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import List, Optional
|
| 4 |
from llama_cpp import Llama
|
| 5 |
from fastapi.responses import PlainTextResponse, JSONResponse
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
import os
|
| 8 |
import time
|
| 9 |
import uuid
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
app = FastAPI()
|
|
|
|
| 12 |
|
| 13 |
llm = None
|
| 14 |
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request, Response
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import List, Optional
|
| 4 |
from llama_cpp import Llama
|
| 5 |
from fastapi.responses import PlainTextResponse, JSONResponse
|
| 6 |
+
from starlette.middleware.base import BaseHTTPMiddleware
|
| 7 |
|
| 8 |
+
|
| 9 |
+
import logging
|
| 10 |
+
import json
|
| 11 |
import os
|
| 12 |
import time
|
| 13 |
import uuid
|
| 14 |
|
| 15 |
+
# Configure logging
|
| 16 |
+
logging.basicConfig(level=logging.INFO)
|
| 17 |
+
logger = logging.getLogger("api_logger")
|
| 18 |
+
|
| 19 |
+
class LoggingMiddleware(BaseHTTPMiddleware):
|
| 20 |
+
async def dispatch(self, request: Request, call_next):
|
| 21 |
+
# Read request body (must be buffered manually)
|
| 22 |
+
body = await request.body()
|
| 23 |
+
logger.info(f"REQUEST: {request.method} {request.url}\nBody: {body.decode('utf-8')}")
|
| 24 |
+
|
| 25 |
+
# Rebuild the request with body for downstream handlers
|
| 26 |
+
request = Request(request.scope, receive=lambda: {'type': 'http.request', 'body': body})
|
| 27 |
+
|
| 28 |
+
# Process the response
|
| 29 |
+
response = await call_next(request)
|
| 30 |
+
response_body = b""
|
| 31 |
+
async for chunk in response.body_iterator:
|
| 32 |
+
response_body += chunk
|
| 33 |
+
|
| 34 |
+
# Log response body and status code
|
| 35 |
+
logger.info(f"RESPONSE: Status {response.status_code}\nBody: {response_body.decode('utf-8')}")
|
| 36 |
+
|
| 37 |
+
# Rebuild response to preserve original functionality
|
| 38 |
+
return Response(
|
| 39 |
+
content=response_body,
|
| 40 |
+
status_code=response.status_code,
|
| 41 |
+
headers=dict(response.headers),
|
| 42 |
+
media_type=response.media_type
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# FastAPI app with middleware
|
| 46 |
app = FastAPI()
|
| 47 |
+
app.add_middleware(LoggingMiddleware)
|
| 48 |
|
| 49 |
llm = None
|
| 50 |
|