fix: use environment variables for VLM/LLM endpoints and models
- trace_vlm_caption.py: use MOMENTRY_LLM_VISION_URL and MOMENTRY_LLM_VISION_MODEL - scene_vlm_caption.py: use MOMENTRY_LLM_VISION_URL and MOMENTRY_LLM_VISION_MODEL - Changed from Ollama /api/generate to OpenAI-compatible /v1/chat/completions format - Added embedding server environment variables
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
"""
|
"""
|
||||||
Scene VLM Caption - Generate VLM descriptions for scene key frames
|
Scene VLM Caption - Generate VLM descriptions for scene key frames
|
||||||
|
|
||||||
Analyzes scene key frames ({uuid}_scene_N.jpg) using VLM (llava:7b).
|
Analyzes scene key frames ({uuid}_scene_N.jpg) using VLM.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
python scene_vlm_caption.py --file-uuid abc123 --output-dir /path/to/output
|
python scene_vlm_caption.py --file-uuid abc123 --output-dir /path/to/output
|
||||||
@@ -11,6 +11,10 @@ Usage:
|
|||||||
Output:
|
Output:
|
||||||
{output_dir}/{uuid}_scene_profile.json with:
|
{output_dir}/{uuid}_scene_profile.json with:
|
||||||
- scenes: [{scene_number, vlm_description, vlm_location, ...}]
|
- scenes: [{scene_number, vlm_description, vlm_location, ...}]
|
||||||
|
|
||||||
|
Environment Variables:
|
||||||
|
MOMENTRY_LLM_VISION_URL - VLM endpoint (default: http://localhost:8091/v1/chat/completions)
|
||||||
|
MOMENTRY_LLM_VISION_MODEL - VLM model (default: llava-v1.6-vicuna-13b)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
@@ -20,6 +24,10 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
# VLM configuration from environment variables
|
||||||
|
VLM_URL = os.environ.get("MOMENTRY_LLM_VISION_URL", "http://localhost:8091/v1/chat/completions")
|
||||||
|
VLM_MODEL = os.environ.get("MOMENTRY_LLM_VISION_MODEL", "llava-v1.6-vicuna-13b")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import requests
|
import requests
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -33,39 +41,44 @@ def encode_image(image_path: str) -> str:
|
|||||||
return base64.b64encode(f.read()).decode("utf-8")
|
return base64.b64encode(f.read()).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
def call_vlm(image_path: str, prompt: str, model: str = "llava:7b", ollama_url: str = "http://localhost:11434") -> str:
|
def call_vlm(image_path: str, prompt: str) -> str:
|
||||||
"""Call Ollama VLM API."""
|
"""Call VLM API using OpenAI-compatible format."""
|
||||||
image_b64 = encode_image(image_path)
|
image_b64 = encode_image(image_path)
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
"model": model,
|
"model": VLM_MODEL,
|
||||||
"prompt": prompt,
|
"messages": [
|
||||||
"images": [image_b64],
|
{"role": "user", "content": [
|
||||||
"stream": False,
|
{"type": "text", "text": prompt},
|
||||||
"options": {"num_predict": 100}
|
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}}
|
||||||
|
]}
|
||||||
|
],
|
||||||
|
"max_tokens": 100,
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = requests.post(f"{ollama_url}/api/generate", json=payload, timeout=30)
|
resp = requests.post(VLM_URL, json=payload, timeout=30)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
return data.get("response", "").strip()
|
return data.get("choices", [{}])[0].get("message", {}).get("content", "").strip()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[vlm] API error: {e}", file=sys.stderr)
|
print(f"[vlm] API error: {e}", file=sys.stderr)
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def get_embedding(text: str, model: str = "nomic-embed-text-v2-moe", ollama_url: str = "http://localhost:11434") -> list:
|
def get_embedding(text: str) -> list:
|
||||||
"""Get embedding from Ollama."""
|
"""Get embedding from embedding server."""
|
||||||
|
embed_url = os.environ.get("MOMENTRY_EMBEDDING_URL", "http://localhost:11436/v1/embeddings")
|
||||||
|
embed_model = os.environ.get("MOMENTRY_EMBEDDING_MODEL", "embeddinggemma-300m")
|
||||||
try:
|
try:
|
||||||
resp = requests.post(
|
resp = requests.post(
|
||||||
f"{ollama_url}/api/embed",
|
embed_url,
|
||||||
json={"model": model, "input": text},
|
json={"model": embed_model, "input": text},
|
||||||
timeout=30,
|
timeout=30,
|
||||||
)
|
)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
return data.get("embeddings", [[]])[0]
|
return data.get("data", [{}])[0].get("embedding", [])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[vlm] Embedding error: {e}", file=sys.stderr)
|
print(f"[vlm] Embedding error: {e}", file=sys.stderr)
|
||||||
return []
|
return []
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"""
|
"""
|
||||||
Trace VLM Caption - Generate VLM descriptions for face traces
|
Trace VLM Caption - Generate VLM descriptions for face traces
|
||||||
|
|
||||||
Analyzes key_face.jpg or key_frame.jpg using VLM (llava:7b) and updates trace_profile.json.
|
Analyzes key_face.jpg or key_frame.jpg using VLM and updates trace_profile.json.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
python trace_vlm_caption.py --trace-dir /path/to/output/{uuid}/trace_0
|
python trace_vlm_caption.py --trace-dir /path/to/output/{uuid}/trace_0
|
||||||
@@ -13,6 +13,10 @@ Output (13 fields):
|
|||||||
Environment: vlm_lighting, vlm_location, vlm_weather, vlm_setting, vlm_transportation
|
Environment: vlm_lighting, vlm_location, vlm_weather, vlm_setting, vlm_transportation
|
||||||
Nature: vlm_has_plants, vlm_plants, vlm_has_animals, vlm_animals
|
Nature: vlm_has_plants, vlm_plants, vlm_has_animals, vlm_animals
|
||||||
Context: vlm_background, vlm_bg_tags
|
Context: vlm_background, vlm_bg_tags
|
||||||
|
|
||||||
|
Environment Variables:
|
||||||
|
MOMENTRY_LLM_VISION_URL - VLM endpoint (default: http://localhost:8091/v1/chat/completions)
|
||||||
|
MOMENTRY_LLM_VISION_MODEL - VLM model (default: llava-v1.6-vicuna-13b)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
@@ -22,6 +26,10 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
# VLM configuration from environment variables
|
||||||
|
VLM_URL = os.environ.get("MOMENTRY_LLM_VISION_URL", "http://localhost:8091/v1/chat/completions")
|
||||||
|
VLM_MODEL = os.environ.get("MOMENTRY_LLM_VISION_MODEL", "llava-v1.6-vicuna-13b")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import requests
|
import requests
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -35,39 +43,44 @@ def encode_image(image_path: str) -> str:
|
|||||||
return base64.b64encode(f.read()).decode("utf-8")
|
return base64.b64encode(f.read()).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
def call_vlm(image_path: str, prompt: str, model: str = "llava:7b", ollama_url: str = "http://localhost:11434") -> str:
|
def call_vlm(image_path: str, prompt: str) -> str:
|
||||||
"""Call Ollama VLM API."""
|
"""Call VLM API using OpenAI-compatible format."""
|
||||||
image_b64 = encode_image(image_path)
|
image_b64 = encode_image(image_path)
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
"model": model,
|
"model": VLM_MODEL,
|
||||||
"prompt": prompt,
|
"messages": [
|
||||||
"images": [image_b64],
|
{"role": "user", "content": [
|
||||||
"stream": False,
|
{"type": "text", "text": prompt},
|
||||||
"options": {"num_predict": 100}
|
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_b64}"}}
|
||||||
|
]}
|
||||||
|
],
|
||||||
|
"max_tokens": 100,
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = requests.post(f"{ollama_url}/api/generate", json=payload, timeout=30)
|
resp = requests.post(VLM_URL, json=payload, timeout=30)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
return data.get("response", "").strip()
|
return data.get("choices", [{}])[0].get("message", {}).get("content", "").strip()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"VLM API error: {e}", file=sys.stderr)
|
print(f"VLM API error: {e}", file=sys.stderr)
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def get_embedding(text: str, model: str = "nomic-embed-text-v2-moe", ollama_url: str = "http://localhost:11434") -> list:
|
def get_embedding(text: str) -> list:
|
||||||
"""Get embedding from Ollama."""
|
"""Get embedding from embedding server."""
|
||||||
|
embed_url = os.environ.get("MOMENTRY_EMBEDDING_URL", "http://localhost:11436/v1/embeddings")
|
||||||
|
embed_model = os.environ.get("MOMENTRY_EMBEDDING_MODEL", "embeddinggemma-300m")
|
||||||
try:
|
try:
|
||||||
resp = requests.post(
|
resp = requests.post(
|
||||||
f"{ollama_url}/api/embed",
|
embed_url,
|
||||||
json={"model": model, "input": text},
|
json={"model": embed_model, "input": text},
|
||||||
timeout=30,
|
timeout=30,
|
||||||
)
|
)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
return data.get("embeddings", [[]])[0]
|
return data.get("data", [{}])[0].get("embedding", [])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[vlm] Embedding error: {e}", file=sys.stderr)
|
print(f"[vlm] Embedding error: {e}", file=sys.stderr)
|
||||||
return []
|
return []
|
||||||
|
|||||||
Reference in New Issue
Block a user