from fastapi import FastAPI, Request import sys import time import uvicorn import structlog import logging from .routers import full, list_speakers,voice_api import nltk from fastapi.middleware.cors import CORSMiddleware nltk.download('punkt') app=FastAPI() origins = [ "http://localhost.tiangolo.com", "https://localhost.tiangolo.com", "http://localhost", "http://localhost:3000", "https://2c6b-182-186-105-95.ngrok-free.app", ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Setup global logger config structlog.configure( processors=[ # If log level is too low, abort pipeline and throw away log entry. structlog.stdlib.filter_by_level, # Add the name of the logger to event dict. # structlog.stdlib.add_logger_name, # Add log level to event dict. structlog.stdlib.add_log_level, # Perform %-style formatting. structlog.stdlib.PositionalArgumentsFormatter(), # Add a timestamp in ISO 8601 format. structlog.processors.TimeStamper(fmt="iso"), # If the "stack_info" key in the event dict is true, remove it and # render the current stack trace in the "stack" key. structlog.processors.StackInfoRenderer(), # If the "exc_info" key in the event dict is either true or a # sys.exc_info() tuple, remove "exc_info" and render the exception # with traceback into the "exception" key. structlog.processors.format_exc_info, # If some value is in bytes, decode it to a unicode str. structlog.processors.UnicodeDecoder(), # Add callsite parameters. # structlog.processors.CallsiteParameterAdder( # { # structlog.processors.CallsiteParameter.FILENAME, # structlog.processors.CallsiteParameter.FUNC_NAME, # structlog.processors.CallsiteParameter.LINENO, # } # ), # Render the final event dict as JSON. structlog.processors.JSONRenderer() ], # `wrapper_class` is the bound logger that you get back from # get_logger(). This one imitates the API of `logging.Logger`. wrapper_class=structlog.stdlib.BoundLogger, # `logger_factory` is used to create wrapped loggers that are used for # OUTPUT. This one returns a `logging.Logger`. The final value (a JSON # string) from the final processor (`JSONRenderer`) will be passed to # the method of the same name as that you've called on the bound logger. logger_factory=structlog.stdlib.LoggerFactory(), # Effectively freeze configuration after creating the first bound # logger. cache_logger_on_first_use=True, ) logging.basicConfig( format="%(message)s", stream=sys.stdout, level=logging.DEBUG, ) log = structlog.get_logger(__name__) # app = FastAPI() @app.middleware("http") async def log_requests(request: Request, call_next): start_time = time.time() response = await call_next(request) process_time = (time.time() - start_time) * 1000 formatted_process_time = '{0:.2f}'.format(process_time) # These fields should be outside the event log.info(f"path={request.url.path} completed_in={formatted_process_time}ms status_code={response.status_code}") return response app.include_router(full.router) app.include_router(list_speakers.router) app.include_router(voice_api.router) if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)