PIbd-32_Kashin_M.I_API_Cour.../router_reviews.py

29 lines
797 B
Python

import pandas as pd
from fastapi import APIRouter
from typing import List
from enums import TypeMood
from schemas import Review
router = APIRouter(
prefix="/reviews",
tags=["Reviews"],
)
@router.get("/{rubrics}")
def get_reviews(rubrics: str,
type_mood: TypeMood) -> list[Review]:
if type_mood == TypeMood.NEGATIVE:
df = pd.read_csv("./sity/sity_negative.csv")
else:
df = pd.read_csv("./sity/sity_positive.csv")
df = df.fillna("")
filtered_df = df[df["rubrics"] == rubrics]
shuffled_df = filtered_df.sample(frac=1)
sorted_df = shuffled_df.sort_values(by="rating", ascending=False)
unique_reviews = sorted_df.drop_duplicates(subset="city")
unique_reviews = unique_reviews.head(10).to_dict(orient="records")
return unique_reviews