Spaces:
Sleeping
Sleeping
update pages/02_π_Multimodal.py
Browse files- pages/02_π_Multimodal.py +197 -198
pages/02_π_Multimodal.py
CHANGED
|
@@ -1,198 +1,197 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import requests
|
| 3 |
-
import base64
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
import json
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
buffered =
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"""
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
model
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
col_1
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
st.
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
image =
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
"
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
"
|
| 159 |
-
"
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="LLaVA Playground",
|
| 10 |
+
page_icon="π",
|
| 11 |
+
layout="wide",
|
| 12 |
+
initial_sidebar_state="expanded",
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def img_to_base64(image):
|
| 17 |
+
"""
|
| 18 |
+
Convert an image to base64 format.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
image: PIL.Image - The image to be converted.
|
| 22 |
+
Returns:
|
| 23 |
+
str: The base64 encoded image.
|
| 24 |
+
"""
|
| 25 |
+
buffered = BytesIO()
|
| 26 |
+
image.save(buffered, format="PNG")
|
| 27 |
+
return base64.b64encode(buffered.getvalue()).decode()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_allowed_model_names(models_info: dict) -> tuple:
|
| 31 |
+
"""
|
| 32 |
+
Returns a tuple containing the names of the allowed models.
|
| 33 |
+
"""
|
| 34 |
+
allowed_models = ["bakllava:latest", "llava:latest"]
|
| 35 |
+
return tuple(
|
| 36 |
+
model
|
| 37 |
+
for model in allowed_models
|
| 38 |
+
if model in [m["name"] for m in models_info["models"]]
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
st.subheader("LLaVA 1.6 Playground", divider="red", anchor=False)
|
| 44 |
+
|
| 45 |
+
models_info = ollama.list()
|
| 46 |
+
available_models = get_allowed_model_names(models_info)
|
| 47 |
+
missing_models = set(["bakllava:latest", "llava:latest"]) - set(available_models)
|
| 48 |
+
|
| 49 |
+
col_1, col_2 = st.columns(2)
|
| 50 |
+
with col_1.popover("βοΈ Model Management", help="Manage models here"):
|
| 51 |
+
if not available_models:
|
| 52 |
+
st.error("No allowed models are available.", icon="π³")
|
| 53 |
+
model_to_download = st.selectbox(
|
| 54 |
+
"Select a model to download", ["bakllava:latest", "llava:latest"]
|
| 55 |
+
)
|
| 56 |
+
if st.button(f"Download {model_to_download}"):
|
| 57 |
+
try:
|
| 58 |
+
ollama.pull(model_to_download)
|
| 59 |
+
st.toast(
|
| 60 |
+
f"""Downloaded model: {
|
| 61 |
+
model_to_download}""",
|
| 62 |
+
icon="β
",
|
| 63 |
+
)
|
| 64 |
+
st.rerun()
|
| 65 |
+
except Exception as e:
|
| 66 |
+
st.error(
|
| 67 |
+
f"""Failed to download model: {
|
| 68 |
+
model_to_download}. Error: {str(e)}""",
|
| 69 |
+
icon="π³",
|
| 70 |
+
)
|
| 71 |
+
else:
|
| 72 |
+
if missing_models:
|
| 73 |
+
model_to_download = st.selectbox(
|
| 74 |
+
":green[**π₯ DOWNLOAD MODEL**]", list(missing_models)
|
| 75 |
+
)
|
| 76 |
+
if st.button(f":green[Download **_{model_to_download}_**]"):
|
| 77 |
+
try:
|
| 78 |
+
ollama.pull(model_to_download)
|
| 79 |
+
st.toast(
|
| 80 |
+
f"""Downloaded model: {
|
| 81 |
+
model_to_download}""",
|
| 82 |
+
icon="β
",
|
| 83 |
+
)
|
| 84 |
+
st.rerun()
|
| 85 |
+
except Exception as e:
|
| 86 |
+
st.error(
|
| 87 |
+
f"""Failed to download model: {
|
| 88 |
+
model_to_download}. Error: {str(e)}""",
|
| 89 |
+
icon="π³",
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
selected_model = st.selectbox(":red[**βοΈ DELETE MODEL**]", available_models)
|
| 93 |
+
if st.button(f"Delete **_{selected_model}_**", type="primary"):
|
| 94 |
+
try:
|
| 95 |
+
ollama.delete(selected_model)
|
| 96 |
+
st.toast(f"Deleted model: {selected_model}", icon="β
")
|
| 97 |
+
st.rerun()
|
| 98 |
+
except Exception as e:
|
| 99 |
+
st.error(
|
| 100 |
+
f"""Failed to delete model: {
|
| 101 |
+
selected_model}. Error: {str(e)}""",
|
| 102 |
+
icon="π³",
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
if not available_models:
|
| 106 |
+
return
|
| 107 |
+
|
| 108 |
+
selected_model = col_2.selectbox(
|
| 109 |
+
"Pick a model available locally on your system β", available_models, key=1
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
if "chats" not in st.session_state:
|
| 113 |
+
st.session_state.chats = []
|
| 114 |
+
|
| 115 |
+
if "uploaded_file_state" not in st.session_state:
|
| 116 |
+
st.session_state.uploaded_file_state = None
|
| 117 |
+
|
| 118 |
+
uploaded_file = st.file_uploader(
|
| 119 |
+
"Upload an image for analysis", type=["png", "jpg", "jpeg"]
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
col1, col2 = st.columns(2)
|
| 123 |
+
|
| 124 |
+
with col2:
|
| 125 |
+
container1 = st.container(height=500, border=True)
|
| 126 |
+
with container1:
|
| 127 |
+
if uploaded_file is not None:
|
| 128 |
+
st.session_state.uploaded_file_state = uploaded_file.getvalue()
|
| 129 |
+
image = Image.open(BytesIO(st.session_state.uploaded_file_state))
|
| 130 |
+
st.image(image, caption="Uploaded image")
|
| 131 |
+
|
| 132 |
+
with col1:
|
| 133 |
+
container2 = st.container(height=500, border=True)
|
| 134 |
+
|
| 135 |
+
if uploaded_file is not None:
|
| 136 |
+
for message in st.session_state.chats:
|
| 137 |
+
avatar = "π" if message["role"] == "assistant" else "π« "
|
| 138 |
+
with container2.chat_message(message["role"], avatar=avatar):
|
| 139 |
+
if message["role"] == "user":
|
| 140 |
+
st.markdown(message["content"])
|
| 141 |
+
else:
|
| 142 |
+
st.markdown(message["content"])
|
| 143 |
+
|
| 144 |
+
if user_input := st.chat_input(
|
| 145 |
+
"Question about the image...", key="chat_input"
|
| 146 |
+
):
|
| 147 |
+
st.session_state.chats.append({"role": "user", "content": user_input})
|
| 148 |
+
container2.chat_message("user", avatar="π« ").markdown(user_input)
|
| 149 |
+
|
| 150 |
+
image_base64 = img_to_base64(image)
|
| 151 |
+
API_URL = "http://localhost:11434/api/generate"
|
| 152 |
+
headers = {
|
| 153 |
+
"Content-Type": "application/json",
|
| 154 |
+
"Accept": "application/json",
|
| 155 |
+
}
|
| 156 |
+
data = {
|
| 157 |
+
"model": selected_model,
|
| 158 |
+
"prompt": user_input,
|
| 159 |
+
"images": [image_base64],
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
with container2.chat_message("assistant", avatar="π"):
|
| 163 |
+
with st.spinner(":blue[processing...]"):
|
| 164 |
+
response = requests.post(API_URL, json=data, headers=headers)
|
| 165 |
+
if response.status_code == 200:
|
| 166 |
+
response_lines = response.text.split("\n")
|
| 167 |
+
llava_response = ""
|
| 168 |
+
for line in response_lines:
|
| 169 |
+
if line.strip(): # Skip empty lines
|
| 170 |
+
try:
|
| 171 |
+
response_data = json.loads(line)
|
| 172 |
+
if "response" in response_data:
|
| 173 |
+
llava_response += response_data["response"]
|
| 174 |
+
except json.JSONDecodeError:
|
| 175 |
+
pass # Skip invalid JSON lines
|
| 176 |
+
if llava_response:
|
| 177 |
+
st.markdown(llava_response)
|
| 178 |
+
else:
|
| 179 |
+
st.error(
|
| 180 |
+
f"""No response received from {
|
| 181 |
+
selected_model}.""",
|
| 182 |
+
icon="π³",
|
| 183 |
+
)
|
| 184 |
+
else:
|
| 185 |
+
st.error(
|
| 186 |
+
f"""Failed to get a response from {
|
| 187 |
+
selected_model}.""",
|
| 188 |
+
icon="π³",
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
st.session_state.chats.append(
|
| 192 |
+
{"role": "assistant", "content": llava_response}
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
if __name__ == "__main__":
|
| 197 |
+
main()
|
|
|