Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from api_helper import call_api # Import the API helper
|
| 3 |
+
|
| 4 |
+
# Streamlit Configurations
|
| 5 |
+
st.set_page_config(page_title="Chatbot", layout="centered")
|
| 6 |
+
|
| 7 |
+
# Chat History Initialization
|
| 8 |
+
if "messages" not in st.session_state:
|
| 9 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
|
| 10 |
+
|
| 11 |
+
# Main Chat UI
|
| 12 |
+
st.title("ChatGPT-like Frontend")
|
| 13 |
+
st.markdown("A simple ChatGPT-style frontend powered by your custom backend.")
|
| 14 |
+
|
| 15 |
+
# Chat Display
|
| 16 |
+
for message in st.session_state["messages"]:
|
| 17 |
+
if message["role"] == "user":
|
| 18 |
+
st.markdown(
|
| 19 |
+
f"<div style='text-align: right; color: blue; margin: 10px 0;'><b>You:</b> {message['content']}</div>",
|
| 20 |
+
unsafe_allow_html=True,
|
| 21 |
+
)
|
| 22 |
+
elif message["role"] == "assistant":
|
| 23 |
+
st.markdown(
|
| 24 |
+
f"<div style='text-align: left; color: green; margin: 10px 0;'><b>Bot:</b> {message['content']}</div>",
|
| 25 |
+
unsafe_allow_html=True,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# User Input
|
| 29 |
+
with st.container():
|
| 30 |
+
user_input = st.text_input("Your message", placeholder="Type your message here...", key="user_input")
|
| 31 |
+
|
| 32 |
+
if user_input:
|
| 33 |
+
# Save User Message
|
| 34 |
+
st.session_state["messages"].append({"role": "user", "content": user_input})
|
| 35 |
+
|
| 36 |
+
# Get Bot Response
|
| 37 |
+
bot_response = call_api(user_input)
|
| 38 |
+
st.session_state["messages"].append({"role": "assistant", "content": bot_response})
|
| 39 |
+
|
| 40 |
+
# Clear Input
|
| 41 |
+
st.experimental_rerun()
|