Spaces:
Sleeping
Sleeping
Commit ·
1a86107
1
Parent(s): b387a86
Replaced finicky groq client with the reliable requests library; updated dependencies
Browse files- app.py +42 -29
- examples/groq_basics.py +41 -0
- st_long_context_basic.py → examples/st_long_context_basic.py +0 -0
- requirements.txt +2 -6
app.py
CHANGED
|
@@ -1,53 +1,66 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
| 3 |
import os
|
| 4 |
import base64
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
client = Groq(
|
| 9 |
-
api_key=os.environ.get("GROQ_API_KEY"),
|
| 10 |
-
)
|
| 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, model, current_image=None):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
#
|
|
|
|
| 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 |
-
|
| 39 |
-
model
|
| 40 |
-
messages
|
| 41 |
-
temperature
|
| 42 |
-
top_p
|
| 43 |
-
stream
|
| 44 |
-
stop
|
| 45 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
st.title("Fast Inference Chat")
|
| 53 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
import os
|
| 5 |
import base64
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
+
# Define function to encode image to base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def encode_image(uploaded_file):
|
| 10 |
bytes_data = uploaded_file.getvalue()
|
| 11 |
base64_image = base64.b64encode(bytes_data).decode('utf-8')
|
| 12 |
return f"data:image/{uploaded_file.type.split('/')[-1]};base64,{base64_image}"
|
| 13 |
|
| 14 |
+
# Define function to generate response from Groq API
|
| 15 |
def generate_response(messages, model, current_image=None):
|
| 16 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 17 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 18 |
|
| 19 |
+
# Prepare messages for API
|
| 20 |
+
api_messages = messages.copy()
|
| 21 |
if current_image:
|
| 22 |
api_messages[-1] = {
|
| 23 |
"role": "user",
|
| 24 |
"content": [
|
| 25 |
{"type": "text", "text": api_messages[-1]["content"]},
|
| 26 |
+
{"type": "image_url", "image_url": {"url": current_image}}
|
|
|
|
|
|
|
|
|
|
| 27 |
]
|
| 28 |
}
|
| 29 |
|
| 30 |
+
payload = json.dumps({
|
| 31 |
+
"model": model,
|
| 32 |
+
"messages": api_messages,
|
| 33 |
+
"temperature": 0.1,
|
| 34 |
+
"top_p": 1,
|
| 35 |
+
"stream": True,
|
| 36 |
+
"stop": None
|
| 37 |
+
})
|
| 38 |
+
|
| 39 |
+
headers = {
|
| 40 |
+
'Content-Type': 'application/json',
|
| 41 |
+
'Authorization': f'Bearer {api_key}'
|
| 42 |
+
}
|
| 43 |
|
| 44 |
+
with requests.post(url, headers=headers, data=payload, stream=True) as response:
|
| 45 |
+
for line in response.iter_lines():
|
| 46 |
+
if line:
|
| 47 |
+
# Each line should start with 'data: '
|
| 48 |
+
if line.startswith(b'data: '):
|
| 49 |
+
data = line[len(b'data: '):]
|
| 50 |
+
if data == b'[DONE]':
|
| 51 |
+
break
|
| 52 |
+
try:
|
| 53 |
+
chunk = json.loads(data)
|
| 54 |
+
# Defensive: check for content in streaming chunk
|
| 55 |
+
content = chunk.get('choices', [{}])[0].get('delta', {}).get('content')
|
| 56 |
+
if content:
|
| 57 |
+
yield content
|
| 58 |
+
except Exception:
|
| 59 |
+
continue
|
| 60 |
+
|
| 61 |
+
# Initialize session state for chat history if it doesn't exist
|
| 62 |
+
if 'messages' not in st.session_state:
|
| 63 |
+
st.session_state.messages = []
|
| 64 |
|
| 65 |
st.title("Fast Inference Chat")
|
| 66 |
|
examples/groq_basics.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
api_key = os.getenv('GROQ_API_KEY')
|
| 6 |
+
|
| 7 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
+
|
| 9 |
+
payload = json.dumps({
|
| 10 |
+
"messages": [
|
| 11 |
+
{
|
| 12 |
+
"role": "user",
|
| 13 |
+
"content": [
|
| 14 |
+
{
|
| 15 |
+
"type": "text",
|
| 16 |
+
"text": "What's in this image?"
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"type": "image_url",
|
| 20 |
+
"image_url": {
|
| 21 |
+
"url": "https://upload.wikimedia.org/wikipedia/commons/f/f2/LPU-v1-die.jpg"
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
]
|
| 25 |
+
}
|
| 26 |
+
],
|
| 27 |
+
"model": "meta-llama/llama-4-scout-17b-16e-instruct",
|
| 28 |
+
"temperature": 1,
|
| 29 |
+
"max_completion_tokens": 1024,
|
| 30 |
+
"top_p": 1,
|
| 31 |
+
"stream": False,
|
| 32 |
+
"stop": None
|
| 33 |
+
})
|
| 34 |
+
headers = {
|
| 35 |
+
'Content-Type': 'application/json',
|
| 36 |
+
'Authorization': f'Bearer {api_key}'
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
| 40 |
+
|
| 41 |
+
print(response.text)
|
st_long_context_basic.py → examples/st_long_context_basic.py
RENAMED
|
File without changes
|
requirements.txt
CHANGED
|
@@ -1,7 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
langchain-core==0.3.15
|
| 4 |
-
langchain-groq==0.2.1
|
| 5 |
-
langchain-text-splitters==0.3.2
|
| 6 |
-
autogen==0.3.1
|
| 7 |
streamlit==1.40.0
|
|
|
|
| 1 |
+
requests==2.32.3
|
| 2 |
+
requests-toolbelt==1.0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
streamlit==1.40.0
|