-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparkchat.py
More file actions
102 lines (87 loc) · 2.55 KB
/
sparkchat.py
File metadata and controls
102 lines (87 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import streamlit as st
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
# Page config
st.set_page_config(
page_title="SparkChat ⚡",
page_icon="⚡",
layout="centered",
)
# Custom CSS styling
st.markdown("""
<style>
body {
background-color: #f5f7fa;
}
.stChatMessage {
padding: 0.5rem;
border-radius: 10px;
}
.stChatMessage.user {
background-color: #dbeafe;
}
.stChatMessage.assistant {
background-color: #fef9c3;
}
.st-emotion-cache-16txtl3 {
font-size: 1.2rem;
}
footer {visibility: hidden;}
</style>
""", unsafe_allow_html=True)
# App header
st.markdown(
"""
<h1 style="text-align: center; color: #4F46E5; margin-bottom: 0;">
⚡ SparkChat
</h1>
<p style="text-align: center; color: #6B7280; margin-top: 0;">
Your personal AI conversation partner, powered by OpenAI.
</p>
""",
unsafe_allow_html=True
)
# Chat history
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state["messages"]:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Initialize model
if "model" not in st.session_state:
st.session_state.model = "gpt-4o-mini"
# User input
if user_prompt := st.chat_input("Ask me anything... 💬"):
st.session_state.messages.append({"role": "user", "content": user_prompt})
with st.chat_message("user"):
st.markdown(user_prompt)
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
stream = client.chat.completions.create(
model=st.session_state.model,
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True,
)
for chunk in stream:
token = chunk.choices[0].delta.content
if token:
full_response += token
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
# Optional footer credits
st.markdown(
"""
<hr style="margin-top: 2em;">
<p style="text-align: center; color: #9CA3AF;">
⚡ SparkChat — Powered by OpenAI
</p>
""",
unsafe_allow_html=True
)