Spaces:
Sleeping
Sleeping
Commit ·
0b9218e
1
Parent(s): c6b7e90
Made image uploads fully compatible with storing chat history
Browse files- examples/vison.ipynb +2 -2
- st_image_chat.py +45 -15
examples/vison.ipynb
CHANGED
|
@@ -20,7 +20,7 @@
|
|
| 20 |
},
|
| 21 |
{
|
| 22 |
"cell_type": "code",
|
| 23 |
-
"execution_count":
|
| 24 |
"id": "4df0ad72-04a4-4b1f-bcaf-b1fb1cf0221f",
|
| 25 |
"metadata": {},
|
| 26 |
"outputs": [
|
|
@@ -28,7 +28,7 @@
|
|
| 28 |
"name": "stdout",
|
| 29 |
"output_type": "stream",
|
| 30 |
"text": [
|
| 31 |
-
"ChatCompletionMessage(content=
|
| 32 |
]
|
| 33 |
}
|
| 34 |
],
|
|
|
|
| 20 |
},
|
| 21 |
{
|
| 22 |
"cell_type": "code",
|
| 23 |
+
"execution_count": 2,
|
| 24 |
"id": "4df0ad72-04a4-4b1f-bcaf-b1fb1cf0221f",
|
| 25 |
"metadata": {},
|
| 26 |
"outputs": [
|
|
|
|
| 28 |
"name": "stdout",
|
| 29 |
"output_type": "stream",
|
| 30 |
"text": [
|
| 31 |
+
"ChatCompletionMessage(content='This is an image of a chip that is being viewed through a microscope.', role='assistant', function_call=None, tool_calls=None)\n"
|
| 32 |
]
|
| 33 |
}
|
| 34 |
],
|
st_image_chat.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Initialize Groq client
|
| 6 |
client = Groq(
|
|
@@ -9,14 +11,35 @@ client = Groq(
|
|
| 9 |
|
| 10 |
# Initialize session state for chat history if it doesn't exist
|
| 11 |
if 'messages' not in st.session_state:
|
| 12 |
-
st.session_state.messages = [
|
| 13 |
-
{"role": "system", "content": "You are a helpful assistant"}
|
| 14 |
-
]
|
| 15 |
|
| 16 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
stream = client.chat.completions.create(
|
| 18 |
-
model=
|
| 19 |
-
messages=
|
| 20 |
temperature=0.1,
|
| 21 |
top_p=1,
|
| 22 |
stream=True,
|
|
@@ -26,38 +49,45 @@ def generate_response(messages):
|
|
| 26 |
for chunk in stream:
|
| 27 |
content = chunk.choices[0].delta.content
|
| 28 |
if content:
|
| 29 |
-
yield content
|
| 30 |
|
| 31 |
-
st.title("Groq
|
| 32 |
|
| 33 |
# Display chat history
|
| 34 |
for message in st.session_state.messages:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Get user input
|
| 40 |
user_input = st.chat_input('Message to Assistant...', key='prompt_input')
|
| 41 |
|
| 42 |
if user_input:
|
| 43 |
-
# Add user message to chat history
|
| 44 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 45 |
|
| 46 |
-
# Display user message
|
| 47 |
with st.chat_message("user"):
|
| 48 |
st.markdown(user_input)
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Generate and display assistant response
|
| 51 |
with st.chat_message("assistant"):
|
| 52 |
response_placeholder = st.empty()
|
| 53 |
full_response = ""
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
# Stream the response
|
| 56 |
with st.spinner("Generating response..."):
|
| 57 |
-
for content in generate_response(st.session_state.messages):
|
| 58 |
full_response += content
|
| 59 |
response_placeholder.markdown(full_response + "▌")
|
| 60 |
response_placeholder.markdown(full_response)
|
| 61 |
|
| 62 |
-
# Add assistant response to chat history
|
| 63 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
+
from io import BytesIO
|
| 6 |
|
| 7 |
# Initialize Groq client
|
| 8 |
client = Groq(
|
|
|
|
| 11 |
|
| 12 |
# Initialize session state for chat history if it doesn't exist
|
| 13 |
if 'messages' not in st.session_state:
|
| 14 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def encode_image(uploaded_file):
|
| 17 |
+
bytes_data = uploaded_file.getvalue()
|
| 18 |
+
base64_image = base64.b64encode(bytes_data).decode('utf-8')
|
| 19 |
+
return f"data:image/{uploaded_file.type.split('/')[-1]};base64,{base64_image}"
|
| 20 |
+
|
| 21 |
+
def generate_response(messages, current_image=None):
|
| 22 |
+
# Create a copy of messages for the API call
|
| 23 |
+
api_messages = messages.copy()
|
| 24 |
+
|
| 25 |
+
# If there's a current image, add it to the last user message
|
| 26 |
+
if current_image:
|
| 27 |
+
api_messages[-1] = {
|
| 28 |
+
"role": "user",
|
| 29 |
+
"content": [
|
| 30 |
+
{"type": "text", "text": api_messages[-1]["content"]},
|
| 31 |
+
{
|
| 32 |
+
"type": "image_url",
|
| 33 |
+
"image_url": {"url": current_image}
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
model = "llama-3.2-11b-vision-preview" if current_image else "llama-3.2-3b-preview"
|
| 39 |
+
|
| 40 |
stream = client.chat.completions.create(
|
| 41 |
+
model=model,
|
| 42 |
+
messages=api_messages,
|
| 43 |
temperature=0.1,
|
| 44 |
top_p=1,
|
| 45 |
stream=True,
|
|
|
|
| 49 |
for chunk in stream:
|
| 50 |
content = chunk.choices[0].delta.content
|
| 51 |
if content:
|
| 52 |
+
yield content
|
| 53 |
|
| 54 |
+
st.title("Groq Chat")
|
| 55 |
|
| 56 |
# Display chat history
|
| 57 |
for message in st.session_state.messages:
|
| 58 |
+
with st.chat_message(message["role"]):
|
| 59 |
+
st.markdown(message["content"])
|
| 60 |
+
|
| 61 |
+
# File uploader for images
|
| 62 |
+
uploaded_file = st.file_uploader("Upload an image (optional)", type=['png', 'jpg', 'jpeg'])
|
| 63 |
|
| 64 |
# Get user input
|
| 65 |
user_input = st.chat_input('Message to Assistant...', key='prompt_input')
|
| 66 |
|
| 67 |
if user_input:
|
| 68 |
+
# Add user message to chat history (text only)
|
| 69 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 70 |
|
| 71 |
+
# Display user message with image if present
|
| 72 |
with st.chat_message("user"):
|
| 73 |
st.markdown(user_input)
|
| 74 |
+
if uploaded_file:
|
| 75 |
+
st.image(uploaded_file)
|
| 76 |
|
| 77 |
# Generate and display assistant response
|
| 78 |
with st.chat_message("assistant"):
|
| 79 |
response_placeholder = st.empty()
|
| 80 |
full_response = ""
|
| 81 |
|
| 82 |
+
# Prepare image for API call if present
|
| 83 |
+
current_image = encode_image(uploaded_file) if uploaded_file else None
|
| 84 |
+
|
| 85 |
# Stream the response
|
| 86 |
with st.spinner("Generating response..."):
|
| 87 |
+
for content in generate_response(st.session_state.messages, current_image):
|
| 88 |
full_response += content
|
| 89 |
response_placeholder.markdown(full_response + "▌")
|
| 90 |
response_placeholder.markdown(full_response)
|
| 91 |
|
| 92 |
+
# Add assistant response to chat history (text only)
|
| 93 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|