-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (43 loc) · 1.5 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from dotenv import load_dotenv
import streamlit as st
from PIL import Image
import google.generativeai as genai
import os
# load the environment variable
load_dotenv()
# set the Google API key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# model = genai.GenerativeModel('gemini-1.5-flash')
model = genai.GenerativeModel('models/gemini-1.5-flash-latest')
# function to generate text
def generate(input_question, image):
task = [input_question, image]
response = model.generate_content(task)
return response.text
# streamlit app
st.title("Generative AI Vision App")
st.markdown("""
Welcome to the Generative AI Vision App!
Ask a question about the uploaded image, and let the model describe it.
""")
# user input for question
input_question = st.text_input("Input your question here : ", key="input")
# File upload for image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
image=""
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image.")
# button to trigger image description generative
submit_button = st.button("Tell me about the image")
# Generate and display response on button click
if submit_button:
if input_question == "":
st.error("Please enter a question")
st.stop()
if image == "":
st.error("Please upload the image")
st.stop()
with st.spinner("Generating response..."):
response = generate(input_question, image)
st.success(response)