Auto-update: Thu Nov 14 11:38:18 PST 2024

This commit is contained in:
sanj 2024-11-14 11:38:18 -08:00
parent 2b638d4bf6
commit 3e33c5914f
2 changed files with 93 additions and 86 deletions

View file

@ -368,7 +368,8 @@ created: "{dt_datetime.now().strftime("%Y-%m-%d %H:%M:%S")}"
with open(absolute_path, 'wb') as f: with open(absolute_path, 'wb') as f:
f.write(body.encode()) f.write(body.encode())
banner = await generate_banner(formatted_day, location, weather_note) if Sys.EXTENSIONS.comfyui:
banner = await generate_banner(formatted_day, location, weather_note)
return absolute_path return absolute_path
@ -422,96 +423,103 @@ async def update_frontmatter(date_time: dt_datetime, key: str, value: str):
return {"message": "Frontmatter updated successfully."} return {"message": "Frontmatter updated successfully."}
@note.post("/note/banner") if Sys.EXTENSIONS.comfyui:
async def banner_endpoint(dt: str, location: str = None, forecast: str = None, mood: str = None, other_context: str = None): @note.post("/note/banner")
''' async def banner_endpoint(dt: str, location: str = None, forecast: str = None, mood: str = None, other_context: str = None):
Endpoint (POST) that generates a new banner image for the Obsidian daily note for a specified date, taking into account optional additional information, then updates the frontmatter if necessary. '''
''' Endpoint (POST) that generates a new banner image for the Obsidian daily note for a specified date, taking into account optional additional information, then updates the frontmatter if necessary.
l.debug(f"banner_endpoint requested with date: {dt} ({type(dt)})") '''
date_time = await gis.dt(dt) l.debug(f"banner_endpoint requested with date: {dt} ({type(dt)})")
l.debug(f"date_time after localization: {date_time} ({type(date_time)})") date_time = await gis.dt(dt)
context = await generate_context(dt, location, forecast, mood, other_context) l.debug(f"date_time after localization: {date_time} ({type(date_time)})")
jpg_path = await generate_banner(date_time, location, mood=mood, other_context=other_context) context = await generate_banner_context(dt, location, forecast, mood, other_context)
return jpg_path jpg_path = await generate_banner(date_time, location, mood=mood, other_context=other_context)
return jpg_path
async def generate_banner(dt, location: Location = None, forecast: str = None, mood: str = None, other_context: str = None): async def generate_banner(dt, location: Location = None, forecast: str = None, mood: str = None, other_context: str = None):
date_time = await gis.dt(dt) if Sys.EXTENSIONS.comfyui:
destination_path, local_path = assemble_journal_path(date_time, filename="Banner", extension=".jpg", no_timestamp = True) date_time = await gis.dt(dt)
if not location or not isinstance(location, Location): destination_path, local_path = assemble_journal_path(date_time, filename="Banner", extension=".jpg", no_timestamp = True)
locations = await gis.fetch_locations(date_time) if not location or not isinstance(location, Location):
if locations: locations = await gis.fetch_locations(date_time)
location = locations[0] if locations:
if not forecast: location = locations[0]
forecast = await update_dn_weather(date_time, False, location.latitude, location.longitude) if not forecast:
forecast = await update_dn_weather(date_time, False, location.latitude, location.longitude)
prompt = await generate_context(date_time, location, forecast, mood, other_context)
l.debug(f"Prompt: {prompt}") prompt = await generate_banner_context(date_time, location, forecast, mood, other_context)
final_path = await img.workflow(prompt, scene=OBSIDIAN_BANNER_SCENE, destination_path=destination_path) l.debug(f"Prompt: {prompt}")
if not str(local_path) in str(final_path): final_path = await img.workflow(prompt, scene=OBSIDIAN_BANNER_SCENE, destination_path=destination_path)
l.info(f"Apparent mismatch between local path, {local_path}, and final_path, {final_path}") if not str(local_path) in str(final_path):
jpg_embed = f"\"![[{local_path}]]\"" l.info(f"Apparent mismatch between local path, {local_path}, and final_path, {final_path}")
await update_frontmatter(date_time, "banner", jpg_embed) jpg_embed = f"\"![[{local_path}]]\""
return local_path await update_frontmatter(date_time, "banner", jpg_embed)
return local_path
async def generate_context(date_time, location: Location, forecast: str, mood: str, other_context: str):
display_name = "Location: "
if location and isinstance(location, Location):
lat, lon = location.latitude, location.longitude
override_location = GEO.find_override_location(lat, lon)
display_name += f"{override_location}, " if override_location else ""
if location.display_name:
display_name += f"{location.display_name}"
else:
display_name += f"{location.road}, " if location.road else ""
display_name += f"the {location.neighbourhood} neighbourhood of " if location.neighbourhood else ""
display_name += f"the {location.suburb} suburb of " if location.suburb else ""
display_name += f"the {location.quarter} quarter, " if location.quarter else ""
display_name += f"{location.city}, " if location.city else ""
display_name += f"{location.state} " if location.state else ""
display_name += f"{location.country} " if location.country else ""
if display_name == "Location: ":
geocoded_location = await GEO.code((lat, lon))
if geocoded_location.display_name or geocoded_location.city or geocoded_location.country:
return await generate_context(date_time, geocoded_location, forecast, mood, other_context)
else:
l.warning(f"Failed to get a useable location for purposes of generating a banner, but we'll generate one anyway.")
elif location and isinstance(location, str):
display_name = f"Location: {location}\n"
else: else:
display_name = "" l.warn(f"generate_banner called, but comfyui extension is disabled")
if not forecast:
forecast = "The weather forecast is: " + await update_dn_weather(date_time)
sentiment = await sentiment_analysis(date_time) async def generate_banner_context(date_time, location: Location, forecast: str, mood: str, other_context: str):
mood = sentiment if not mood else mood if Sys.EXTENSIONS.comfyui:
mood = f"Mood: {mood}" if mood else "" display_name = "Location: "
if mood and sentiment: mood = f"Mood: {mood}, {sentiment}" if location and isinstance(location, Location):
elif mood and not sentiment: mood = f"Mood: {mood}" lat, lon = location.latitude, location.longitude
elif sentiment and not mood: mood = f"Mood: {sentiment}" override_location = GEO.find_override_location(lat, lon)
else: mood = "" display_name += f"{override_location}, " if override_location else ""
if location.display_name:
events = await cal.get_events(date_time, date_time) display_name += f"{location.display_name}"
formatted_events = []
for event in events: else:
event_str = event.get('name') display_name += f"{location.road}, " if location.road else ""
if event.get('location'): display_name += f"the {location.neighbourhood} neighbourhood of " if location.neighbourhood else ""
event_str += f" at {event.get('location')}" display_name += f"the {location.suburb} suburb of " if location.suburb else ""
formatted_events.append(event_str) display_name += f"the {location.quarter} quarter, " if location.quarter else ""
display_name += f"{location.city}, " if location.city else ""
additional_info = ', '.join(formatted_events) if formatted_events else '' display_name += f"{location.state} " if location.state else ""
display_name += f"{location.country} " if location.country else ""
other_context = f"{other_context}, {additional_info}" if other_context else additional_info
other_context = f"Additional information: {other_context}" if other_context else "" if display_name == "Location: ":
geocoded_location = await GEO.code((lat, lon))
prompt = "Generate an aesthetically appealing banner image for a daily note that helps to visualize the following scene information: " if geocoded_location.display_name or geocoded_location.city or geocoded_location.country:
prompt += "\n".join([display_name, forecast, mood, other_context]) return await generate_banner_context(date_time, geocoded_location, forecast, mood, other_context)
else:
return prompt l.warning(f"Failed to get a useable location for purposes of generating a banner, but we'll generate one anyway.")
elif location and isinstance(location, str):
display_name = f"Location: {location}\n"
else:
display_name = ""
if not forecast:
forecast = "The weather forecast is: " + await update_dn_weather(date_time)
sentiment = await sentiment_analysis(date_time)
mood = sentiment if not mood else mood
mood = f"Mood: {mood}" if mood else ""
if mood and sentiment: mood = f"Mood: {mood}, {sentiment}"
elif mood and not sentiment: mood = f"Mood: {mood}"
elif sentiment and not mood: mood = f"Mood: {sentiment}"
else: mood = ""
events = await cal.get_events(date_time, date_time)
formatted_events = []
for event in events:
event_str = event.get('name')
if event.get('location'):
event_str += f" at {event.get('location')}"
formatted_events.append(event_str)
additional_info = ', '.join(formatted_events) if formatted_events else ''
other_context = f"{other_context}, {additional_info}" if other_context else additional_info
other_context = f"Additional information: {other_context}" if other_context else ""
prompt = "Generate an aesthetically appealing banner image for a daily note that helps to visualize the following scene information: "
prompt += "\n".join([display_name, forecast, mood, other_context])
return prompt
else:
l.warn(f"generate_banner_context called, but comfyui extension disabled")
async def get_note(date_time: dt_datetime): async def get_note(date_time: dt_datetime):

File diff suppressed because one or more lines are too long