-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanimation_generator.py
More file actions
49 lines (40 loc) · 1.82 KB
/
Copy pathanimation_generator.py
File metadata and controls
49 lines (40 loc) · 1.82 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
"""Initial commit — metadata update
This comment was added to associate the 'Initial commit' with this file.
"""
from langchain_core.messages import HumanMessage, SystemMessage
from prompts.llm_templates import GENERATOR_SYSTEM_PROMPT, GENERATOR_PROMPT
class AnimationGenerator:
def __init__(self, vlm, logger):
self.vlm = vlm
self.logger = logger
self.system_prompt = GENERATOR_SYSTEM_PROMPT
self.animation_prompt_template = GENERATOR_PROMPT
def animate(self, content, previous_html):
formatted_prompt = self.animation_prompt_template.format(
class_name=content['class_name'],
viewbox=content['viewbox'],
animation_plan=content['animation_plan'],
previous_html=previous_html,
)
image_payload = [
{'type': 'image_url', 'image_url': {"url": f"data:image/jpeg;base64,{content['big_picture']}"}},
{'type': 'image_url', 'image_url': {"url": f"data:image/jpeg;base64,{content['little_picture']}"}},
]
chat_prompt = [
SystemMessage(content=self.system_prompt),
HumanMessage(content=[
{'type': 'text', 'text': formatted_prompt},
*image_payload,
]),
]
# Generate the HTML and CSS code using the LLM
response = self.vlm.invoke(chat_prompt)
response = response.content.replace("```html", "").replace("```", "").strip()
self.logger.info(f"[AnimationGenerator]\n{response}")
try:
# Extract the CSS code from the response
css_code = response.split("<style>")[1].split("</style>")[0].strip()
return css_code
except IndexError:
return self.animate(content, previous_html)
# Module is intended to be imported and used by `main.py` or other callers.