From 6657ca0f01c2dc030c83db3553fef91faf3f158d Mon Sep 17 00:00:00 2001 From: Mike Smith <89040888+smiggiddy@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:43:34 -0400 Subject: [PATCH] feat: backend improvements --- memory-game/mg-backend/ai.py | 8 ++++---- memory-game/mg-backend/crud.py | 4 ++++ memory-game/mg-backend/main.py | 28 ++++++---------------------- memory-game/mg-backend/models.py | 2 +- memory-game/mg-backend/schemas.py | 2 +- 5 files changed, 16 insertions(+), 28 deletions(-) diff --git a/memory-game/mg-backend/ai.py b/memory-game/mg-backend/ai.py index 6df7e84..f490aff 100644 --- a/memory-game/mg-backend/ai.py +++ b/memory-game/mg-backend/ai.py @@ -26,7 +26,7 @@ class Results(BaseModel): class AI: genai = genai.configure(api_key=GEMINI_API_KEY) - def generate_topics(self) -> list: + def generate_topics(self, topics) -> list: model = genai.GenerativeModel( "gemini-1.5-flash", generation_config={ @@ -35,8 +35,8 @@ class AI: }, ) - prompt = """ - Return 12 items for a toddler aged 2 to 3 to choose from in a memory game. e.g. "ball", "car" . Don't use my examples words + prompt = f""" + Return 12 items for a toddler aged 2 to 3 years old to choose from in a memory game. e.g. "ball", "car" . Don't use my examples words. As this prompt may be called at different times, the potential items sent should be random that the ods of you sending the same thing multiple times is reduced. Previously returned topics are found in this array {topics} and do not return a topic found in this list. """ response = model.generate_content(prompt).text @@ -45,7 +45,7 @@ class AI: def generate_card_json(self, photo_payload, topic): prompt = f""" This JSON payload will be analyzed for a toddler matching game. Your job is to identify which object within the payload that best matches the topic "{topic}". - Define a "best match" as containing the keywords from the topic within the "alt" field of the image object that would be appropriate for a toddler's matching game. a "best match" should also be the primary subject in the photo, if there are other items in the alt text then that's determined to be a bad match. + Define a "best match" as containing the keywords from the topic within the "alt" field of the image object that would be appropriate for a toddler's matching game. a "best match" should also be the primary and only subject in the photo, if there are other items in the alt text then that's determined to be a bad match and should not be selected. For example, "cat sitting on a book" would be a bad match and should not be selected, where "black cat" is a good match. if its determine the alt text is not a great match for the toddler game set the `bad_match` to True and for best matches set to False {photo_payload} diff --git a/memory-game/mg-backend/crud.py b/memory-game/mg-backend/crud.py index a229555..7c2308c 100644 --- a/memory-game/mg-backend/crud.py +++ b/memory-game/mg-backend/crud.py @@ -11,6 +11,10 @@ def get_cards(db: Session, skip: int = 0, limit: int = 100): return db.query(models.Card).offset(skip).limit(limit).all() +def get_topics(db: Session, skip: int = 0, limit: int = 100): + return db.query(models.Card.topic).offset(skip).limit(limit).all() + + def create_card(db: Session, card: schemas.CardCreate): db_card = models.Card(**card.model_dump()) db.add(db_card) diff --git a/memory-game/mg-backend/main.py b/memory-game/mg-backend/main.py index 8e8a4f5..2a3d8ee 100644 --- a/memory-game/mg-backend/main.py +++ b/memory-game/mg-backend/main.py @@ -50,25 +50,6 @@ photos = Pictures(PEXELS_API_KEY) @app.get("/") async def read_main(): return data - # data = [] - # - # topics = ai.generate_topics() - # - # try: - # for item in topics: - # - # logger.info(item) - # picture_data = photos.search(item["topic"]) - # - # card_json = ai.generate_card_json(picture_data, item) - # logger.info(card_json) - # - # data.append(card_json) - # - # return data - # except Exception as e: - # logger.error(e) - # return {"error": "uname to handle request"} @app.post("/ai-cards", response_model=schemas.Card) @@ -82,12 +63,14 @@ def read_cards(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): # return the items in a random order for the game shuffle(cards) - return cards + return cards[:12] @app.get("/load-data") -async def load_data(): - topics = ai.generate_topics() +def load_data(db: Session = Depends(get_db)): + # Get Current Topics + db_topics = [item[0] for item in crud.get_topics(db)] + topics = ai.generate_topics(db_topics) try: for item in topics: @@ -103,6 +86,7 @@ async def load_data(): json=card_json, headers={"Content-Type": "application/json"}, ) + logger.info(r.status_code) logger.info(r.json()) return {"success": "entered into the db"} diff --git a/memory-game/mg-backend/models.py b/memory-game/mg-backend/models.py index 8b795da..e9b24bc 100644 --- a/memory-game/mg-backend/models.py +++ b/memory-game/mg-backend/models.py @@ -7,7 +7,7 @@ class Card(Base): __tablename__ = "card" id = Column(Integer, primary_key=True) - photo_id = Column(Integer) + photo_id = Column(Integer, unique=True) medium_url = Column(String) photo_url = Column(String) topic = Column(String, index=True) diff --git a/memory-game/mg-backend/schemas.py b/memory-game/mg-backend/schemas.py index d76190e..13dbe8a 100644 --- a/memory-game/mg-backend/schemas.py +++ b/memory-game/mg-backend/schemas.py @@ -18,4 +18,4 @@ class Card(CardBase): id: int class Config: - orm_mode = True + from_attributes = True