Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Backend #8

Merged
merged 129 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
2e75d28
Merge branch 'main' of https://github.com/PerfectThymeTech/video-anal…
marvinbuss Oct 11, 2024
c1949a0
Update requirements
marvinbuss Oct 11, 2024
0dd2471
Add workflows
marvinbuss Oct 11, 2024
6972d9b
Add tests directory
marvinbuss Oct 11, 2024
4085dba
Add backend baseline
marvinbuss Oct 15, 2024
05ceeb6
Merge branch 'main' of https://github.com/PerfectThymeTech/video-anal…
marvinbuss Oct 16, 2024
7301df8
Update locals and role assignments
marvinbuss Oct 17, 2024
a06ded7
Update backend
marvinbuss Oct 17, 2024
2e4f3e3
Update config and logging utils
marvinbuss Oct 17, 2024
db250db
remove
marvinbuss Oct 17, 2024
ac8a498
Merge branch 'main' of https://github.com/PerfectThymeTech/video-anal…
marvinbuss Oct 17, 2024
25b850f
Update speech name
marvinbuss Oct 17, 2024
7ba1462
Update app settings
marvinbuss Oct 17, 2024
d06ea6a
lint
marvinbuss Oct 17, 2024
694448e
fix bug
marvinbuss Oct 17, 2024
a601a96
Update tests
marvinbuss Oct 21, 2024
1095517
Update ignore files
marvinbuss Oct 21, 2024
90cd57d
Update dependencies and config
marvinbuss Oct 21, 2024
3318483
Add baseline function code
marvinbuss Oct 21, 2024
1665550
Lint
marvinbuss Oct 21, 2024
51b41fe
Update terraform version
marvinbuss Oct 21, 2024
cb62ead
Update function nanme
marvinbuss Oct 21, 2024
c7d0524
Update dependencies
marvinbuss Oct 21, 2024
30d4ef4
Update test template
marvinbuss Oct 21, 2024
c87214b
Add IDs to workflow steps
marvinbuss Oct 21, 2024
2700d95
disable tests
marvinbuss Oct 21, 2024
55616fe
Add missing identity and role assignment
marvinbuss Oct 21, 2024
867362a
Update identity
marvinbuss Oct 21, 2024
e53aa0e
Update Function config
marvinbuss Oct 22, 2024
f6aa135
cleanup imports for utils
marvinbuss Oct 22, 2024
f6f21cc
Add variable for function result
marvinbuss Oct 22, 2024
dea1a44
Cleanup imports of videoextraction utils
marvinbuss Oct 22, 2024
8b1a0b9
Add speech client class
marvinbuss Oct 22, 2024
55a16fc
lint
marvinbuss Oct 22, 2024
37b45c3
Update secret store for function
marvinbuss Oct 22, 2024
290f5d0
Update function route
marvinbuss Oct 22, 2024
ff6beb7
Add role assignments for blob trigger
marvinbuss Oct 22, 2024
e25ed1f
Add blob trigger connection settings
marvinbuss Oct 22, 2024
513668c
Update function code
marvinbuss Oct 22, 2024
b5b68c2
Remove function name
marvinbuss Oct 22, 2024
2040c41
test without helath app
marvinbuss Oct 22, 2024
8ab3270
Try again
marvinbuss Oct 22, 2024
20b4645
Test again
marvinbuss Oct 22, 2024
23a7d33
Update function
marvinbuss Oct 22, 2024
24ca8db
Add app setting
marvinbuss Oct 22, 2024
d1c7ba9
Add health blueprint
marvinbuss Oct 22, 2024
a678fac
Lint
marvinbuss Oct 22, 2024
7003589
test
marvinbuss Oct 22, 2024
89b0e00
Register second blueprint
marvinbuss Oct 22, 2024
b4510ad
Tets other trigger
marvinbuss Oct 22, 2024
8fe7ab9
Add mising permissions
marvinbuss Oct 22, 2024
1db5a64
Add missing config
marvinbuss Oct 22, 2024
8e4e2c8
Test other blob trigger
marvinbuss Oct 22, 2024
caad729
lint
marvinbuss Oct 22, 2024
5ec4855
Update trigger again
marvinbuss Oct 22, 2024
0de5d48
Update trigger
marvinbuss Oct 22, 2024
8308211
Remove one trigger
marvinbuss Oct 22, 2024
c604440
test with health
marvinbuss Oct 22, 2024
6d4e556
Test function name
marvinbuss Oct 22, 2024
4ceb280
Enable built-in logging
marvinbuss Oct 22, 2024
f5c124a
Update function code
marvinbuss Oct 22, 2024
15c9cd9
Enable public network access
marvinbuss Oct 22, 2024
000ff15
lint
marvinbuss Oct 22, 2024
a4a35f6
Disable most of the function code
marvinbuss Oct 22, 2024
550a32c
Add missing dependency
marvinbuss Oct 22, 2024
79f6c92
Enable function
marvinbuss Oct 22, 2024
34c58e2
Update guid function
marvinbuss Oct 22, 2024
b5ee5a4
Fix reference bug
marvinbuss Oct 22, 2024
b0cf723
Update dependency
marvinbuss Oct 22, 2024
3d3625b
Add managed identity client id as config
marvinbuss Oct 22, 2024
578d475
Add missing dependency
marvinbuss Oct 22, 2024
18ab7d3
lint
marvinbuss Oct 22, 2024
a30ba09
Fix bug in code to create directory path
marvinbuss Oct 22, 2024
858e433
Fix bug in directory creation
marvinbuss Oct 22, 2024
54e69a8
Update role assignments
marvinbuss Oct 23, 2024
c1f7895
Add local auth for ai speech
marvinbuss Oct 23, 2024
0b34849
Enable key bsed access in function
marvinbuss Oct 23, 2024
483066b
Add speech resource ID reference and update base url
marvinbuss Oct 23, 2024
70a8977
Smaller fixes for speech config
marvinbuss Oct 23, 2024
201f5d7
Update secret reference
marvinbuss Oct 23, 2024
9021c84
Update speech client
marvinbuss Oct 23, 2024
117b556
lint
marvinbuss Oct 23, 2024
29aeea5
Update logging config
marvinbuss Oct 23, 2024
b890cd7
Update locals
marvinbuss Oct 23, 2024
8a1f20d
lint
marvinbuss Oct 23, 2024
956a30e
Update utils for copy activity
marvinbuss Oct 23, 2024
558c1f9
Ad delete operation
marvinbuss Oct 23, 2024
6f6c838
minor bugfix
marvinbuss Oct 24, 2024
d779b16
Update function config
marvinbuss Oct 24, 2024
e98f267
Remove manual app setting and prepare aad auth to app insights
marvinbuss Oct 24, 2024
e2e4072
prepare aad auth to app insights
marvinbuss Oct 24, 2024
1559b12
Add logging for health endpoint
marvinbuss Oct 24, 2024
d613375
Add function to upload from url
marvinbuss Oct 24, 2024
95a82b9
Extend function logic
marvinbuss Oct 24, 2024
b09463f
Update model to latest api version
marvinbuss Oct 24, 2024
2703fd4
Add prompts
marvinbuss Oct 24, 2024
efc66eb
Add main content language as input variable
marvinbuss Oct 24, 2024
9652d14
Update model structure for health endpoint
marvinbuss Oct 24, 2024
cb4785d
Add config to python level
marvinbuss Oct 24, 2024
9f460ce
Add missing umi reference for file upload in function
marvinbuss Oct 24, 2024
c01834c
Add word level timestamps for speech service
marvinbuss Oct 24, 2024
cb7f33b
Add langchain to requirements
marvinbuss Oct 24, 2024
b8cc577
Add speech analysis function
marvinbuss Oct 24, 2024
6830ce7
Test langchain 0.3
marvinbuss Oct 24, 2024
af3dc13
Add AI Speech analysis
marvinbuss Oct 24, 2024
a5d5ebf
Fix missing await
marvinbuss Oct 24, 2024
488b4e6
Update blob domain name
marvinbuss Oct 24, 2024
6a8eecc
Register ai speech function
marvinbuss Oct 24, 2024
b5a4881
lint
marvinbuss Oct 24, 2024
5a79f31
Add missing umi reference
marvinbuss Oct 24, 2024
436c1e8
Allow all storage copy activities
marvinbuss Oct 24, 2024
ca05cce
Add save results
marvinbuss Oct 24, 2024
b0fa526
Update terraform provider azapi
marvinbuss Oct 25, 2024
cff3641
Update to azapi v2
marvinbuss Oct 25, 2024
e39b45f
Fix model conversion in function
marvinbuss Oct 25, 2024
3778e47
Enhance generation of blob properties
marvinbuss Oct 25, 2024
4e71915
Update System prompt
marvinbuss Oct 25, 2024
d5ed06e
Add timestamp calculation to function
marvinbuss Oct 25, 2024
7a234f7
Update auth for blob properties and make call async
marvinbuss Oct 25, 2024
1191278
Lint
marvinbuss Oct 25, 2024
d852f14
Update file name
marvinbuss Oct 25, 2024
51db5cc
Update path and reference json instead of string
marvinbuss Oct 25, 2024
9e86a18
Update reference in function
marvinbuss Oct 25, 2024
e78715d
Fix reference bug in timestamp calculation
marvinbuss Oct 25, 2024
0e58d5e
Fix naming of function parameter
marvinbuss Oct 25, 2024
71ed3d2
Update order of steps in function
marvinbuss Oct 25, 2024
357721e
Remove verbose exception info from warning log
marvinbuss Oct 25, 2024
c03530e
Update to json dumps
marvinbuss Oct 25, 2024
5fdd313
Small fix for indexing
marvinbuss Oct 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions .github/workflows/_functionDeployTemplate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: Function App Deploy Template

on:
workflow_call:
inputs:
environment:
required: true
type: string
default: "dev"
description: "Specifies the environment of the deployment."
python_version:
required: true
type: string
default: "3.11"
description: "Specifies the python version."
function_directory:
required: true
type: string
description: "Specifies the directory of the Azure Web App."
function_name:
required: true
type: string
description: "Specifies the name of the Azure Web App."
tenant_id:
required: true
type: string
description: "Specifies the tenant id of the deployment."
subscription_id:
required: true
type: string
description: "Specifies the subscription id of the deployment."
secrets:
CLIENT_ID:
required: true
description: "Specifies the client id."

permissions:
id-token: write
contents: read

jobs:
deployment:
name: Function App Deploy
runs-on: [self-hosted]
continue-on-error: false
environment: ${{ inputs.environment }}
concurrency:
group: function-${{ inputs.function_name }}-${{ inputs.environment }}
cancel-in-progress: false

steps:
# Check Out Repository
- name: Check Out Repository
id: checkout_repository
uses: actions/checkout@v4

# Setup Python
- name: Setup Python
id: python_setup
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python_version }}
cache: "pip"
cache-dependency-path: |
${{ inputs.function_directory }}/requirements.txt

# Install Web App Dependencies
- name: Resolve Web App Dependencies
id: function_dependencies
shell: bash
run: |
pushd "${WEBAPP_DIRECTORY}"
python -m pip install --upgrade pip
pip install -r requirements.txt --target=".python_packages/lib/site-packages"
popd
env:
WEBAPP_DIRECTORY: ${{ inputs.function_directory }}

# Azure login
- name: Azure login
id: azure_login
uses: azure/login@v2
with:
client-id: ${{ secrets.CLIENT_ID }}
tenant-id: ${{ inputs.tenant_id }}
subscription-id: ${{ inputs.subscription_id }}

# Deploy Function
- name: Deploy Function
id: function_deploy
uses: Azure/functions-action@v1
with:
app-name: ${{ inputs.function_name }}
package: ${{ inputs.function_directory }}
scm-do-build-during-deployment: true
enable-oryx-build: true
48 changes: 48 additions & 0 deletions .github/workflows/_functionTestTemplate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Function App Test Template

on:
workflow_call:
inputs:
python_version:
required: true
type: string
default: "3.11"
description: "Specifies the python version."
function_directory:
required: true
type: string
description: "Specifies the directory of the Azure Web App."

jobs:
test:
name: Function App Test
runs-on: [ubuntu-latest]
continue-on-error: false

steps:
# Check Out Repository
- name: Check Out Repository
id: checkout_repository
uses: actions/checkout@v4

# Setup Python
- name: Setup Python
id: python_setup
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python_version }}
cache: "pip"
cache-dependency-path: |
${{ inputs.function_directory }}/requirements.txt
requirements.txt

# Run Python Tests
- name: Run Python Tests
id: python_test
working-directory: ${{ inputs.function_directory }}
run: |
pip install -r "./../../requirements.txt" -q
pip install -r "./requirements.txt" -q
pytest
env:
FUNCTION_DIRECTORY: ${{ inputs.function_directory }}
2 changes: 2 additions & 0 deletions .github/workflows/_terraformDestroyTemplate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ jobs:

# Terraform Init
- name: Terraform Init
id: terraform_init
working-directory: ${{ inputs.working_directory }}
run: |
terraform init -backend-config=../../config/${CONFIG}/azurerm.tfbackend
Expand All @@ -89,6 +90,7 @@ jobs:

# Terraform Destroy
- name: Terraform Destroy
id: terraform_destroy
working-directory: ${{ inputs.working_directory }}
run: |
terraform apply -var-file="../../config/${CONFIG}/vars.tfvars" -auto-approve -input=false -destroy
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/_terraformEnvironmentTemplate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ jobs:

# Terraform Init
- name: Terraform Init
id: terraform_init
working-directory: ${{ inputs.working_directory }}
run: |
terraform init -backend-config=../../config/${CONFIG}/azurerm.tfbackend
Expand All @@ -231,6 +232,7 @@ jobs:

# Terraform Apply
- name: Terraform Apply
id: terraform_apply
working-directory: ${{ inputs.working_directory }}
run: |
terraform apply -var-file="../../config/${CONFIG}/vars.tfvars" -auto-approve -input=false
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/function.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Function App Deployment
on:
push:
branches:
- main
paths:
- "**.py"
- "code/backend/**"
- "config/**"
- "tests/**"
- "requirements.txt"

pull_request:
branches:
- main
paths:
- "**.py"
- "code/backend/**"
- "config/**"
- "tests/**"
- "requirements.txt"

jobs:
# function_test:
# uses: ./.github/workflows/_functionTestTemplate.yml
# name: "Web App Test"
# with:
# python_version: "3.11"
# function_directory: "./code/backend"

function_dev:
uses: ./.github/workflows/_functionDeployTemplate.yml
name: "Web App - Dev"
# needs: [function_test]
with:
environment: "dev"
python_version: "3.11"
function_directory: "./code/backend"
function_name: "vdnlytcs-dev-app001"
tenant_id: "37963dd4-f4e6-40f8-a7d6-24b97919e452"
subscription_id: "1fdab118-1638-419a-8b12-06c9543714a0"
secrets:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
id: terraform_setup
uses: hashicorp/setup-terraform@v3
with:
terraform_version: "1.9.6"
terraform_version: "1.9.8"
terraform_wrapper: true

# Checkout repository
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
with:
environment: "dev"
config: "PerfectThymeTech"
terraform_version: "1.9.6"
terraform_version: "1.9.8"
node_version: 20
working_directory: "./code/infra"
tenant_id: "37963dd4-f4e6-40f8-a7d6-24b97919e452"
Expand All @@ -42,7 +42,7 @@ jobs:
with:
environment: "dev"
config: "PerfectThymeTech"
terraform_version: "1.9.6"
terraform_version: "1.9.8"
node_version: 20
working_directory: "./code/infra"
tenant_id: "37963dd4-f4e6-40f8-a7d6-24b97919e452"
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v5.0.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
Expand All @@ -15,7 +15,7 @@ repos:
- id: isort
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/psf/black
rev: 23.12.1
rev: 24.10.0
hooks:
- id: black
- repo: local
Expand Down
10 changes: 10 additions & 0 deletions code/backend/.funcignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.git*
.vscode
__azurite_db*__.json
__blobstorage__
__queuestorage__
__pycache__
local.settings.json
test
venv
.vscode
Empty file.
84 changes: 84 additions & 0 deletions code/backend/aispeechanalysis/function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import json
import logging

import azure.functions as func
import azurefunctions.extensions.bindings.blob as blob
from aispeechanalysis.llm import LlmClient
from aispeechanalysis.utils import get_timestamps_for_sections, get_transcript
from shared.config import settings
from shared.utils import load_blob, upload_string

bp = func.Blueprint()


@bp.function_name("AiSpeechAnalysis")
@bp.blob_trigger(
arg_name="client",
path="internal-analysis-speech/{name}",
connection="BlobTrigger",
# source="LogsAndContainerScan",
)
async def ai_speech_analysis(client: blob.BlobClient) -> func.HttpResponse:
logging.info("Azure AI Speech file upload detected.")

# Initialize
logging.info("Initialize")
ai_speech_analysis_guid = str.split(client.blob_name, sep="/")[0]

# Download blob file content and convert to json
logging.info("Download blob file content and convert to json.")
result_load_blob = await load_blob(
storage_domain_name=f"{client.account_name}.blob.core.windows.net",
storage_container_name=client.container_name,
storage_blob_name=client.blob_name,
encoding="utf-8",
managed_identity_client_id=settings.MANAGED_IDENTITY_CLIENT_ID,
)
result_load_blob_json = json.loads(result_load_blob)
logging.debug(f"Loaded blob content as json: '{result_load_blob_json}'")

# Get transcript
logging.info("Get transcript from Azure AI Speech content.")
result_get_transcript = get_transcript(ai_speech_blob_json=result_load_blob_json)

# Use Open AI to generate scenes
logging.info("Use Open AI to generate scenes.")
llm_client = LlmClient(
azure_open_ai_base_url=settings.AZURE_OPEN_AI_BASE_URL,
azure_open_ai_api_version=settings.AZURE_OPEN_AI_API_VERSION,
azure_open_ai_deployment_name=settings.AZURE_OPEN_AI_DEPLOYMENT_NAME,
azure_open_ai_temperature=settings.AZURE_OPEN_AI_TEMPERATURE,
managed_identity_client_id=settings.MANAGED_IDENTITY_CLIENT_ID,
)
result_invoke_llm_chain = llm_client.invoke_llm_chain(
news_content=result_get_transcript,
news_show_details="This is a news show covering different news content.",
language=settings.MAIN_CONTENT_LANGUAGE,
)

# Save llm result
logging.info("Saving LLM result.")
_ = await upload_string(
data=result_invoke_llm_chain.model_dump_json(),
storage_domain_name=f"{client.account_name}.blob.core.windows.net",
storage_container_name=settings.STORAGE_CONTAINER_INTERNAL_ANALYSIS_SPEECH_NAME,
storage_blob_name=f"{ai_speech_analysis_guid}/llm.json",
managed_identity_client_id=settings.MANAGED_IDENTITY_CLIENT_ID,
)

# Get timestamps for news sections
logging.info("Get timestamps for news sections")
result_get_timestamps_for_sections = get_timestamps_for_sections(
result_stt=result_load_blob_json,
result_llm=result_invoke_llm_chain.model_dump(),
)

# Save results
logging.info("Save results")
_ = await upload_string(
data=json.dumps(result_get_timestamps_for_sections),
storage_domain_name=f"{client.account_name}.blob.core.windows.net",
storage_container_name=settings.STORAGE_CONTAINER_RESULTS_NAME,
storage_blob_name=f"{ai_speech_analysis_guid}/timestamps.json",
managed_identity_client_id=settings.MANAGED_IDENTITY_CLIENT_ID,
)
Loading
Loading