build: add run all shell script with mongodb
Browse files- app/api/management_api.py +37 -3
- docker/{mondodb-docker-compose.yaml → mongodb-docker-compose.yaml} +0 -0
- main.py +1 -30
- run_all.sh +37 -0
app/api/management_api.py
CHANGED
|
@@ -1,14 +1,21 @@
|
|
| 1 |
from fastapi import APIRouter
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import toml
|
| 5 |
from loguru import logger
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
class HealthResponse(BaseModel):
|
| 13 |
status: str = "ok"
|
| 14 |
|
|
@@ -21,7 +28,7 @@ async def health_check():
|
|
| 21 |
return HealthResponse()
|
| 22 |
|
| 23 |
|
| 24 |
-
#### Version
|
| 25 |
__version__ = None
|
| 26 |
|
| 27 |
|
|
@@ -52,3 +59,30 @@ async def version_api():
|
|
| 52 |
Version endpoint, returns the version of the system
|
| 53 |
"""
|
| 54 |
return {"version": _load_version()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter
|
| 2 |
+
from fastapi.responses import RedirectResponse, JSONResponse
|
| 3 |
from pathlib import Path
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import toml
|
| 6 |
from loguru import logger
|
| 7 |
+
from environs import Env
|
| 8 |
+
import json
|
| 9 |
|
| 10 |
|
| 11 |
+
env = Env()
|
| 12 |
+
env.read_env()
|
| 13 |
+
REDIRECT_TO_GRADIO_UI = env.bool("REDIRECT_TO_GRADIO_UI", True)
|
| 14 |
|
| 15 |
+
router = APIRouter(tags=["management"])
|
| 16 |
|
| 17 |
+
|
| 18 |
+
#### Health Check #################################################
|
| 19 |
class HealthResponse(BaseModel):
|
| 20 |
status: str = "ok"
|
| 21 |
|
|
|
|
| 28 |
return HealthResponse()
|
| 29 |
|
| 30 |
|
| 31 |
+
#### Version #######################################################
|
| 32 |
__version__ = None
|
| 33 |
|
| 34 |
|
|
|
|
| 59 |
Version endpoint, returns the version of the system
|
| 60 |
"""
|
| 61 |
return {"version": _load_version()}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def _get_manifest() -> JSONResponse:
|
| 65 |
+
print("get manifest")
|
| 66 |
+
current_dir = Path(__file__).resolve().parent.parent.parent
|
| 67 |
+
manifest_path = current_dir / "static" / "manifest.json"
|
| 68 |
+
with open(manifest_path, "r") as f:
|
| 69 |
+
return JSONResponse(json.load(f))
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
### root ##########################################################
|
| 73 |
+
@router.get("/")
|
| 74 |
+
async def root():
|
| 75 |
+
"""
|
| 76 |
+
Redirect root to Gradio UI
|
| 77 |
+
"""
|
| 78 |
+
if REDIRECT_TO_GRADIO_UI:
|
| 79 |
+
return RedirectResponse(url="/ui")
|
| 80 |
+
else:
|
| 81 |
+
return _get_manifest()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
### manifest #######################################################
|
| 85 |
+
@router.get("/manifest.json")
|
| 86 |
+
async def get_manifest():
|
| 87 |
+
"""Return the web app manifest"""
|
| 88 |
+
return _get_manifest()
|
docker/{mondodb-docker-compose.yaml → mongodb-docker-compose.yaml}
RENAMED
|
File without changes
|
main.py
CHANGED
|
@@ -1,30 +1,14 @@
|
|
| 1 |
-
import json
|
| 2 |
from fastapi import FastAPI
|
| 3 |
-
from fastapi.responses import RedirectResponse, JSONResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from app.api import chat_api, conversation_api, management_api
|
| 7 |
-
from app.config.log import log_config
|
| 8 |
from loguru import logger
|
| 9 |
-
from environs import Env
|
| 10 |
from contextlib import asynccontextmanager
|
| 11 |
from app.db.factory import db_client
|
| 12 |
from gradio_chatbot import build_gradio_app, app_auth
|
| 13 |
import gradio as gr
|
| 14 |
-
import os
|
| 15 |
from app.core.initial_setup.setup import InitialSetup
|
| 16 |
|
| 17 |
-
print(log_config.get_log_level())
|
| 18 |
-
|
| 19 |
-
env = Env()
|
| 20 |
-
env.read_env()
|
| 21 |
-
|
| 22 |
-
DB_DATABASE_TYPE = env.str("DB_DATABASE_TYPE", "mongodb")
|
| 23 |
-
|
| 24 |
-
# special handling for Hugging Face Space
|
| 25 |
-
IS_HF_SPACE = os.environ.get("SPACE_ID") is not None
|
| 26 |
-
SPACE_URL = "https://lokumai-openai-openapi-template.hf.space" if IS_HF_SPACE else "http://localhost:7860"
|
| 27 |
-
|
| 28 |
|
| 29 |
@asynccontextmanager
|
| 30 |
async def lifespan(app: FastAPI):
|
|
@@ -115,6 +99,7 @@ app.add_middleware(
|
|
| 115 |
|
| 116 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 117 |
app.mount("/.well-known", StaticFiles(directory=".well-known"), name="well-known")
|
|
|
|
| 118 |
app.include_router(chat_api.router)
|
| 119 |
app.include_router(management_api.router)
|
| 120 |
app.include_router(conversation_api.router)
|
|
@@ -124,18 +109,4 @@ demo = build_gradio_app()
|
|
| 124 |
app = gr.mount_gradio_app(app, demo, path="/ui", auth=app_auth)
|
| 125 |
|
| 126 |
|
| 127 |
-
@app.get("/")
|
| 128 |
-
async def root():
|
| 129 |
-
"""Redirect root to Gradio UI"""
|
| 130 |
-
return RedirectResponse(url="/ui")
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
@app.get("/manifest.json")
|
| 134 |
-
async def get_manifest():
|
| 135 |
-
"""Return the web app manifest"""
|
| 136 |
-
manifest_path = os.path.join(os.path.dirname(__file__), "static", "manifest.json")
|
| 137 |
-
with open(manifest_path, "r") as f:
|
| 138 |
-
return JSONResponse(json.load(f))
|
| 139 |
-
|
| 140 |
-
|
| 141 |
# uv run uvicorn main:app --host 0.0.0.0 --port 7860 --reload
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from app.api import chat_api, conversation_api, management_api
|
|
|
|
| 5 |
from loguru import logger
|
|
|
|
| 6 |
from contextlib import asynccontextmanager
|
| 7 |
from app.db.factory import db_client
|
| 8 |
from gradio_chatbot import build_gradio_app, app_auth
|
| 9 |
import gradio as gr
|
|
|
|
| 10 |
from app.core.initial_setup.setup import InitialSetup
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@asynccontextmanager
|
| 14 |
async def lifespan(app: FastAPI):
|
|
|
|
| 99 |
|
| 100 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 101 |
app.mount("/.well-known", StaticFiles(directory=".well-known"), name="well-known")
|
| 102 |
+
|
| 103 |
app.include_router(chat_api.router)
|
| 104 |
app.include_router(management_api.router)
|
| 105 |
app.include_router(conversation_api.router)
|
|
|
|
| 109 |
app = gr.mount_gradio_app(app, demo, path="/ui", auth=app_auth)
|
| 110 |
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
# uv run uvicorn main:app --host 0.0.0.0 --port 7860 --reload
|
run_all.sh
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# check if mongodb docker container is running and start with docker compose docker/mongodb-docker-compose.yml
|
| 4 |
+
MONGO_CONTAINER_NAME="mongo"
|
| 5 |
+
echo "Starting MongoDB container..."
|
| 6 |
+
echo "Checking if MongoDB container is running..."
|
| 7 |
+
if ! docker ps | grep -q "$MONGO_CONTAINER_NAME"; then
|
| 8 |
+
echo "Starting MongoDB container..."
|
| 9 |
+
docker compose -f docker/mongodb-docker-compose.yaml up -d
|
| 10 |
+
echo "MongoDB container started"
|
| 11 |
+
echo "Waiting for MongoDB container to be ready..."
|
| 12 |
+
sleep 10
|
| 13 |
+
echo "MongoDB container is ready"
|
| 14 |
+
else
|
| 15 |
+
echo "MongoDB container is already running"
|
| 16 |
+
fi
|
| 17 |
+
|
| 18 |
+
# stop existing application if running
|
| 19 |
+
echo "--------------------------------"
|
| 20 |
+
echo "checking if application is running..."
|
| 21 |
+
echo "Stopping existing application if running..."
|
| 22 |
+
pkill -f "uvicorn main:app"
|
| 23 |
+
|
| 24 |
+
# wait for the process to stop
|
| 25 |
+
sleep 2
|
| 26 |
+
|
| 27 |
+
# start FastAPI application with uvicorn integrated Gradio Application
|
| 28 |
+
echo "--------------------------------"
|
| 29 |
+
echo "Starting application..."
|
| 30 |
+
echo "--------------------------------"
|
| 31 |
+
uv run uvicorn main:app --host 0.0.0.0 --port 7860 --reload
|
| 32 |
+
|
| 33 |
+
# catch SIGINT (Ctrl+C) to stop the application gracefully
|
| 34 |
+
trap "echo 'Stopping application...'; pkill -f 'uvicorn main:app'; exit" INT
|
| 35 |
+
|
| 36 |
+
# wait for the application to run
|
| 37 |
+
wait
|