forked from daxa-ai/pebblo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config.yaml Support for Pebblo (daxa-ai#137)
* Updated Test Cases * Fixed EntityClassifier init * Updated outputdir path --------- Co-authored-by: kunal.cd <kunal@clouddefense.io>
- Loading branch information
1 parent
7d2c728
commit c6656a0
Showing
9 changed files
with
146 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import yaml | ||
|
||
from pydantic import BaseSettings, Field | ||
import pathlib | ||
|
||
# Default config value | ||
dir_path = pathlib.Path().absolute() | ||
|
||
|
||
# Port BaseModel | ||
class PortConfig(BaseSettings): | ||
host: str = Field(default='localhost') | ||
port: int = Field(default=8000) | ||
|
||
|
||
# Report BaseModel | ||
class ReportConfig(BaseSettings): | ||
format: str = Field(default='pdf') | ||
outputDir: str = Field(dir_path) | ||
|
||
|
||
# Logging BaseModel | ||
class LoggingConfig(BaseSettings): | ||
level: str = Field(default='info') | ||
|
||
|
||
# ConfigFile BaseModel | ||
class Config(BaseSettings): | ||
daemon: PortConfig | ||
reports: ReportConfig | ||
logging: LoggingConfig | ||
|
||
|
||
def load_config(path) -> Config: | ||
try: | ||
# If Path does not exist in command, set default config value | ||
conf_obj = Config( | ||
daemon=PortConfig( | ||
host='localhost', | ||
port=8000 | ||
), | ||
reports=ReportConfig( | ||
format='pdf', | ||
outputDir='~/.pebblo' | ||
), | ||
logging=LoggingConfig( | ||
level='info' | ||
) | ||
) | ||
if not path: | ||
# Setting Default config details | ||
return conf_obj.dict() | ||
|
||
# If Path exist, set config value | ||
else: | ||
con_file = path | ||
try: | ||
with open(con_file, "r") as output: | ||
cred_json = yaml.safe_load(output) | ||
parsed_config = Config.parse_obj(cred_json) | ||
config_dict = parsed_config.dict() | ||
return config_dict | ||
except IOError as err: | ||
print(f"no credentials file found at {con_file}") | ||
return conf_obj.dict() | ||
|
||
except Exception as err: | ||
print(f'Error while loading config details, err: {err}') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
daemon: | ||
port: 8000 | ||
host: localhost | ||
logging: | ||
level: info | ||
reports: | ||
format: pdf | ||
outputDir: ~/.pebblo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from fastapi import FastAPI | ||
|
||
import uvicorn | ||
import asyncio | ||
from pebblo.app.routers.routers import router_instance | ||
|
||
|
||
class Service: | ||
def __init__(self, config_details): | ||
# Initialise app instance | ||
self.app = FastAPI() | ||
# Register the router instance with the main app | ||
self.app.include_router(router_instance.router) | ||
# Fetching Details from Config File | ||
self.config_details = config_details | ||
self.port = self.config_details.get('daemon', {}).get('port', 8000) | ||
self.host = self.config_details.get('daemon', {}).get('host', '0.0.0.0') | ||
self.log_level = self.config_details.get('logging', {}).get('level', 'info') | ||
|
||
async def create_main_api_server(self): | ||
# Add config Details to Uvicorn | ||
config = uvicorn.Config(app=self.app, host=self.host, port=self.port, log_level=self.log_level) | ||
server = uvicorn.Server(config) | ||
await server.serve() | ||
|
||
def start(self): | ||
asyncio.run(self.create_main_api_server()) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import uvicorn | ||
from fastapi import FastAPI | ||
from pebblo.app.routers.routers import router_instance | ||
from pebblo.app.config.config import load_config | ||
import argparse | ||
|
||
from pebblo.topic_classifier.topic_classifier import TopicClassifier | ||
from pebblo.entity_classifier.entity_classifier import EntityClassifier | ||
|
||
config_details = {} | ||
|
||
|
||
def start(): | ||
global config_details | ||
# For loading config file details | ||
parser = argparse.ArgumentParser(description="Pebblo CLI") | ||
parser.add_argument('--config', type=str, help="Config file path") | ||
args = parser.parse_args() | ||
path = args.config | ||
config_details = load_config(path) | ||
|
||
# LazyLoading TopicClassifier and EntityClassifier to avoid circular dependency | ||
from pebblo.topic_classifier.topic_classifier import TopicClassifier | ||
from pebblo.entity_classifier.entity_classifier import EntityClassifier | ||
# Init TopicClassifier(This step downloads the models and put in cache) | ||
_ = TopicClassifier() | ||
# Init EntityClassifier(This step downloads all necessary training models) | ||
_ = EntityClassifier() | ||
|
||
# Initialise app instance | ||
app = FastAPI() | ||
|
||
# Register the router instance with the main app | ||
app.include_router(router_instance.router) | ||
|
||
# running local server | ||
uvicorn.run(app, host="localhost", port=8000, log_level="info") | ||
# Starting Uvicorn Service Using config details | ||
from pebblo.app.config.service import Service | ||
svc = Service(config_details) | ||
svc.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters