Spaces:
Runtime error

tinkvu's picture
Create app.py
cb7ca51 verified
Raw History Blame Contribute Delete
2.62 kB
# Import necessary libraries
import streamlit as st
from PIL import Image
import io
import json
import google.generativeai as genai # Assuming this library is installed locally
# Access the API key using Streamlit's secrets functionality
google_api_key = "AIzaSyC31zB5aAjOpFTUbEycRdmMhvA_5gDoVDs"
genai.configure(api_key=google_api_key)
# Define the main function for Streamlit app
def main():
# Set up Streamlit app title
st.title("Captio.io")
st.write("No need of waiting too long for Instagram Captions")
st.write("Model used: Google Gemini 1.5 Flash")
# Upload image file
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png"])
# Check if file is uploaded
if uploaded_file is not None:
# Open the image using PIL
image = Image.open(uploaded_file)
# Generate captions
model = genai.GenerativeModel('gemini-1.5-flash')
caption_prompts = [
"Write 3 short engaging Instagram captions based on this picture to be posted on Instagram. Creative genre. Add hashtags",
"Write 3 short engaging Instagram captions based on this picture to be posted on Instagram. Funny genre.Add hashtags",
"Write 3 short engaging Instagram captions based on this picture to be posted on Instagram. Professional genre. Add hashtags"
]
captions = []
# Generate captions for each category
for prompt in caption_prompts:
caption_response = model.generate_content([prompt, image], stream=True)
caption_response.resolve()
captions.append(caption_response.text)
# Define columns layout
col1, col2, col3, col4 = st.columns(4)
# Display the resized image in the first column
with col1:
st.subheader("Image")
st.image(image, caption="Uploaded Image", use_column_width=True)
# Display creative captions in the second column
with col2:
st.subheader("Creative Captions")
for caption in captions[0].split('\n'):
st.write(caption)
# Display funny captions in the third column
with col3:
st.subheader("Funny Captions")
for caption in captions[1].split('\n'):
st.write(caption)
# Display professional captions in the fourth column
with col4:
st.subheader("Professional Captions")
for caption in captions[2].split('\n'):
st.write(caption)
# Run the main function to start the Streamlit app
if __name__ == "__main__":
main()