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:
1 parent
ebd5ffa58b
commit
9f68608929
1 file changed
+13
-6
@@ -6,7 +6,7 @@ ACCESS_TOKEN="my_bot_token" # Mastodon Access Token
|
|||||||
SUBREDDIT="memes" # Subreddit (must be Image Only, like r/memes)
|
SUBREDDIT="memes" # Subreddit (must be Image Only, like r/memes)
|
||||||
|
|
||||||
# Fetch the JSON feed
|
# 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
|
# extract the first item
|
||||||
item=$(echo "$json" | jq '.items[0]')
|
item=$(echo "$json" | jq '.items[0]')
|
||||||
@@ -15,13 +15,11 @@ item=$(echo "$json" | jq '.items[0]')
|
|||||||
title=$(echo "$item" | jq -r '.title')
|
title=$(echo "$item" | jq -r '.title')
|
||||||
url=$(echo "$item" | jq -r '.url')
|
url=$(echo "$item" | jq -r '.url')
|
||||||
|
|
||||||
# Make sure to stop if the RSS-Bridge is offline or the extraction failed
|
if [ "$url" == "null" ] || [ -z "$url" ]; then # Make sure there is no null posting. Happens when RSS-Bridge gets no Content from Reddit
|
||||||
if [ "$url" == "null" ]; then
|
|
||||||
exit 1
|
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.
|
INSTANCE_URL="https://failed.dhusch.de" # am not sure if the exit actually works. But don't want the Bot to do anything.
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
tags=$(echo "$item" | jq -r '.tags') # Needed for NSFW check
|
tags=$(echo "$item" | jq -r '.tags') # Needed for NSFW check
|
||||||
|
|
||||||
# extract the image URL from the content_html
|
# extract the image URL from the content_html
|
||||||
@@ -33,13 +31,22 @@ wget "$image_url" -N -P /tmp/ # Download the image and save to the output fil
|
|||||||
|
|
||||||
# Resize Files larger that 8 MB
|
# Resize Files larger that 8 MB
|
||||||
if [ $(du -m /tmp/$filename | awk '{print $1}') -gt 8 ]; then # Check if the file size is greater than 8 MB
|
if [ $(du -m /tmp/$filename | awk '{print $1}') -gt 8 ]; then # Check if the file size is greater than 8 MB
|
||||||
ffmpeg -i /tmp/$filename -fs 8M /tmp/ff.$filename # Resize the file to 8MB using FFMPEG (needs to be installed)
|
ffmpeg -i /tmp/$filename -fs 8M /tmp/ff.$filename # Resize the file to 8 MB using FFMPEG (needs to be installed)
|
||||||
mv /tmp/ff.$filename /tmp/$filename
|
mv /tmp/ff.$filename /tmp/$filename
|
||||||
fi
|
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
|
# Upload the image and get the media ID
|
||||||
MEDIA_ID=$(curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \
|
MEDIA_ID=$(curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \
|
||||||
-F "file=@/tmp/$filename" \
|
-F "file=@/tmp/$filename" \
|
||||||
|
-F "description=AI generated Alt Text: $alt" \
|
||||||
"$INSTANCE_URL/api/v1/media" \
|
"$INSTANCE_URL/api/v1/media" \
|
||||||
| jq -r '.id')
|
| jq -r '.id')
|
||||||
|
|
||||||
@@ -56,7 +63,7 @@ else
|
|||||||
|
|
||||||
# Post the status with the image and make it NSFW
|
# Post the status with the image and make it NSFW
|
||||||
curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \
|
curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \
|
||||||
-F "status=$title
|
-F "status=$title
|
||||||
|
|
||||||
($url) #NSFW" \
|
($url) #NSFW" \
|
||||||
-F "sensitive=true" \
|
-F "sensitive=true" \
|
||||||
|
|||||||
Reference in new issue
Block a user