diff --git a/server/.env.example b/server/.env.example index 373fcc5..e3ca509 100644 --- a/server/.env.example +++ b/server/.env.example @@ -1,6 +1,6 @@ DATABASE_URI="sqlite:///database.db" 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" -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 \ No newline at end of file diff --git a/server/requirements.txt b/server/requirements.txt index b750282..a0ccd53 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,9 +1,9 @@ fastapi==0.128.6 sqlmodel==0.0.32 -python-dotenv==1.2.1 openai==2.21.0 spacy==3.8.11 PyJWT>=2.11.0 -argon2-cffi>=25.1.0 # python -m spacy download en_core_web_sm -# python -m spacy download fr_core_news_sm \ No newline at end of file +# python -m spacy download fr_core_news_sm +argon2-cffi>=25.1.0 +pydantic-settings==2.13.1 diff --git a/server/src/app/__pycache__/config.cpython-311.pyc b/server/src/app/__pycache__/config.cpython-311.pyc new file mode 100644 index 0000000..b0182d1 Binary files /dev/null and b/server/src/app/__pycache__/config.cpython-311.pyc differ diff --git a/server/src/app/__pycache__/database.cpython-311.pyc b/server/src/app/__pycache__/database.cpython-311.pyc index abff500..67b67ad 100644 Binary files a/server/src/app/__pycache__/database.cpython-311.pyc and b/server/src/app/__pycache__/database.cpython-311.pyc differ diff --git a/server/src/app/__pycache__/main.cpython-311.pyc b/server/src/app/__pycache__/main.cpython-311.pyc index cfdbf97..590e08e 100644 Binary files a/server/src/app/__pycache__/main.cpython-311.pyc and b/server/src/app/__pycache__/main.cpython-311.pyc differ diff --git a/server/src/app/config.py b/server/src/app/config.py new file mode 100644 index 0000000..130233b --- /dev/null +++ b/server/src/app/config.py @@ -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() \ No newline at end of file diff --git a/server/src/app/database.py b/server/src/app/database.py index 31406b5..80582f2 100644 --- a/server/src/app/database.py +++ b/server/src/app/database.py @@ -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) diff --git a/server/src/app/main.py b/server/src/app/main.py index cb0d9f9..8f99323 100644 --- a/server/src/app/main.py +++ b/server/src/app/main.py @@ -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( diff --git a/server/src/app/services/__pycache__/auth.cpython-311.pyc b/server/src/app/services/__pycache__/auth.cpython-311.pyc index 81a240e..32ec1d0 100644 Binary files a/server/src/app/services/__pycache__/auth.cpython-311.pyc and b/server/src/app/services/__pycache__/auth.cpython-311.pyc differ diff --git a/server/src/app/services/__pycache__/language_generation.cpython-311.pyc b/server/src/app/services/__pycache__/language_generation.cpython-311.pyc index 7511e8b..c72f822 100644 Binary files a/server/src/app/services/__pycache__/language_generation.cpython-311.pyc and b/server/src/app/services/__pycache__/language_generation.cpython-311.pyc differ diff --git a/server/src/app/services/auth.py b/server/src/app/services/auth.py index 7959fda..71e8a8e 100644 --- a/server/src/app/services/auth.py +++ b/server/src/app/services/auth.py @@ -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 diff --git a/server/src/app/services/language_generation.py b/server/src/app/services/language_generation.py index 4f41604..a4335a0 100644 --- a/server/src/app/services/language_generation.py +++ b/server/src/app/services/language_generation.py @@ -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}],