-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini.py
68 lines (52 loc) · 1.88 KB
/
gemini.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Streamlite front end
# google-generativeai to access LLM model
# python-dotenv to load environment variable
from PIL import Image
# loading environment variable
import streamlit as st
import google.generativeai as genai
#genai.configure(api_key = os.getenv("GOOGLE_API_KEY"))
genai.configure(api_key = st.secrets["GOOGLE_API_KEY"])
# function for loading model and get response
def model_response(input,img):
if input!="" and img!= None:
model = genai.GenerativeModel("gemini-pro-vision")
response = model.generate_content([input,img])
return response.text
elif input!="" and img == None:
model = genai.GenerativeModel("gemini-pro")
response = model.generate_content(input)
return response.text
elif input=="" and img != None:
model = genai.GenerativeModel("gemini-pro-vision")
response = model.generate_content(img)
return response.text
else:
return "No Input Provided"
# Starting streamlit
st.set_page_config(page_title="Gemini pro vision")
st.header("Gemini application")
input=st.text_input("Question: ",key = "input")
image = st.file_uploader("Choose Image File:",type=["jpg","jpeg","png"])
img =""
if image is not None:
img = Image.open(image)
st.image(img,caption="Uploaded image",use_column_width=True)
else:
img = None
submit = st.button("Ask the question")
## on click action
if submit and img == None and input == "":
response = model_response(input,img)
st.subheader("Response :")
st.write(response)
elif submit and img != None:
response = model_response(input,img)
st.subheader("Model Used: gemini-pro-vision")
st.subheader("Response :")
st.write(response)
elif submit and img == None:
response = model_response(input,None)
st.subheader("Model Used: gemini-pro")
st.subheader("Response :")
st.write(response)