feat: backend improvements

This commit is contained in:
Mike 2024-08-05 14:43:34 -04:00
parent a19e72afaf
commit 6657ca0f01
5 changed files with 16 additions and 28 deletions

View file

@ -26,7 +26,7 @@ class Results(BaseModel):
class AI: class AI:
genai = genai.configure(api_key=GEMINI_API_KEY) genai = genai.configure(api_key=GEMINI_API_KEY)
def generate_topics(self) -> list: def generate_topics(self, topics) -> list:
model = genai.GenerativeModel( model = genai.GenerativeModel(
"gemini-1.5-flash", "gemini-1.5-flash",
generation_config={ generation_config={
@ -35,8 +35,8 @@ class AI:
}, },
) )
prompt = """ prompt = f"""
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 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 response = model.generate_content(prompt).text
@ -45,7 +45,7 @@ class AI:
def generate_card_json(self, photo_payload, topic): def generate_card_json(self, photo_payload, topic):
prompt = f""" 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}". 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 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} {photo_payload}

View file

@ -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() 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): def create_card(db: Session, card: schemas.CardCreate):
db_card = models.Card(**card.model_dump()) db_card = models.Card(**card.model_dump())
db.add(db_card) db.add(db_card)

View file

@ -50,25 +50,6 @@ photos = Pictures(PEXELS_API_KEY)
@app.get("/") @app.get("/")
async def read_main(): async def read_main():
return data 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) @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 # return the items in a random order for the game
shuffle(cards) shuffle(cards)
return cards return cards[:12]
@app.get("/load-data") @app.get("/load-data")
async def load_data(): def load_data(db: Session = Depends(get_db)):
topics = ai.generate_topics() # Get Current Topics
db_topics = [item[0] for item in crud.get_topics(db)]
topics = ai.generate_topics(db_topics)
try: try:
for item in topics: for item in topics:
@ -103,6 +86,7 @@ async def load_data():
json=card_json, json=card_json,
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
logger.info(r.status_code)
logger.info(r.json()) logger.info(r.json())
return {"success": "entered into the db"} return {"success": "entered into the db"}

View file

@ -7,7 +7,7 @@ class Card(Base):
__tablename__ = "card" __tablename__ = "card"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
photo_id = Column(Integer) photo_id = Column(Integer, unique=True)
medium_url = Column(String) medium_url = Column(String)
photo_url = Column(String) photo_url = Column(String)
topic = Column(String, index=True) topic = Column(String, index=True)

View file

@ -18,4 +18,4 @@ class Card(CardBase):
id: int id: int
class Config: class Config:
orm_mode = True from_attributes = True