Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import google.generativeai as genai | |
| import io | |
| import os | |
| from dotenv import load_dotenv | |
| from PIL import Image | |
| load_dotenv() | |
| # Configure the API key | |
| genai.configure(api_key= "AIzaSyBaxMCjBV5fBlsKUmFb-8SGgkiirv1ZKck") | |
| # Set up the model | |
| model = genai.GenerativeModel('gemini-1.5-flash-latest') | |
| def get_gemini_response(image_blob, prompt): | |
| response = model.generate_content([prompt, image_blob]) | |
| return response.text | |
| # Streamlit app | |
| st.set_page_config(page_title="Image Insights Generator", page_icon="π·", layout="wide") | |
| # Sidebar with instructions and additional information | |
| st.sidebar.title("Instructions") | |
| st.sidebar.write(""" | |
| 1. Upload an image using the file uploader. | |
| 2. Enter a question about the image in the text input box. | |
| 3. Click the "Generate Insights" button to get AI insights about the image. | |
| """) | |
| st.sidebar.markdown("---") | |
| st.sidebar.caption("Created with Streamlit and Gemini Pro Vision") | |
| # Main content | |
| st.title("π· Image Insights Generator") | |
| st.write("Upload an image and ask a question about it. Our AI will analyze the image and provide insights!") | |
| # File uploader for image | |
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Display the uploaded image | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="Uploaded Image", use_column_width=True) | |
| # Text input for the prompt | |
| prompt = st.text_input("What would you like to know about this image?", placeholder="Enter your question here...") | |
| if st.button("Generate Insights"): | |
| with st.spinner("Analyzing the image..."): | |
| # Prepare the image for the Gemini API | |
| img_byte_arr = io.BytesIO() | |
| image.save(img_byte_arr, format='PNG') | |
| img_byte_arr = img_byte_arr.getvalue() | |
| # Create a Blob from the image data | |
| image_blob = { | |
| "mime_type": "image/png", | |
| "data": img_byte_arr | |
| } | |
| # Progress bar | |
| progress_bar = st.progress(0) | |
| for i in range(1, 101): | |
| progress_bar.progress(i) | |
| if i == 100: | |
| # Get the response from Gemini Pro Vision | |
| response = get_gemini_response(image_blob, prompt) | |
| # Display the response | |
| st.subheader("π AI Insights:") | |
| st.write(response) | |
| st.balloons() # Add some celebration! | |
| # Custom CSS for styling | |
| st.markdown(""" | |
| <style> | |
| body { | |
| font-family: 'Arial', sans-serif; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |