added code to log to mlflow
Formatting Check / formatting-check (push) Successful in 11s
Python Code Quality / python-code-quality (push) Failing after 19s
Python Test / python-test (push) Successful in 40s

This commit is contained in:
Brian Bjarke Jensen
2025-09-25 23:57:57 +02:00
parent cdde0131a7
commit 6075848c91
+40 -14
View File
@@ -9,6 +9,8 @@ import requests
import structlog import structlog
from dotenv import load_dotenv from dotenv import load_dotenv
from PIL import Image from PIL import Image
from datetime import datetime
import mlflow
from python_utils import check_env from python_utils import check_env
from data_store.repositories import JobRepository from data_store.repositories import JobRepository
from data_store.dto import ( from data_store.dto import (
@@ -21,6 +23,7 @@ from data_store.dto import (
PointOfViewEnum, PointOfViewEnum,
) )
RUN_NAME = f"query-ai-model_{datetime.now().strftime('%Y%m%d-%H%M%S')}"
MODEL = "llava:13b" MODEL = "llava:13b"
CATEGORY_PROMPT_MAP = { CATEGORY_PROMPT_MAP = {
"angle": ( "angle": (
@@ -270,19 +273,49 @@ if __name__ == "__main__":
) )
# Set up basic logging configuration # Set up basic logging configuration
logging.basicConfig(level=logging.ERROR) logging.basicConfig(level=logging.ERROR)
# Connect to MLFlow
mlflow.set_tracking_uri("http://10.0.0.2:5000")
experiment = mlflow.get_experiment_by_name("visual-semiotic-ai-analysis")
experiment_id = experiment.experiment_id if experiment else None
# Calculate accuracy # Calculate accuracy
category_events = {} category_events = {}
for category, initial_prompt in CATEGORY_PROMPT_MAP.items(): for category, initial_prompt in CATEGORY_PROMPT_MAP.items():
step = 0
run = mlflow.start_run(
experiment_id=experiment_id,
run_name=RUN_NAME,
tags={
"model": MODEL,
"category": category,
},
)
mlflow.log_param(f"step_{step}_prompt", initial_prompt)
print(f"Calculating accuracy for category: {category}") print(f"Calculating accuracy for category: {category}")
accuracy = calculate_model_prompt_accuracy(MODEL, initial_prompt, category) accuracy = calculate_model_prompt_accuracy(MODEL, initial_prompt, category)
mlflow.log_metric("accuracy", accuracy, step=step)
# Log to MLFlow
prompt_accuracy_map = {initial_prompt: accuracy} prompt_accuracy_map = {initial_prompt: accuracy}
print(f"Prompt accuracy for category '{category}': {accuracy:.2%}") print(f"Prompt accuracy for category '{category}': {accuracy:.2%}")
# loop to refine prompt based on accuracy # Loop to refine prompt based on accuracy
for i in range(30): for i in range(99):
refined_prompt = refine_prompt(MODEL, prompt_accuracy_map) step += 1
print(f"Refined prompt: {refined_prompt}") try:
accuracy = calculate_model_prompt_accuracy(MODEL, refined_prompt, category) # Generate refined prompt
prompt_accuracy_map[refined_prompt] = accuracy refined_prompt = refine_prompt(MODEL, prompt_accuracy_map)
mlflow.log_param(f"step_{step}_prompt", refined_prompt)
print(f"Refined prompt: {refined_prompt}")
except Exception as e:
print(f"Error refining prompt: {e}")
continue
try:
# Calculate accuracy for refined prompt
accuracy = calculate_model_prompt_accuracy(MODEL, refined_prompt, category)
mlflow.log_metric(f"accuracy", accuracy, step=step)
except Exception as e:
print(f"Error calculating accuracy: {e}")
continue
# Update prompt accuracy map
# prompt_accuracy_map[refined_prompt] = accuracy
print(f"Prompt accuracy for category '{category}': {accuracy:.2%}") print(f"Prompt accuracy for category '{category}': {accuracy:.2%}")
# Stop if accuracy is 100% # Stop if accuracy is 100%
if accuracy == 1.0: if accuracy == 1.0:
@@ -290,11 +323,4 @@ if __name__ == "__main__":
f"Achieved 100% accuracy for category '{category}'. Stopping refinement." f"Achieved 100% accuracy for category '{category}'. Stopping refinement."
) )
break break
print( mlflow.end_run()
f"Final prompt accuracy map for category '{category}': {prompt_accuracy_map}"
)
category_events[category] = prompt_accuracy_map
# save results to json file
with open("prompt_accuracy_results.json", "w", encoding="utf-8") as f:
json.dump(category_events, f, indent=2)
print("Prompt accuracy results saved to 'prompt_accuracy_results.json'.")