Skip to content

Commit

Permalink
Refactor main.py: Remove unused imports and authentication code
Browse files Browse the repository at this point in the history
  • Loading branch information
sahi-mfg committed Feb 2, 2024
1 parent b257b52 commit 43ce7ea
Showing 1 changed file with 1 addition and 78 deletions.
79 changes: 1 addition & 78 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,16 @@
import os
from datetime import datetime, timedelta
from io import BytesIO

import uvicorn
from doten import load_dotenv # type: ignore
from fastapi import Depends, FastAPI, File, HTTPException, UploadFile, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from jose import jwt # type: ignore
from passlib.context import CryptContext # type: ignore
from fastapi import FastAPI, File, HTTPException, UploadFile
from PIL import Image
from pydantic import BaseModel
from sqlalchemy import Column, Integer, String # type: ignore
from sqlalchemy.ext.declarative import declarative_base # type: ignore

from .model import load_model, predict, prepare_image

app = FastAPI(title="Age Detection", description="API to predict age from images", version="0.1")

model = load_model()

load_dotenv()

SECRET_KEY = os.getenv("SECRET_KEY")
ALGORITHM = os.getenv("ALGORITHM")
ACCESS_TOKEN_EXPIRE_MINUTES = os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES")


Base = declarative_base()


class User(Base):
__tablename__ = "users"

id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True)
hashed_password = Column(String)


class Token(BaseModel):
access_token: str
token_type: str


class UserInDB(User):
hashed_password: str


pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)


def get_password_hash(password):
return pwd_context.hash(password)


def authenticate_user(users, username: str, password: str):
user = users.get(username)
if not user:
return False
if not verify_password(password, user.hashed_password):
return False
return user


def create_acess_token(data: dict, expires_delta: timedelta = timedelta(minutes=15)):
to_encode = data.copy()
expire = datetime.utcnow() + expires_delta
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt


@app.post("/token", response_model=Token)
def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
user = authenticate_user(users, form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_acess_token(data={"sub": user.username}, expires_delta=access_token_expires)
return {"access_token": access_token, "token_type": "bearer"}


@app.get("/", tags=["Welcome"])
def greeting():
Expand Down

0 comments on commit 43ce7ea

Please sign in to comment.