Compare commits
5
Commits
9a3694fc98
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5faf3ef2a7 | ||
|
|
007161a927 | ||
|
|
d804cc4a13 | ||
|
|
6075848c91 | ||
|
|
cdde0131a7 |
@@ -14,6 +14,7 @@ classifiers = [
|
|||||||
"Operating System :: OS Independent",
|
"Operating System :: OS Independent",
|
||||||
]
|
]
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"mlflow>=3.4.0",
|
||||||
"pandas>=2.3.2",
|
"pandas>=2.3.2",
|
||||||
"pillow>=11.3.0",
|
"pillow>=11.3.0",
|
||||||
"pydantic>=2.11.9",
|
"pydantic>=2.11.9",
|
||||||
@@ -82,4 +83,5 @@ dev = [
|
|||||||
"ruff>=0.13.0",
|
"ruff>=0.13.0",
|
||||||
"safety>=3.2.11",
|
"safety>=3.2.11",
|
||||||
"testcontainers>=4.13.0",
|
"testcontainers>=4.13.0",
|
||||||
|
"types-requests>=2.32.4.20250913",
|
||||||
]
|
]
|
||||||
|
|||||||
+52
-16
@@ -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,7 +23,9 @@ from data_store.dto import (
|
|||||||
PointOfViewEnum,
|
PointOfViewEnum,
|
||||||
)
|
)
|
||||||
|
|
||||||
MODEL = "llava:13b"
|
RUN_NAME = f"query-ai-model_{datetime.now().strftime('%Y%m%d-%H%M%S')}"
|
||||||
|
IMAGE_MODEL = "llava:13b"
|
||||||
|
PROMPT_MODEL = "mistral-small:22b-instruct-2409-q4_K_M"
|
||||||
CATEGORY_PROMPT_MAP = {
|
CATEGORY_PROMPT_MAP = {
|
||||||
"angle": (
|
"angle": (
|
||||||
"In the context of visual angle in the Kress and van Leeuwen framework, "
|
"In the context of visual angle in the Kress and van Leeuwen framework, "
|
||||||
@@ -201,6 +205,13 @@ def calculate_model_prompt_accuracy(
|
|||||||
total_score += score
|
total_score += score
|
||||||
# Calculate accuracy
|
# Calculate accuracy
|
||||||
jobs_evaluated = total_jobs - jobs_skipped
|
jobs_evaluated = total_jobs - jobs_skipped
|
||||||
|
mlflow.log_metrics(
|
||||||
|
{
|
||||||
|
"total_jobs": total_jobs,
|
||||||
|
"jobs_skipped": jobs_skipped,
|
||||||
|
"jobs_evaluated": jobs_evaluated,
|
||||||
|
}
|
||||||
|
)
|
||||||
print(
|
print(
|
||||||
f"Total Jobs: {total_jobs}, Jobs Skipped: {jobs_skipped}, Jobs Evaluated: {jobs_evaluated}"
|
f"Total Jobs: {total_jobs}, Jobs Skipped: {jobs_skipped}, Jobs Evaluated: {jobs_evaluated}"
|
||||||
)
|
)
|
||||||
@@ -270,19 +281,51 @@ 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 = {}
|
|
||||||
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={
|
||||||
|
"image_model": IMAGE_MODEL,
|
||||||
|
"prompt_model": PROMPT_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(IMAGE_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
|
||||||
|
try:
|
||||||
|
# Generate refined prompt
|
||||||
|
refined_prompt = refine_prompt(PROMPT_MODEL, prompt_accuracy_map)
|
||||||
|
mlflow.log_param(f"step_{step}_prompt", refined_prompt)
|
||||||
print(f"Refined prompt: {refined_prompt}")
|
print(f"Refined prompt: {refined_prompt}")
|
||||||
accuracy = calculate_model_prompt_accuracy(MODEL, refined_prompt, category)
|
except Exception as e:
|
||||||
prompt_accuracy_map[refined_prompt] = accuracy
|
print(f"Error refining prompt: {e}")
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
# Calculate accuracy for refined prompt
|
||||||
|
accuracy = calculate_model_prompt_accuracy(
|
||||||
|
IMAGE_MODEL, refined_prompt, category
|
||||||
|
)
|
||||||
|
mlflow.log_metric("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 +333,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'.")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user