Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,13 @@ import requests
|
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
import os
|
5 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
|
6 |
|
7 |
# Load the BLIP model for creative caption generation
|
8 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
10 |
|
11 |
-
# Function to generate a creative caption
|
12 |
def generate_caption(image):
|
13 |
# Prepare image for the model
|
14 |
inputs = processor(images=image, return_tensors="pt")
|
@@ -19,13 +20,30 @@ def generate_caption(image):
|
|
19 |
# Decode the output to get a readable caption
|
20 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Streamlit app
|
26 |
def main():
|
27 |
st.title("Creative Image Caption Generator")
|
28 |
-
st.write("Upload an image, and let the AI generate a creative and descriptive caption for it
|
29 |
|
30 |
# Upload image
|
31 |
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
|
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
import os
|
5 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
6 |
+
from random import choice
|
7 |
|
8 |
# Load the BLIP model for creative caption generation
|
9 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
10 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
11 |
|
12 |
+
# Function to generate a detailed, creative caption
|
13 |
def generate_caption(image):
|
14 |
# Prepare image for the model
|
15 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
20 |
# Decode the output to get a readable caption
|
21 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
22 |
|
23 |
+
# Enhance the caption with more details (assuming the image context is about nature, people, etc.)
|
24 |
+
detailed_caption = enhance_caption(caption)
|
25 |
+
|
26 |
+
return detailed_caption
|
27 |
+
|
28 |
+
# Enhance caption with surrounding context and social media flavor
|
29 |
+
def enhance_caption(caption):
|
30 |
+
# Possible enhancements for social media flavor
|
31 |
+
emojis = ["π
", "π", "π»", "π΄", "ποΈ", "π¦", "π", "π"]
|
32 |
+
hashtags = ["#NatureLovers", "#TravelVibes", "#Sunset", "#BeachLife", "#AdventureAwaits"]
|
33 |
+
|
34 |
+
# Select random emojis and hashtags to add a fun social media touch
|
35 |
+
selected_emoji = choice(emojis)
|
36 |
+
selected_hashtags = " ".join(choice(hashtags) for _ in range(2)) # Select two random hashtags
|
37 |
+
|
38 |
+
# Creating a more detailed, fun caption for social media
|
39 |
+
enhanced_caption = f"{caption} {selected_emoji} {selected_hashtags}"
|
40 |
+
|
41 |
+
return enhanced_caption
|
42 |
|
43 |
# Streamlit app
|
44 |
def main():
|
45 |
st.title("Creative Image Caption Generator")
|
46 |
+
st.write("Upload an image, and let the AI generate a creative and descriptive caption for it, ready for social media sharing!")
|
47 |
|
48 |
# Upload image
|
49 |
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|