replace loadenv by pydantic-settings
This commit is contained in:
BIN
server/src/app/__pycache__/config.cpython-311.pyc
Normal file
BIN
server/src/app/__pycache__/config.cpython-311.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
19
server/src/app/config.py
Normal file
19
server/src/app/config.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
class Settings(BaseSettings):
|
||||
# Database
|
||||
DATABASE_URI: str
|
||||
|
||||
# Language model
|
||||
LANGUAGE_MODEL_API: str = "http://localhost:8080/v1"
|
||||
LANGUAGE_MODEL_NAME: str = "SmolLM3-Q4_K_M.gguf"
|
||||
|
||||
# Security
|
||||
ORIGIN: str
|
||||
SECRET_KEY : str
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
|
||||
settings = Settings()
|
||||
@@ -1,13 +1,8 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from src.app.config import settings
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
|
||||
load_dotenv()
|
||||
|
||||
database_uri=os.environ.get("DATABASE_URI")
|
||||
|
||||
connect_args = {"check_same_thread": False}
|
||||
engine = create_engine(database_uri, echo=False, connect_args=connect_args)
|
||||
engine = create_engine(settings.DATABASE_URI, echo=False, connect_args=connect_args)
|
||||
|
||||
def create_db_and_tables():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from src.app.config import settings
|
||||
from contextlib import asynccontextmanager
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
@@ -18,8 +17,6 @@ from .api import router
|
||||
#Test
|
||||
from src.app.faker_seed import faker
|
||||
|
||||
load_dotenv()
|
||||
|
||||
#TODO : alternative @app.on_event("startup") ?
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
@@ -31,7 +28,7 @@ async def lifespan(app: FastAPI):
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
app.include_router(router)
|
||||
|
||||
origin = os.environ.get("ORIGIN")
|
||||
origin = settings.ORIGIN
|
||||
origins = [origin]
|
||||
|
||||
app.add_middleware(
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,4 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from src.app.config import settings
|
||||
from datetime import timedelta, datetime, timezone
|
||||
from typing import Annotated
|
||||
from pydantic import BaseModel
|
||||
@@ -19,12 +17,9 @@ from argon2.exceptions import (
|
||||
from src.app.models.user import User
|
||||
from src.app.data.user import get_user
|
||||
|
||||
load_dotenv()
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/token")
|
||||
password_hasher = PasswordHasher()
|
||||
|
||||
secret_key = os.environ.get("SECRET_SIGN")
|
||||
algorithm = "HS256"
|
||||
access_token_expire_minutes = 10080
|
||||
|
||||
@@ -56,7 +51,7 @@ def create_access_token(data: dict):
|
||||
expire = datetime.now(timezone.utc) + timedelta(minutes=access_token_expire_minutes)
|
||||
to_encode = data.copy()
|
||||
to_encode.update({"exp": expire})
|
||||
encoded_jwt = jwt.encode(to_encode, secret_key, algorithm=algorithm)
|
||||
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=algorithm)
|
||||
return encoded_jwt
|
||||
|
||||
async def hash_password(password: str) -> str:
|
||||
@@ -69,7 +64,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> Use
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
try:
|
||||
payload = jwt.decode(token, secret_key, algorithms=[algorithm])
|
||||
payload = jwt.decode(token, settings.SECRET_KEY, algorithm=algorithm)
|
||||
username = payload.get("sub")
|
||||
if username is None:
|
||||
raise credentials_exception
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
import os
|
||||
import spacy
|
||||
|
||||
from src.app.config import settings
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
from src.app.models.knowledge import Knowledge
|
||||
|
||||
language_model_api=os.environ.get("LANGUAGE_MODEL_API")
|
||||
model_name=os.environ.get("LANGUAGE_MODEL_NAME")
|
||||
|
||||
client = OpenAI(
|
||||
base_url=language_model_api,
|
||||
base_url=settings.LANGUAGE_MODEL_API,
|
||||
api_key = "sk-no-key-required"
|
||||
)
|
||||
|
||||
@@ -23,7 +19,7 @@ def questions_generation(knowledge: Knowledge):
|
||||
|
||||
#SLM processing
|
||||
response = client.responses.create(
|
||||
model=model_name,
|
||||
model=settings.LANGUAGE_MODEL_NAME,
|
||||
input=[
|
||||
{"role": "system", "content": "Question Generation"},
|
||||
{"role": "user", "content": prompt}],
|
||||
|
||||
Reference in New Issue
Block a user