Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- requirements.txt +4 -0
- vision.py +58 -0
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
streamlit
|
| 3 |
+
google-generativeai
|
| 4 |
+
python-dotenv
|
vision.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import os
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Load environment variables from .env file
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Set up Google API key from environment variable
|
| 12 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 13 |
+
|
| 14 |
+
# Create a Gemini 1.5 Flash model instance
|
| 15 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 16 |
+
|
| 17 |
+
# Define a function to get a response from the Gemini model
|
| 18 |
+
def get_gemini_response(input, image):
|
| 19 |
+
# If input prompt is not empty, generate content with both input and image
|
| 20 |
+
if input != "":
|
| 21 |
+
response = model.generate_content([input, image])
|
| 22 |
+
# If input prompt is empty, generate content with only the image
|
| 23 |
+
else:
|
| 24 |
+
response = model.generate_content(image)
|
| 25 |
+
# Return the generated text response
|
| 26 |
+
return response.text
|
| 27 |
+
|
| 28 |
+
# Set up Streamlit page configuration
|
| 29 |
+
st.set_page_config("Visualise your image")
|
| 30 |
+
|
| 31 |
+
# Add a header to the page
|
| 32 |
+
st.header("Visualise with Gemini")
|
| 33 |
+
|
| 34 |
+
# Create a text input field for the user to enter a prompt
|
| 35 |
+
input = st.text_input("Input Prompt: ", key="input")
|
| 36 |
+
|
| 37 |
+
# Create a file uploader for the user to upload an image
|
| 38 |
+
uploaded_file = st.file_uploader("Choose an image...", type=['jpg', 'png', 'jpeg'])
|
| 39 |
+
|
| 40 |
+
# Initialize an empty image variable
|
| 41 |
+
image = ''
|
| 42 |
+
|
| 43 |
+
# If an image is uploaded, open it using PIL and display it
|
| 44 |
+
if uploaded_file is not None:
|
| 45 |
+
image = Image.open(uploaded_file)
|
| 46 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 47 |
+
|
| 48 |
+
# Create a submit button
|
| 49 |
+
submit = st.button("Tell me about the image")
|
| 50 |
+
|
| 51 |
+
# If the submit button is clicked, generate a response from the Gemini model
|
| 52 |
+
if submit:
|
| 53 |
+
# Call the get_gemini_response function with the input prompt and image
|
| 54 |
+
response = get_gemini_response(input, image)
|
| 55 |
+
# Add a subheader to display the response
|
| 56 |
+
st.subheader("The response is")
|
| 57 |
+
# Display the generated response
|
| 58 |
+
st.write(response)
|