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:
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}

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()
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)

View file

@ -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"}

View file

@ -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)

View file

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