forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenglass.py
More file actions
65 lines (55 loc) · 2.25 KB
/
Copy pathopenglass.py
File metadata and controls
65 lines (55 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
from typing import Any, Protocol, cast
from utils.llm.clients import get_llm
from utils.llm.usage_tracker import track_usage, Features
MessagePart = dict[str, object]
ChatMessage = dict[str, object]
class AsyncVisionLlm(Protocol):
async def ainvoke(
self,
input: object,
*,
config: dict[str, Any] | None = None,
max_completion_tokens: int | None = None,
) -> object: ...
def _response_text(response: object) -> str:
content = getattr(response, "content", response)
if isinstance(content, str):
return content
if isinstance(content, list):
parts: list[str] = []
for item in cast(list[object], content):
if isinstance(item, str):
parts.append(item)
elif isinstance(item, dict):
block = cast(dict[str, object], item)
text = block.get("text") or block.get("content") or ""
if text:
parts.append(str(text))
elif item is not None:
parts.append(str(item))
return "".join(parts)
return "" if content is None else str(content)
async def describe_image(uid: str, base64_data: str, content_type: str = "image/jpeg") -> str:
"""
Generates a description for a base64 encoded image using a vision model via LangChain.
"""
prompt = (
"You are my AI assistant, seeing the world through my smart glasses. In a single, descriptive paragraph, "
"tell me what's happening from a first-person perspective. Focus on the most important aspects of the scene: "
"the people, their actions, the key objects, and the overall environment. What is the general mood or atmosphere?"
)
content: list[MessagePart] = [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {"url": f"data:{content_type};base64,{base64_data}"},
},
]
message: ChatMessage = {
"role": "user",
"content": content,
}
with track_usage(uid, Features.OPENGLASS):
response = await cast(AsyncVisionLlm, get_llm('openglass')).ainvoke([message], max_completion_tokens=150)
description = _response_text(response).strip()
return description if description != '""' else ""