Update to use Craiyon v3

This commit is contained in:
Mikkel Munch Mortensen 2023-05-09 10:19:07 +02:00
parent 756daa6c07
commit 0e4f3f6151

View file

@ -66,14 +66,17 @@ def get_photo_url(menu: str):
} }
request_data = { request_data = {
"model": "photo",
"negative_prompt": "",
"prompt": translation, "prompt": translation,
"version": "35s5hfwn9n78gb06",
"token": token, "token": token,
"version": "35s5hfwn9n78gb06",
} }
sys.stdout.write("Asking Craiyon to generate images for the menu...\n") sys.stdout.write("Asking Craiyon to generate images for the menu...\n")
craiyon_start = datetime.datetime.now()
try: try:
response = requests.post( response = requests.post(
"https://api.craiyon.com/draw", "https://api.craiyon.com/v3",
headers=headers, headers=headers,
json=request_data, json=request_data,
timeout=90, timeout=90,
@ -87,6 +90,7 @@ def get_photo_url(menu: str):
except JSONDecodeError: except JSONDecodeError:
sys.stderr.write("Unable to parse JSON from Craiyon response:\n") sys.stderr.write("Unable to parse JSON from Craiyon response:\n")
sys.stderr.write(f"{response.text}\n") sys.stderr.write(f"{response.text}\n")
sys.stderr.write("Waited for {datetime.datetime.now() - craiyon_start}.")
return None return None
return (f"https://img.craiyon.com/{image}", translation) return (f"https://img.craiyon.com/{image}", translation)
@ -119,7 +123,13 @@ response = requests.post(
) )
data = response.json() data = response.json()
sys.stdout.write("Data retrieved from SharePoint.\n") sys.stdout.write("Data retrieved from SharePoint.\n")
if response.status_code != 200:
hook_url = SLACK_ERROR_HOOK
photo = None
message = plain_message = (
f"SharePoint responded with status {response.status_code} :("
)
else:
# Extract today's menu from the SharePoint data. # Extract today's menu from the SharePoint data.
today = datetime.date.today().strftime("%d-%m-%Y") today = datetime.date.today().strftime("%d-%m-%Y")
menu = None menu = None
@ -146,7 +156,7 @@ else:
for emoji, keywords in { for emoji, keywords in {
"flag-in": ["indisk", "indien"], "flag-in": ["indisk", "indien"],
"flag-gr": ["græsk", "grækenland"], "flag-gr": ["græsk", "grækenland"],
"fish": ["fisk", "laks", "rødspætte", "sej "], "fish": ["fisk", "laks", "rødspætte", "sej ", "kulmule"],
"shrimp": ["reje"], "shrimp": ["reje"],
"pig2": ["skinke", "gris", "nakkefilet"], "pig2": ["skinke", "gris", "nakkefilet"],
"cow": ["hakkebøf"], "cow": ["hakkebøf"],
@ -162,7 +172,10 @@ else:
"mango": ["mango"], "mango": ["mango"],
"mushroom": ["svampe", "kantarel", "champignon"], "mushroom": ["svampe", "kantarel", "champignon"],
"eggplant": ["moussaka", "mousakka"], "eggplant": ["moussaka", "mousakka"],
"cheese_wedge": [" ost"],
"beans": ["bønne"], "beans": ["bønne"],
"olive": ["oliven"],
"spaghetti": ["spaghetti"],
"hamburger": ["burger"], "hamburger": ["burger"],
"sandwich": ["sandwich"], "sandwich": ["sandwich"],
"stuffed_flatbread": ["pita"], "stuffed_flatbread": ["pita"],
@ -224,13 +237,19 @@ payload = {
} }
if photo is not None: if photo is not None:
payload["blocks"][0]["accessory"] = { photo_text = "Et ukvalificeret bud på hvordan dagens menu kunne se ud"
payload["blocks"].insert(
0,
{
"type": "image", "type": "image",
"image_url": photo[0], "image_url": photo[0],
"alt_text": ( "alt_text": photo_text,
"Et ukvalificeret bud på hvordan dagens menu kunne se ud" "title": {
), "type": "plain_text",
"text": photo_text,
},
} }
)
sys.stdout.write("Posting menu to Slack...\n") sys.stdout.write("Posting menu to Slack...\n")
hook = request.Request( hook = request.Request(