Skip to content

Commit

Permalink
Merge pull request #15 from monkey-patch-sdk/demo/only-food
Browse files Browse the repository at this point in the history
Demo/only food
  • Loading branch information
MichaelSel authored Oct 30, 2023
2 parents c475154 + f494a0f commit c299433
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/monkeyFunctions.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions apps/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
title="monkey-patch-apps",
)

origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


app.include_router(router)

# origins = [CLIENT_URL]
Expand Down
12 changes: 12 additions & 0 deletions apps/controllers/food_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import List, Dict
from services.food_service import specific_ratings, recommended_dishes


def get_ratings(reviews: List[str]) -> Dict[str, int]:
rating = specific_ratings(reviews)
return rating.model_dump()


def get_best_dishes(reviews: List[str]) -> List[str]:
recommended = recommended_dishes(reviews)
return recommended
2 changes: 2 additions & 0 deletions apps/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
# local
from routers import (
youtube_router as youtube,
food_router as food,
)


router = APIRouter()
router.include_router(youtube.router)
router.include_router(food.router)
26 changes: 26 additions & 0 deletions apps/routers/food_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# standard / third party
import json

from fastapi import APIRouter

# local
from controllers.food_controller import get_best_dishes, get_ratings

from services.food_service import get_yelp_reviews

router = APIRouter(
prefix="/food",
tags=["food"],
)


@router.get("/")
async def analyze_reviews(url: str):
reviews = get_yelp_reviews(url)
ratings = get_ratings(reviews)
best_dishes = get_best_dishes(reviews)
return {
"message": "success",
"ratings": ratings,
"best_dishes": best_dishes,
}
67 changes: 67 additions & 0 deletions apps/services/food_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from pydantic import Field, BaseModel
from typing import List, Annotated, Dict
from monkey import Monkey
import openai
import requests
from bs4 import BeautifulSoup

# from dotenv import load_dotenv
import os


# load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")


class RatingModel(BaseModel):
food: int = Annotated[
Field(..., ge=1, le=10), "The food-only rating based on the provided reviews"
]
service: int = Annotated[
Field(..., ge=1, le=10), "The service-only rating based on the provided reviews"
]
atmosphere: int = Annotated[
Field(..., ge=1, le=10),
"The atmosphere-only rating based on the provided reviews",
]
location: int = Annotated[
Field(..., ge=1, le=10),
"The location-only rating based on the provided reviews",
]


@Monkey.patch
def specific_ratings(reviews: List[str]) -> RatingModel:
"""
based on the reviews, separately rate (from 1 to 10) the following aspects:
- food
- service
- atmosphere
- location
- overall
"""


@Monkey.align
def test_specific_ratings():
"""We can test the function as normal using Pytest or Unittest"""


@Monkey.patch
def recommended_dishes(reviews: List[str]) -> List[str]:
"""
List the top 5 (or fewer) best dishes based on the given reviews
"""


@Monkey.align
def test_recommended_dishes():
"""We can test the function as normal using Pytest or Unittest"""


def get_yelp_reviews(yelp_url: str) -> List[str]:
response = requests.get(yelp_url)
soup = BeautifulSoup(response.text, "html.parser")
# get all "p" tags with class beginning with comment__
reviews = soup.find_all("p", class_=lambda x: x and x.startswith("comment__"))
return [review.text for review in reviews]

0 comments on commit c299433

Please sign in to comment.