replace loadenv by pydantic-settings
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
DATABASE_URI="sqlite:///database.db"
|
DATABASE_URI="sqlite:///database.db"
|
||||||
LANGUAGE_MODEL_API="http://localhost:8080/v1"
|
LANGUAGE_MODEL_API="http://localhost:8080/v1"
|
||||||
MODEL_NAME="SmolLM3-Q4_K_M.gguf"
|
LANGUAGE_MODEL_NAME="SmolLM3-Q4_K_M.gguf"
|
||||||
ORIGIN="http://localhost:5173"
|
ORIGIN="http://localhost:5173"
|
||||||
SECRET_SIGN="xxxx" #generate secure random secret key: openssl rand -hex 32
|
SECRET_KEY="xxxx" #generate secure random secret key: openssl rand -hex 32
|
||||||
ACCESS_TOKEN_EXPIRE_MINUTES=10080
|
ACCESS_TOKEN_EXPIRE_MINUTES=10080
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
fastapi==0.128.6
|
fastapi==0.128.6
|
||||||
sqlmodel==0.0.32
|
sqlmodel==0.0.32
|
||||||
python-dotenv==1.2.1
|
|
||||||
openai==2.21.0
|
openai==2.21.0
|
||||||
spacy==3.8.11
|
spacy==3.8.11
|
||||||
PyJWT>=2.11.0
|
PyJWT>=2.11.0
|
||||||
argon2-cffi>=25.1.0
|
|
||||||
# python -m spacy download en_core_web_sm
|
# python -m spacy download en_core_web_sm
|
||||||
# python -m spacy download fr_core_news_sm
|
# python -m spacy download fr_core_news_sm
|
||||||
|
argon2-cffi>=25.1.0
|
||||||
|
pydantic-settings==2.13.1
|
||||||
|
|||||||
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 src.app.config import settings
|
||||||
from dotenv import load_dotenv
|
|
||||||
from sqlmodel import Session, SQLModel, create_engine
|
from sqlmodel import Session, SQLModel, create_engine
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
database_uri=os.environ.get("DATABASE_URI")
|
|
||||||
|
|
||||||
connect_args = {"check_same_thread": False}
|
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():
|
def create_db_and_tables():
|
||||||
SQLModel.metadata.create_all(engine)
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
|
from src.app.config import settings
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
import os
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
@@ -18,8 +17,6 @@ from .api import router
|
|||||||
#Test
|
#Test
|
||||||
from src.app.faker_seed import faker
|
from src.app.faker_seed import faker
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
#TODO : alternative @app.on_event("startup") ?
|
#TODO : alternative @app.on_event("startup") ?
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
@@ -31,7 +28,7 @@ async def lifespan(app: FastAPI):
|
|||||||
app = FastAPI(lifespan=lifespan)
|
app = FastAPI(lifespan=lifespan)
|
||||||
app.include_router(router)
|
app.include_router(router)
|
||||||
|
|
||||||
origin = os.environ.get("ORIGIN")
|
origin = settings.ORIGIN
|
||||||
origins = [origin]
|
origins = [origin]
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,4 @@
|
|||||||
import os
|
from src.app.config import settings
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
from datetime import timedelta, datetime, timezone
|
from datetime import timedelta, datetime, timezone
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -19,12 +17,9 @@ from argon2.exceptions import (
|
|||||||
from src.app.models.user import User
|
from src.app.models.user import User
|
||||||
from src.app.data.user import get_user
|
from src.app.data.user import get_user
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/token")
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/token")
|
||||||
password_hasher = PasswordHasher()
|
password_hasher = PasswordHasher()
|
||||||
|
|
||||||
secret_key = os.environ.get("SECRET_SIGN")
|
|
||||||
algorithm = "HS256"
|
algorithm = "HS256"
|
||||||
access_token_expire_minutes = 10080
|
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)
|
expire = datetime.now(timezone.utc) + timedelta(minutes=access_token_expire_minutes)
|
||||||
to_encode = data.copy()
|
to_encode = data.copy()
|
||||||
to_encode.update({"exp": expire})
|
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
|
return encoded_jwt
|
||||||
|
|
||||||
async def hash_password(password: str) -> str:
|
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"},
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
payload = jwt.decode(token, secret_key, algorithms=[algorithm])
|
payload = jwt.decode(token, settings.SECRET_KEY, algorithm=algorithm)
|
||||||
username = payload.get("sub")
|
username = payload.get("sub")
|
||||||
if username is None:
|
if username is None:
|
||||||
raise credentials_exception
|
raise credentials_exception
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
import os
|
|
||||||
import spacy
|
import spacy
|
||||||
|
|
||||||
|
from src.app.config import settings
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
from pydantic import BaseModel
|
|
||||||
from src.app.models.knowledge import Knowledge
|
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(
|
client = OpenAI(
|
||||||
base_url=language_model_api,
|
base_url=settings.LANGUAGE_MODEL_API,
|
||||||
api_key = "sk-no-key-required"
|
api_key = "sk-no-key-required"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,7 +19,7 @@ def questions_generation(knowledge: Knowledge):
|
|||||||
|
|
||||||
#SLM processing
|
#SLM processing
|
||||||
response = client.responses.create(
|
response = client.responses.create(
|
||||||
model=model_name,
|
model=settings.LANGUAGE_MODEL_NAME,
|
||||||
input=[
|
input=[
|
||||||
{"role": "system", "content": "Question Generation"},
|
{"role": "system", "content": "Question Generation"},
|
||||||
{"role": "user", "content": prompt}],
|
{"role": "user", "content": prompt}],
|
||||||
|
|||||||
Reference in New Issue
Block a user