-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from monkey-patch-sdk/demo/only-food
Demo/only food
- Loading branch information
Showing
7 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |