Skip to content

Commit

Permalink
Merge pull request #8 from olp-cs/upgrade-api-to-pydantic-v2
Browse files Browse the repository at this point in the history
Upgrade Harmony API to Pydantic v2
  • Loading branch information
woodthom2 committed Aug 20, 2024
2 parents 0872203 + 4f960e8 commit 25a3634
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion harmony_api/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import os
from typing import Union

from pydantic import BaseSettings
from pydantic_settings import BaseSettings

GOOGLE_APPLICATION_CREDENTIALS = os.getenv("GOOGLE_APPLICATION_CREDENTIALS", '{}')
if GOOGLE_APPLICATION_CREDENTIALS:
Expand Down
2 changes: 1 addition & 1 deletion harmony_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_example_instruments() -> List[Instrument]:
str(os.getcwd()) + "/example_questionnaires.json", "r", encoding="utf-8"
) as file:
for line in file:
instrument = Instrument.parse_raw(line)
instrument = Instrument.model_validate_json(line)
example_instruments.append(instrument)

return example_instruments
Expand Down
4 changes: 2 additions & 2 deletions harmony_api/routers/text_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ def match(match_body: MatchBody) -> MatchResponse:

# Model
model = match_body.parameters
if model.dict() not in ALL_HARMONY_API_MODELS:
if model.model_dump(mode="json") not in ALL_HARMONY_API_MODELS:
raise http_exceptions.CouldNotProcessRequestHTTPException(
"Could not process request because the model does not exist."
)
if not helpers.check_model_availability(model.dict()):
if not helpers.check_model_availability(model.model_dump(mode="json")):
raise http_exceptions.CouldNotProcessRequestHTTPException(
"Could not process request because the model is not available."
)
Expand Down
8 changes: 4 additions & 4 deletions harmony_api/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
"""

from fastapi.concurrency import run_in_threadpool
from rocketry import Rocketry
from rocketry.conds import cron
# from rocketry import Rocketry
# from rocketry.conds import cron

from harmony_api.services.instruments_cache import InstrumentsCache
from harmony_api.services.vectors_cache import VectorsCache

app = Rocketry(executation="async")
# app = Rocketry(executation="async")


@app.task(cron("0 */12 * * *"))
# @app.task(cron("0 */12 * * *"))
async def do_every_12th_hour():
"""
Save the caches to disk every 12th hour.
Expand Down
2 changes: 1 addition & 1 deletion harmony_api/services/instruments_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def save(self):
# Instruments to dict
cache_parsed: dict[str, List] = {}
for key, value in self.__cache.items():
instruments = [x.dict() for x in value]
instruments = [x.model_dump(mode="json")() for x in value]
cache_parsed[key] = instruments

with open(cache_file_path, "w", encoding="utf8") as file:
Expand Down
9 changes: 5 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from harmony_api.routers.info_router import router as info_router
from harmony_api.routers.text_router import router as text_router
from harmony_api.services.instruments_cache import InstrumentsCache
from harmony_api.scheduler import app as app_rocketry
# from harmony_api.scheduler import app as app_rocketry
from harmony_api.services.vectors_cache import VectorsCache

description = """
Expand Down Expand Up @@ -89,7 +89,7 @@ class Server(uvicorn.Server):
"""

def handle_exit(self, sig: int, frame):
app_rocketry.session.shut_down()
# app_rocketry.session.shut_down()

return super().handle_exit(sig, frame)

Expand All @@ -112,11 +112,12 @@ async def main():
)

api = asyncio.create_task(server.serve())
scheduler = asyncio.create_task(app_rocketry.serve())
# scheduler = asyncio.create_task(app_rocketry.serve())

# Start both applications (FastAPI & Rocketry)
print("INFO:\t Starting applications...")
await asyncio.wait([api, scheduler])
# await asyncio.wait([api, scheduler])
await asyncio.wait(api)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fastapi==0.109.1
requests==2.31.0
pydantic==1.10.7
pydantic==2.8.2
pydantic-settings==2.4.0
uvicorn==0.21.1
pandas==2.0.0
tika==2.6.0
Expand All @@ -10,7 +11,6 @@ XlsxWriter==3.0.9
openpyxl==3.1.2
sentence-transformers==2.2.2
wget==3.2
rocketry==2.5.1
openai==1.30.2
vertexai==1.49.0
numpy==1.26.4
Expand Down

0 comments on commit 25a3634

Please sign in to comment.