Spaces:
Runtime error
Runtime error
root
commited on
Commit
·
91c1e66
1
Parent(s):
3870273
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import base64
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
# Function to encode the image
|
| 9 |
+
def encode_image(image_file):
|
| 10 |
+
return base64.b64encode(image_file.getvalue()).decode('utf-8')
|
| 11 |
+
|
| 12 |
+
# Set up the Streamlit page
|
| 13 |
+
st.set_page_config(page_title="LLM Chat Interface", page_icon="🤖")
|
| 14 |
+
st.title("LLM Chat Interface")
|
| 15 |
+
|
| 16 |
+
# Input for API endpoint and token
|
| 17 |
+
api_endpoint = st.sidebar.text_input("API Endpoint", "http://209.222.10.17:8000/v1/chat/completions")
|
| 18 |
+
api_token = st.sidebar.text_input("API Token", type="password")
|
| 19 |
+
|
| 20 |
+
# File uploader for image
|
| 21 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 22 |
+
|
| 23 |
+
# Text input for user message
|
| 24 |
+
user_input = st.text_area("Enter your message:", height=100)
|
| 25 |
+
|
| 26 |
+
if st.button("Send"):
|
| 27 |
+
if not api_token:
|
| 28 |
+
st.error("Please enter an API token.")
|
| 29 |
+
elif not user_input:
|
| 30 |
+
st.error("Please enter a message.")
|
| 31 |
+
else:
|
| 32 |
+
# Prepare the message content
|
| 33 |
+
content = [{"type": "text", "text": user_input}]
|
| 34 |
+
|
| 35 |
+
# If an image is uploaded, add it to the content
|
| 36 |
+
if uploaded_file is not None:
|
| 37 |
+
# Encode and add the image
|
| 38 |
+
image_base64 = encode_image(uploaded_file)
|
| 39 |
+
content.append({
|
| 40 |
+
"type": "image_url",
|
| 41 |
+
"image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}
|
| 42 |
+
})
|
| 43 |
+
|
| 44 |
+
# Prepare the request payload
|
| 45 |
+
payload = {
|
| 46 |
+
"model": "mistralai/Pixtral-12B-2409",
|
| 47 |
+
"messages": [{"role": "user", "content": content}]
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
# Send the request to the API
|
| 51 |
+
headers = {
|
| 52 |
+
"Content-Type": "application/json",
|
| 53 |
+
"Authorization": f"Bearer {api_token}"
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
with st.spinner("Waiting for response..."):
|
| 57 |
+
try:
|
| 58 |
+
response = requests.post(api_endpoint, headers=headers, json=payload)
|
| 59 |
+
response.raise_for_status()
|
| 60 |
+
result = response.json()
|
| 61 |
+
|
| 62 |
+
# Display the response
|
| 63 |
+
st.subheader("Response:")
|
| 64 |
+
st.write(result['choices'][0]['message']['content'])
|
| 65 |
+
except requests.exceptions.RequestException as e:
|
| 66 |
+
st.error(f"An error occurred: {str(e)}")
|
| 67 |
+
|
| 68 |
+
# Display the uploaded image
|
| 69 |
+
if uploaded_file is not None:
|
| 70 |
+
image = Image.open(uploaded_file)
|
| 71 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|