Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,103 +1,106 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
logger.
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#https://www.youtube.com/watch?v=AREBu2B5H3M&t=140s
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import click
|
| 5 |
+
import logging
|
| 6 |
+
import sys
|
| 7 |
+
import yaml
|
| 8 |
+
import os
|
| 9 |
+
from langchain.llms import AzureOpenAI, OpenAI
|
| 10 |
+
from app.api_funcs import get_job_infos, get_run, get_model, \
|
| 11 |
+
trans_model, batch_mod_permission, prepare_api_docs
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
logger.setLevel(logging.DEBUG)
|
| 15 |
+
handler = logging.StreamHandler(sys.stdout)
|
| 16 |
+
handler.setLevel(logging.DEBUG)
|
| 17 |
+
logger.addHandler(handler)
|
| 18 |
+
|
| 19 |
+
PATH = Path(os.path.abspath(os.path.dirname(__file__)))
|
| 20 |
+
|
| 21 |
+
# Open the YAML file
|
| 22 |
+
conf_path = PATH / "app" / 'llm.yaml'
|
| 23 |
+
with open(conf_path) as f:
|
| 24 |
+
config = yaml.safe_load(f)
|
| 25 |
+
|
| 26 |
+
# Use AzureOpenAI, if config contains deployment name, otherwise OpenAI
|
| 27 |
+
if config['model'].get('deployment_name', False):
|
| 28 |
+
llm = AzureOpenAI(**config['model'])
|
| 29 |
+
else:
|
| 30 |
+
llm = OpenAI(**config['model'])
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
headers = {"Authorization": f"Bearer {os.getenv('DBR_BEARER_TOKEN')}"}
|
| 34 |
+
updated_api_docs = prepare_api_docs()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def comma_list(comma_str: str):
|
| 38 |
+
return comma_str.split(',')
|
| 39 |
+
|
| 40 |
+
def determine_api_text(updated_api_docs: dict, query: str):
|
| 41 |
+
pick_api_prompt = """Please return the file name from the list {api_docs}
|
| 42 |
+
that best corresponds to the following query: {query}. \
|
| 43 |
+
DO NOT EXPLAIN your answer!
|
| 44 |
+
"""
|
| 45 |
+
api_docs = os.listdir(PATH / "app" / "dbr_api_docs")
|
| 46 |
+
selected_api_doc = llm(pick_api_prompt.format(api_docs=api_docs, query=query)).lstrip().rstrip()
|
| 47 |
+
logger.info(f"\nSelecting the following api document: {selected_api_doc}")
|
| 48 |
+
api_text = updated_api_docs[selected_api_doc]
|
| 49 |
+
return api_text, selected_api_doc
|
| 50 |
+
|
| 51 |
+
# Add subcommands for commands
|
| 52 |
+
@click.group()
|
| 53 |
+
def cli():
|
| 54 |
+
pass
|
| 55 |
+
|
| 56 |
+
@cli.group(help='Run machine learning model.')
|
| 57 |
+
def ml():
|
| 58 |
+
pass
|
| 59 |
+
|
| 60 |
+
# Add commands for specific subcommands of 'ml'
|
| 61 |
+
@ml.command(help='Get information about a model.')
|
| 62 |
+
@click.argument('query', type=str)
|
| 63 |
+
def get_model_info(query):
|
| 64 |
+
# Instruction to get model infos
|
| 65 |
+
api_text, _ = determine_api_text(updated_api_docs, query)
|
| 66 |
+
logger.info(get_model(llm, query, api_text, headers))
|
| 67 |
+
|
| 68 |
+
@ml.command(help='Get information about a model run.')
|
| 69 |
+
@click.argument('run_id', type=str)
|
| 70 |
+
@click.argument('query', type=str)
|
| 71 |
+
def get_run_info(query, run_id):
|
| 72 |
+
# ID of the model run for which you'd like information.
|
| 73 |
+
# Which information should be pulled from the run?
|
| 74 |
+
api_text, _ = determine_api_text(updated_api_docs, query)
|
| 75 |
+
logger.info(get_run(llm, run_id, query, api_text, headers))
|
| 76 |
+
|
| 77 |
+
@ml.command(help='Transition a model from one state to another.')
|
| 78 |
+
@click.argument('query', type=str)
|
| 79 |
+
def transition_model(query):
|
| 80 |
+
# Instruction to transition a model.
|
| 81 |
+
api_text, _ = determine_api_text(updated_api_docs, query)
|
| 82 |
+
trans_model(llm, query, api_text, headers)
|
| 83 |
+
|
| 84 |
+
@cli.command(help='View job history.')
|
| 85 |
+
@click.argument('query', type=str)
|
| 86 |
+
def jobs(query):
|
| 87 |
+
if ";" not in query:
|
| 88 |
+
query = query + ";"
|
| 89 |
+
query, response_query = query.split(";")
|
| 90 |
+
api_text, _ = determine_api_text(updated_api_docs, query)
|
| 91 |
+
# The query for the LLM + an optional query for the API response
|
| 92 |
+
logger.info(get_job_infos(llm, query, response_query, api_text, headers))
|
| 93 |
+
|
| 94 |
+
@cli.command(help='Manage user permissions.')
|
| 95 |
+
@click.argument('query', type=str)
|
| 96 |
+
@click.argument('jobs', type=comma_list)
|
| 97 |
+
def permissions(jobs, query):
|
| 98 |
+
api_text, api_name = determine_api_text(updated_api_docs, query)
|
| 99 |
+
# Add/Get user permissions.
|
| 100 |
+
batch_mod_permission(
|
| 101 |
+
logger, llm, updated_api_docs, api_text, api_name, headers,
|
| 102 |
+
query, jobs=jobs
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
if __name__ == '__main__':
|
| 106 |
+
cli()
|