From a71e6d989d0505209f80fb4e6f2ff2313ddcc45e Mon Sep 17 00:00:00 2001 From: Chukwuma Nwaugha Date: Sat, 2 Nov 2024 09:14:41 +0000 Subject: [PATCH] install setup tools and create a setup file for utils_pkg --- requirements.txt | 3 ++- server/src/__init__.py | 40 +++++++++++++++++++++++++++++----------- server/src/main.py | 21 +++++++++++++-------- utils_pkg/setup.py | 10 ++++++++++ 4 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 utils_pkg/setup.py diff --git a/requirements.txt b/requirements.txt index 72eb39a..2b9910d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,5 +30,6 @@ google-cloud-storage google-api-python-client google-generativeai -watchdog ruff +watchdog +setuptools diff --git a/server/src/__init__.py b/server/src/__init__.py index 1029fcb..95bc62e 100644 --- a/server/src/__init__.py +++ b/server/src/__init__.py @@ -1,24 +1,42 @@ import asyncio +import os import sys from pathlib import Path import uvloop from dotenv import load_dotenv -from services.admin_sdk import init_admin_sdk - -### Begin: Add the project root to sys.path -# Calculate the path to the root of the project, assuming the script is in the 'streamlit' directory +## Begin: Add the project root to sys.path project_root = Path(__file__).parent.parent.resolve() -# Add the project root to sys.path sys.path.append(str(project_root)) -### End: Add the project root to sys.path - asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) - -print("running uvloop as the event loop policy for asyncio") +print("running uvloop as event loop policy for asyncio") load_dotenv() -# init firebase admin sdk -init_admin_sdk() + + +def init_shared_packages(paths: list[str]): + # Add the shared module directory to the Python path + for path in paths: + pkg_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", path)) + sys.path.append(pkg_path) + + +def initialize(): + from services.admin_sdk import init_admin_sdk + + init_admin_sdk() + + +def print_project_meta(): + print(f"Project root: {project_root}") + print(f"Python version: {sys.version}") + print(f"Current working directory: {os.getcwd()}") + + +init_shared_packages(["services", "utils"]) + +initialize() + +print_project_meta() diff --git a/server/src/main.py b/server/src/main.py index 58d72c2..b41a6e3 100644 --- a/server/src/main.py +++ b/server/src/main.py @@ -3,21 +3,21 @@ from time import time from typing import Callable, Optional -from fastapi import FastAPI, HTTPException, Request +from fastapi import BackgroundTasks, FastAPI, HTTPException, Request from fastapi.middleware.cors import CORSMiddleware from fastapi_utilities import add_timer_middleware from pydantic import BaseModel from services.storage import StorageManager -from utils.audio_manager import AudioManager, AudioManagerConfig -from utils.audiocast_request import AudioScriptMaker, generate_source_content -from utils.chat_request import chat_request -from utils.chat_utils import ( +from utils_pkg.audio_manager import AudioManager, AudioManagerConfig +from utils_pkg.audiocast_request import AudioScriptMaker, generate_source_content +from utils_pkg.chat_request import chat_request +from app.src.utils.chat_utils import ( SessionChatMessage, SessionChatRequest, content_categories, ) -from utils.session_manager import SessionManager +from app.src.utils.session_manager import SessionManager @asynccontextmanager @@ -81,14 +81,19 @@ class GenerateAudioCastResponse(BaseModel): @app.post("/chat/{session_id}", response_model=str) -async def chat_endpoint(session_id: str, request: SessionChatRequest): +async def chat_endpoint( + session_id: str, request: SessionChatRequest, background_tasks: BackgroundTasks +): try: content_category = request.content_category db = SessionManager(session_id) db._add_chat(request.message) def on_finish(text: str): - db._add_chat(SessionChatMessage(role="assistant", content=text)) + background_tasks.add_task( + db._add_chat, + SessionChatMessage(role="assistant", content=text), + ) response = chat_request( content_category=content_category, diff --git a/utils_pkg/setup.py b/utils_pkg/setup.py new file mode 100644 index 0000000..91ba350 --- /dev/null +++ b/utils_pkg/setup.py @@ -0,0 +1,10 @@ +from setuptools import find_packages, setup + +setup( + name="utils_pkg", + version="1.0.0", + packages=find_packages(), + install_requires=[ + # List any dependencies here + ], +)