Spaces:
Running
Running
Add image upload functionality and base64 encoding in app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,27 @@ import os
|
|
2 |
from dotenv import find_dotenv, load_dotenv
|
3 |
import streamlit as st
|
4 |
from groq import Groq
|
|
|
5 |
|
6 |
# Load environment variables
|
7 |
load_dotenv(find_dotenv())
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# Set up Streamlit page configuration
|
10 |
st.set_page_config(
|
11 |
page_icon="π",
|
@@ -16,9 +33,6 @@ st.set_page_config(
|
|
16 |
# App Title
|
17 |
st.title("Groq Chat with LLaMA3x")
|
18 |
|
19 |
-
# Initialize the Groq client using the API key from the environment variables
|
20 |
-
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
21 |
-
|
22 |
# Cache the model fetching function to improve performance
|
23 |
@st.cache_data
|
24 |
def fetch_available_models():
|
@@ -77,6 +91,9 @@ with st.sidebar:
|
|
77 |
# Define a function to clear messages when the model changes
|
78 |
def reset_chat_on_model_change():
|
79 |
st.session_state.messages = []
|
|
|
|
|
|
|
80 |
|
81 |
# Model selection dropdown
|
82 |
if models:
|
@@ -110,6 +127,35 @@ with st.sidebar:
|
|
110 |
# Button to clear the chat
|
111 |
if st.button("Clear Chat"):
|
112 |
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
st.markdown("### Usage Summary")
|
115 |
usage_box = st.empty()
|
@@ -132,16 +178,56 @@ st.markdown("### Chat Interface")
|
|
132 |
for message in st.session_state.messages:
|
133 |
avatar = "π" if message["role"] == "assistant" else "π§βπ»"
|
134 |
with st.chat_message(message["role"], avatar=avatar):
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
-
# Capture user input
|
138 |
-
user_input = st.chat_input("Enter your message here...")
|
139 |
|
140 |
-
|
|
|
141 |
# Append the user input to the session state
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
with st.chat_message("user", avatar="π§βπ»"):
|
|
|
144 |
st.markdown(user_input)
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
# Generate a response using the selected model
|
147 |
try:
|
|
|
2 |
from dotenv import find_dotenv, load_dotenv
|
3 |
import streamlit as st
|
4 |
from groq import Groq
|
5 |
+
import base64
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv(find_dotenv())
|
9 |
|
10 |
+
# Function to encode the image to a base64 string
|
11 |
+
def encode_image(uploaded_file):
|
12 |
+
"""
|
13 |
+
Encodes an uploaded image file into a base64 string.
|
14 |
+
|
15 |
+
Args:
|
16 |
+
uploaded_file: The file-like object uploaded via Streamlit.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
str: The base64 encoded string of the image.
|
20 |
+
"""
|
21 |
+
return base64.b64encode(uploaded_file.read()).decode('utf-8')
|
22 |
+
|
23 |
+
# Initialize the Groq client using the API key from the environment variables
|
24 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
25 |
+
|
26 |
# Set up Streamlit page configuration
|
27 |
st.set_page_config(
|
28 |
page_icon="π",
|
|
|
33 |
# App Title
|
34 |
st.title("Groq Chat with LLaMA3x")
|
35 |
|
|
|
|
|
|
|
36 |
# Cache the model fetching function to improve performance
|
37 |
@st.cache_data
|
38 |
def fetch_available_models():
|
|
|
91 |
# Define a function to clear messages when the model changes
|
92 |
def reset_chat_on_model_change():
|
93 |
st.session_state.messages = []
|
94 |
+
st.session_state.image_used = False
|
95 |
+
uploaded_file = None
|
96 |
+
base64_image = None
|
97 |
|
98 |
# Model selection dropdown
|
99 |
if models:
|
|
|
127 |
# Button to clear the chat
|
128 |
if st.button("Clear Chat"):
|
129 |
st.session_state.messages = []
|
130 |
+
st.session_state.image_used = False
|
131 |
+
|
132 |
+
# Initialize session state for tracking uploaded image usage
|
133 |
+
if "image_used" not in st.session_state:
|
134 |
+
st.session_state.image_used = False # Flag to track image usage
|
135 |
+
|
136 |
+
# Check if the selected model supports vision
|
137 |
+
base64_image = None
|
138 |
+
uploaded_file = None
|
139 |
+
if model_option and "vision" in model_option.lower():
|
140 |
+
st.markdown(
|
141 |
+
"### Upload an Image"
|
142 |
+
"\n\n*One per conversation*"
|
143 |
+
)
|
144 |
+
|
145 |
+
# File uploader for images (only if image hasn't been used yet)
|
146 |
+
if not st.session_state.image_used:
|
147 |
+
uploaded_file = st.file_uploader(
|
148 |
+
"Upload an image for the model to process:",
|
149 |
+
type=["png", "jpg", "jpeg"],
|
150 |
+
help="Upload an image if the model supports vision tasks.",
|
151 |
+
accept_multiple_files=False
|
152 |
+
)
|
153 |
+
if uploaded_file:
|
154 |
+
base64_image = encode_image(uploaded_file)
|
155 |
+
st.image(uploaded_file, caption="Uploaded Image")
|
156 |
+
else:
|
157 |
+
base64_image = None
|
158 |
+
|
159 |
|
160 |
st.markdown("### Usage Summary")
|
161 |
usage_box = st.empty()
|
|
|
178 |
for message in st.session_state.messages:
|
179 |
avatar = "π" if message["role"] == "assistant" else "π§βπ»"
|
180 |
with st.chat_message(message["role"], avatar=avatar):
|
181 |
+
# Check if the content is a list (text and image combined)
|
182 |
+
if isinstance(message["content"], list):
|
183 |
+
for item in message["content"]:
|
184 |
+
if item["type"] == "text":
|
185 |
+
st.markdown(item["text"])
|
186 |
+
elif item["type"] == "image_url":
|
187 |
+
# Handle base64-encoded image URLs
|
188 |
+
if item["image_url"]["url"].startswith("data:image"):
|
189 |
+
st.image(item["image_url"]["url"], caption="Uploaded Image")
|
190 |
+
st.session_state.image_used = True
|
191 |
+
else:
|
192 |
+
st.warning("Invalid image format or unsupported URL.")
|
193 |
+
else:
|
194 |
+
# For plain text content
|
195 |
+
st.markdown(message["content"])
|
196 |
|
|
|
|
|
197 |
|
198 |
+
# Capture user input
|
199 |
+
if user_input:=st.chat_input("Enter your message here..."):
|
200 |
# Append the user input to the session state
|
201 |
+
# including the image if uploaded
|
202 |
+
if base64_image and not st.session_state.image_used:
|
203 |
+
# Append the user message with the image to session state
|
204 |
+
st.session_state.messages.append(
|
205 |
+
{
|
206 |
+
"role": "user",
|
207 |
+
"content": [
|
208 |
+
{"type": "text", "text": user_input},
|
209 |
+
{
|
210 |
+
"type": "image_url",
|
211 |
+
"image_url": {
|
212 |
+
"url": f"data:image/jpeg;base64,{base64_image}",
|
213 |
+
},
|
214 |
+
},
|
215 |
+
],
|
216 |
+
}
|
217 |
+
)
|
218 |
+
st.session_state.image_used = True
|
219 |
+
else:
|
220 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
221 |
+
|
222 |
+
# Display the uploaded image and user query in the chat
|
223 |
with st.chat_message("user", avatar="π§βπ»"):
|
224 |
+
# Display the user input
|
225 |
st.markdown(user_input)
|
226 |
+
|
227 |
+
# Display the uploaded image only if it's included in the current message
|
228 |
+
if base64_image and st.session_state.image_used:
|
229 |
+
st.image(uploaded_file, caption="Uploaded Image")
|
230 |
+
base64_image = None
|
231 |
|
232 |
# Generate a response using the selected model
|
233 |
try:
|