add alt text generation using ollama llava (local ai)

this might get removed later, if performance on my server is too bad!
This commit is contained in:
D5k H3h authored and GitHub committed 2024-10-16 21:33:24 +02:00
1 parent ebd5ffa58b
commit 9f68608929
1 file changed
+11 -4
+11 -4
View File
@@ -6,7 +6,7 @@ ACCESS_TOKEN="my_bot_token" # Mastodon Access Token
SUBREDDIT="memes" # Subreddit (must be Image Only, like r/memes)
# Fetch the JSON feed
json=$(curl -H 'User-Agent: Mozilla/5.0' "https://dhusch.de/rss-bridge/?action=display&bridge=RedditBridge&context=single&r=$SUBREDDIT&f=&score=&d=hot&search=&format=Json" 2>/dev/null)
json=$(curl -H 'User-Agent: Mozilla/5.0' "https://bridge.easter.fr/?action=display&bridge=RedditBridge&context=single&r=$SUBREDDIT&f=&score=&d=hot&search=&format=Json" 2>/dev/null)
# extract the first item
item=$(echo "$json" | jq '.items[0]')
@@ -15,13 +15,11 @@ item=$(echo "$json" | jq '.items[0]')
title=$(echo "$item" | jq -r '.title')
url=$(echo "$item" | jq -r '.url')
# Make sure to stop if the RSS-Bridge is offline or the extraction failed
if [ "$url" == "null" ]; then
if [ "$url" == "null" ] || [ -z "$url" ]; then # Make sure there is no null posting. Happens when RSS-Bridge gets no Content from Reddit
exit 1
INSTANCE_URL="https://failed.dhusch.de" # am not sure if the exit actually works. But don't want the Bot to do anything.
fi
tags=$(echo "$item" | jq -r '.tags') # Needed for NSFW check
# extract the image URL from the content_html
@@ -37,9 +35,18 @@ if [ $(du -m /tmp/$filename | awk '{print $1}') -gt 8 ]; then # Check if the fil
mv /tmp/ff.$filename /tmp/$filename
fi
ffmpeg -i /tmp/$filename /tmp/meme.gen.png
alt=$(ollama run llava "Describe the meme. do not use more than 1000 characters /tmp/meme.gen.png")
ollama stop llava
alt=$(echo $alt | sed 's/"//g' | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
echo $alt
rm /tmp/meme.gen.png
# Upload the image and get the media ID
MEDIA_ID=$(curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \
-F "file=@/tmp/$filename" \
-F "description=AI generated Alt Text: $alt" \
"$INSTANCE_URL/api/v1/media" \
| jq -r '.id')