-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
61 lines (48 loc) · 1.55 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
55
56
57
58
59
60
61
import os
import base64
import streamlit as st
from ollama import Client
ollama_base_url = os.getenv("OLLAMA_BASE_URL")
llm_name = os.getenv("LLM")
def process_stream(stream):
for chunk in stream:
yield chunk['message']['content']
def get_base64_encoded_image(image_path):
with open(image_path, "rb") as img_file:
return base64.b64encode(img_file.read()).decode('utf-8')
# Streamlit UI
base64_image = get_base64_encoded_image('img/kccneu24.png')
styl = f"""
<style>
.main {{
background-image: url(data:image/png;base64,{base64_image});
background-repeat: repeat;
background-size: cover;
background-attachment: fixed;
}}
</style>
"""
st.markdown(styl, unsafe_allow_html=True)
st.title(':grey[Describe an Image with LLaVa 📸]')
picture = st.camera_input("")
st.subheader(':grey[Take a picture. Say ] :blue[_Kubernetes_]:grey[!]')
if picture:
with open ('snap.jpg','wb') as f:
f.write(picture.getbuffer())
# Initialize the Ollama client
client = Client(host=ollama_base_url)
# Define the path to your image
image_path = 'snap.jpg'
# Prepare the message to send to the LLaVA model
message = {
'role': 'user',
'content': 'Describe this image in a respectful way, without mentioning any text elements.',
'images': [image_path]
}
# Use the ollama.chat function to send the image and retrieve the description
stream = client.chat(
model="llava", # Specify the desired LLaVA model size
messages=[message],
stream=True,
)
st.write_stream(process_stream(stream))