Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from helper import invoke_text_api, invoke_text_image_api
|
4 |
+
import base64
|
5 |
+
|
6 |
+
# App Title with Emoji
|
7 |
+
st.title("π€ ChatBot with Image Support πΌοΈ")
|
8 |
+
|
9 |
+
# Sidebar for Image Upload
|
10 |
+
st.sidebar.header("π Upload an Image (Optional)")
|
11 |
+
uploaded_file = st.sidebar.file_uploader("πΈ Upload an image", type=["png", "jpg", "jpeg"])
|
12 |
+
|
13 |
+
# Sidebar Parameters for AI Model
|
14 |
+
st.sidebar.header("βοΈ Model Parameters")
|
15 |
+
|
16 |
+
max_tokens = st.sidebar.slider("π Max Tokens (Response Length)", 100, 2000, 1000)
|
17 |
+
temperature = st.sidebar.slider("π₯ Temperature (Creativity)", 0.0, 1.0, 0.7)
|
18 |
+
top_k = st.sidebar.slider("π― Top K (Diversity Filter)", 1, 500, 150)
|
19 |
+
top_p = st.sidebar.slider("π Top P (Probability Sampling)", 0.0, 1.0, 0.98)
|
20 |
+
|
21 |
+
# Initialize chat history
|
22 |
+
if "messages" not in st.session_state:
|
23 |
+
st.session_state.messages = []
|
24 |
+
|
25 |
+
# Display chat messages from history
|
26 |
+
for message in st.session_state.messages:
|
27 |
+
with st.chat_message(message["role"]):
|
28 |
+
st.markdown(message["content"])
|
29 |
+
|
30 |
+
# Handle user input
|
31 |
+
if prompt := st.chat_input("What would you like to ask? π€"):
|
32 |
+
# Display user message
|
33 |
+
st.chat_message("user").markdown(prompt)
|
34 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
35 |
+
|
36 |
+
# Call the appropriate API function based on input type
|
37 |
+
with st.spinner("π€ Thinking..."):
|
38 |
+
if uploaded_file:
|
39 |
+
# Convert uploaded image to Base64
|
40 |
+
image_bytes = uploaded_file.read()
|
41 |
+
base64_image = base64.b64encode(image_bytes).decode("utf-8")
|
42 |
+
response = invoke_text_image_api(base64_image, prompt, max_tokens, temperature, top_k, top_p)
|
43 |
+
else:
|
44 |
+
response = invoke_text_api(prompt, max_tokens, temperature, top_k, top_p)
|
45 |
+
|
46 |
+
# Display assistant response
|
47 |
+
with st.chat_message("assistant"):
|
48 |
+
st.markdown(response)
|
49 |
+
|
50 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|