Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: apradoada/pet_name_generator
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: AdaGold/pet_name_generator
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 15 commits
  • 6 files changed
  • 2 contributors

Commits on Jun 21, 2024

  1. Create README.md

    apradoada authored Jun 21, 2024
    Copy the full SHA
    2331aec View commit details
  2. Update README.md

    apradoada authored Jun 21, 2024
    Copy the full SHA
    8ee452b View commit details

Commits on Dec 5, 2024

  1. Update pet model

    apradoada committed Dec 5, 2024
    Copy the full SHA
    2f9e150 View commit details

Commits on Dec 6, 2024

  1. Copy the full SHA
    3846a17 View commit details
  2. Copy the full SHA
    feef808 View commit details
  3. Pushing new migrations

    apradoada committed Dec 6, 2024
    Copy the full SHA
    2da90b8 View commit details
  4. Copy the full SHA
    66174e2 View commit details
  5. Copy the full SHA
    a755f61 View commit details
  6. Copy the full SHA
    eab3be7 View commit details
  7. Copy the full SHA
    1b93f8d View commit details
  8. Updated README.md

    apradoada committed Dec 6, 2024
    Copy the full SHA
    ed49f1b View commit details
  9. Merge branch 'solution'

    apradoada committed Dec 6, 2024
    Copy the full SHA
    033c541 View commit details

Commits on Dec 9, 2024

  1. Copy the full SHA
    feefd7c View commit details
  2. Copy the full SHA
    d571fb9 View commit details
  3. Copy the full SHA
    3181020 View commit details
Showing with 163 additions and 19 deletions.
  1. +27 −0 README.md
  2. +7 −10 app/__init__.py
  3. +22 −6 app/models/pet.py
  4. +35 −3 app/routes/pet_routes.py
  5. +36 −0 migrations/versions/b69c72688ecc_.py
  6. +36 −0 migrations/versions/d066cb56e11a_.py
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Part 1 - Setup

1. Clone the Pet Name Generator Repo
2. Create your venv
3. Install your requirements.txt
4. Create a database in psql called pet_name_gen_dev
5. Create your .env file and add pet_name_gen_dev as the SQLALCHEMY_DATABASE_URI.
6. Run your flask db init and flask db upgrade
7. If you want, check your database to make sure it has the pet relation

Part 2 - Install OpenAI

1. Install the Gemini API python library using pip
2. Run the command pip freeze > requirements.txt to add Gemini to the requirements.txt file
3. Add your Gemini Secret key to your .env file
4. Import `google.generativeai` into your pet_routes.py file
5. Create an Gemini client in your pet_routes.py file

Part 3 - Write Your Pet Name POST Route

1. Now that everything is set up and Gemini has been installed, write a helper function that will take the correct parameters to generate a suggestion for a pet’s name given its species, color and personality.
2. Work with your group to write a prompt to feed into the Gemini API that will generate a name for your pet with the parameters given. Feel free to test out a few prompts and make sure to use print statements to analyze the response you get from each.
3. Once you’ve got a working prompt and feel satisfied with your understanding of the response, write the add_pet function to add a pet to the database. Your request body should only include the pet’s species (type), personality and color. The name should be generated using AI and then added to the Pet object which then gets sent to the database. Remember that you may have to tweak the string you receive in order to effectively add it to the Pet object.

Part 4 - (Optional) Write a PATCH Route

If you have completed the steps up to this point, try adding a route that allows you to request a different name for a specific pet. It’s up to you how you want to achieve this, but see if you are able to tweak your earlier helper method to make this possible. Once the AI suggests a new name, update the name in the database.
17 changes: 7 additions & 10 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import os
from flask import Flask
from .routes.pet_routes import bp
from .routes.pet_routes import bp as pets_bp
from .db import db, migrate
from .models import pet

def create_app(test_config=None):
def create_app(config=None):
# __name__ stores the name of the module we're in
app = Flask(__name__)

if not test_config:
db_to_use = os.environ.get("SQLALCHEMY_DATABASE_URI")
else:
db_to_use = os.environ.get("SQLALCHEMY_TEST_DATABASE_URI")


app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = db_to_use
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI')
if config:
app.config.update(config)

db.init_app(app)
migrate.init_app(app, db)

app.register_blueprint(bp)
app.register_blueprint(pets_bp)

return app
28 changes: 22 additions & 6 deletions app/models/pet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlalchemy.orm import Mapped, mapped_column
from typing import Optional
from ..db import db

class Pet(db.Model):
@@ -8,11 +9,26 @@ class Pet(db.Model):
personality: Mapped[str]
color: Mapped[str]

def to_dict(self):
return {
def to_dict(self):
pet_dict = {
"id" : self.id,
"name" : self.name,
"animal type": self.animal_type,
"animal": self.animal_type,
"personality": self.personality,
"coloring": self.color
}
"coloration": self.color
}
if self.name:
pet_dict["name"] = self.name

return pet_dict


@classmethod
def from_dict(cls, data_dict):
new_pet = cls(
name = data_dict["name"],
animal_type = data_dict["animal"],
personality = data_dict["personality"],
color = data_dict["coloration"]
)

return new_pet
38 changes: 35 additions & 3 deletions app/routes/pet_routes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
from flask import Blueprint, jsonify, request, abort, make_response
from flask import Blueprint, request, abort, make_response
from ..db import db
from ..models.pet import Pet

bp = Blueprint("pets", __name__, url_prefix="/pets")

@bp.post("")
def add_pets():
pass
def create_pet():
pass

@bp.get("")
def get_pets():
pet_query = db.select(Pet)

pets = db.session.scalars(pet_query)
response = []

for pet in pets:
response.append(pet.to_dict())

return response

@bp.get("/<pet_id>")
def get_single_pet(pet_id):
pet = validate_model(Pet,pet_id)
return pet.to_dict()

def validate_model(cls,id):
try:
id = int(id)
except:
response = response = {"message": f"{cls.__name__} {id} invalid"}
abort(make_response(response , 400))

query = db.select(cls).where(cls.id == id)
model = db.session.scalar(query)
if model:
return model

response = {"message": f"{cls.__name__} {id} not found"}
abort(make_response(response, 404))
36 changes: 36 additions & 0 deletions migrations/versions/b69c72688ecc_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""empty message
Revision ID: b69c72688ecc
Revises: d066cb56e11a
Create Date: 2024-12-06 09:53:41.147503
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'b69c72688ecc'
down_revision = 'd066cb56e11a'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('pet', schema=None) as batch_op:
batch_op.alter_column('name',
existing_type=sa.VARCHAR(),
nullable=False)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('pet', schema=None) as batch_op:
batch_op.alter_column('name',
existing_type=sa.VARCHAR(),
nullable=True)

# ### end Alembic commands ###
36 changes: 36 additions & 0 deletions migrations/versions/d066cb56e11a_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""empty message
Revision ID: d066cb56e11a
Revises: f3508bc26a9b
Create Date: 2024-12-06 09:04:41.877457
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'd066cb56e11a'
down_revision = 'f3508bc26a9b'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('pet', schema=None) as batch_op:
batch_op.alter_column('name',
existing_type=sa.VARCHAR(),
nullable=True)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('pet', schema=None) as batch_op:
batch_op.alter_column('name',
existing_type=sa.VARCHAR(),
nullable=False)

# ### end Alembic commands ###