Merge pull request #1 from Axiomatic-AI/try-excercise

Add some fixes after trying the excercise
This commit is contained in:
jcbwndsr 2025-02-19 18:27:03 +01:00 committed by GitHub
commit eb20b68f03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 43 additions and 39 deletions

View File

@ -21,3 +21,14 @@ class HouseCreateRequest(BaseModel):
description="Description of the house", description="Description of the house",
examples=["A beautiful 3-bedroom house"], examples=["A beautiful 3-bedroom house"],
) )
square_feet: float = Field(
...,
description="Square footage of the house",
examples=[1500.00],
)
bedrooms: int = Field(
..., description="Number of bedrooms in the house", examples=[3]
)
bathrooms: float = Field(
..., description="Number of bathrooms in the house", examples=[2.5]
)

View File

@ -11,3 +11,6 @@ class House(SQLModel, table=True):
price: float = Field() price: float = Field()
description: str = Field() description: str = Field()
owner_user_id: UUID = Field(foreign_key="owner.user_id") owner_user_id: UUID = Field(foreign_key="owner.user_id")
square_feet: float = Field()
bedrooms: int = Field()
bathrooms: float = Field()

View File

@ -1,9 +1,9 @@
from typing import Annotated from typing import Annotated, Literal
from uuid import UUID from uuid import UUID
from fastapi import Depends from fastapi import Depends
from sqlalchemy.ext.asyncio.session import AsyncSession from sqlalchemy.ext.asyncio.session import AsyncSession
from sqlmodel import select from sqlmodel import asc, desc, select
from ..models.house import House from ..models.house import House
from ..providers.db_provider import get_session from ..providers.db_provider import get_session
@ -13,8 +13,19 @@ class HouseRepository:
def __init__(self, session: Annotated[AsyncSession, Depends(get_session)]) -> None: def __init__(self, session: Annotated[AsyncSession, Depends(get_session)]) -> None:
self.session = session self.session = session
async def get_all(self, limit: int = 100, offset: int = 0) -> list[House]: async def get_all(
statement = select(House).offset(offset).limit(limit) self,
limit: int = 100,
offset: int = 0,
order_by: Literal["PRICE"] = "PRICE",
sort_order: Literal["ASC", "DESC"] = "DESC",
) -> list[House]:
sorter = desc if sort_order == "DESC" else asc
statement = (
select(House).offset(offset).limit(limit).order_by(sorter(House.price))
)
result = await self.session.execute(statement) result = await self.session.execute(statement)
return result.scalars().all() return result.scalars().all()

View File

@ -1,4 +1,4 @@
from typing import Annotated from typing import Annotated, Literal
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
@ -35,6 +35,9 @@ async def create_house(
country=body.country, country=body.country,
price=body.price, price=body.price,
description=body.description, description=body.description,
square_feet=body.square_feet,
bedrooms=body.bedrooms,
bathrooms=body.bathrooms,
) )
await house_repository.save(house) await house_repository.save(house)
@ -45,10 +48,14 @@ async def create_house(
@router.get("") @router.get("")
async def get_all_houses( async def get_all_houses(
house_repository: Annotated[HouseRepository, Depends()], house_repository: Annotated[HouseRepository, Depends()],
order_by: Literal["PRICE"] = "PRICE",
sort_order: Literal["ASC", "DESC"] = "DESC",
limit: int = 100, limit: int = 100,
offset: int = 0, offset: int = 0,
) -> HousesListResponse: ) -> HousesListResponse:
all_houses = await house_repository.get_all(offset=offset, limit=limit) all_houses = await house_repository.get_all(
offset=offset, limit=limit, order_by=order_by, sort_order=sort_order
)
house_responses = [ house_responses = [
HouseResponse( HouseResponse(
@ -61,6 +68,5 @@ async def get_all_houses(
) )
for house in all_houses for house in all_houses
] ]
print(house_responses)
return HousesListResponse(houses=house_responses) return HousesListResponse(houses=house_responses)

View File

@ -1,45 +1,16 @@
import random
from dataclasses import dataclass
from typing import List
@dataclass
class Prediction:
predicted_price: float
confidence_score: float
similar_listings: List[float]
class HousePricePredictor: class HousePricePredictor:
""" """
Mock ML model that predicts house prices. Mock ML model that predicts house prices.
In a real scenario, this would load a trained model. In a real scenario, this would load a trained model.
""" """
def __init__(self): async def predict(
# Mock initialization - in reality would load model weights
pass
def predict(
self, square_feet: float, bedrooms: int, bathrooms: float self, square_feet: float, bedrooms: int, bathrooms: float
) -> Prediction: ) -> float:
"""
Mock prediction method that returns:
- predicted price
- confidence score
- similar listing prices
"""
# Mock prediction logic
base_price = square_feet * 200 base_price = square_feet * 200
bedroom_value = bedrooms * 25000 bedroom_value = bedrooms * 25000
bathroom_value = bathrooms * 15000 bathroom_value = bathrooms * 15000
predicted_price = base_price + bedroom_value + bathroom_value predicted_price = base_price + bedroom_value + bathroom_value
# Add some randomness to make it interesting return predicted_price
confidence_score = random.uniform(0.8, 0.99)
similar_listings = [
predicted_price * random.uniform(0.9, 1.1) for _ in range(3)
]
return Prediction(predicted_price, confidence_score, similar_listings)

View File

@ -1,5 +1,7 @@
import random import random
from ..models import User
class InvestorPredictor: class InvestorPredictor:
def is_investor(user: User) -> bool: def is_investor(user: User) -> bool: