Repository navigation
Expand file tree
/
Copy pathExample_2.py
More file actions
59 lines (48 loc) · 1.58 KB
/
Copy pathExample_2.py
File metadata and controls
59 lines (48 loc) · 1.58 KB
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
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from dotenv import load_dotenv
import base64
import os
import streamlit as st
# Load the Gemini API key and set it to the model
load_dotenv()
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", api_key=GEMINI_API_KEY)
def encode_image(image_file):
return base64.b64encode(image_file.read()).decode()
# Built a web app interface using Streamlit
st.title("Image Analysis Using Gemini")
col1, col2 = st.columns(2)
with col1:
uploaded_file = st.file_uploader("Upload your image", type=["jpg", "png"])
with col2:
if uploaded_file is not None:
st.image(uploaded_file)
else:
st.write("Image Preview")
question = st.text_input("Enter a question")
if question and uploaded_file is not None:
image = encode_image(uploaded_file)
# Create a chat prompt template to specify the image analytic role for LLM
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant that can describe images."),
(
"human",
[
{"type": "text", "text": "{input}"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image}",
"detail": "low",
},
},
],
),
]
)
# Invoke the LLM to describe the image details
chain = prompt | llm
res = chain.invoke({"input": question, "image": image})
st.write(res.content)