Spaces:
Runtime error
Runtime error
File size: 5,632 Bytes
ea00f81 87bbecf ea00f81 b2fec43 ea00f81 18e1807 d3c7ca5 18e1807 b2fec43 d0ef483 b2fec43 e921d69 b2fec43 d0ef483 95b1ea2 87bbecf b2fec43 ea00f81 87bbecf b2fec43 95b1ea2 b2fec43 d0ef483 b2fec43 ea00f81 95b1ea2 b2fec43 d3c7ca5 5edf636 d3c7ca5 7f25ab9 d308107 d8ce419 7f25ab9 d3c7ca5 062d690 ea00f81 1aa1834 |
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
import os
import openai
import streamlit as st
import json
# Set OpenAI API key
openai.api_key = os.environ["OPENAI_API_KEY"]
# Set page config
st.set_page_config(page_title="JChat Real Estate Investing Chatbot", page_icon="🏘️", layout="wide",)
# Define path for saving chat history
CHAT_HISTORY_PATH = "chat_history.json"
# Define chat history object and counter
chat_history = []
counter = 0
# Load existing chat history if file exists
if os.path.exists(CHAT_HISTORY_PATH):
use_chat_history = st.sidebar.checkbox("Use existing chat history?")
if use_chat_history:
with open(CHAT_HISTORY_PATH, "r") as f:
chat_history = json.load(f)
else:
st.sidebar.info("No chat history found.")
# Define chatbot function
def chatbot(input):
global counter
global chat_history
# Append user input to chat history
if input:
chat_history.append({"role": "user", "content": input})
# Get AI response from OpenAI API
if len(chat_history) >= 2:
prompt = f"{chat_history[-2]['content']}\nUser: {chat_history[-1]['content']}\nAI:"
else:
prompt = f"User: {chat_history[-1]['content']}\nAI:"
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=100,
n=1,
stop=None,
temperature=0.2,
).choices[0].text
# Append AI response to chat history
chat_history.append({"role": "assistant", "content": response})
counter += 1
# Save chat history to file
with open(CHAT_HISTORY_PATH, "w") as f:
json.dump(chat_history, f)
# Check if it's time to prompt for donation
if counter >= 3:
st.experimental_rerun()
st.write("Thank you for using our chatbot! If you find our service helpful, please consider making a donation to support us: <a href='https://www.paypal.me/exnav29' target='_blank'>Donate Now</a>", unsafe_allow_html=True)
counter = 0
return response
# Define main function
def main():
if not os.path.isfile(CHAT_HISTORY_PATH):
if st.button("Create chat history file"):
with open(CHAT_HISTORY_PATH, "w") as f:
f.write('')
st.write("Chat history file created successfully!")
else:
st.warning("Without a chat history file, your chat history will not be saved.")
st.sidebar.checkbox("Use existing chat history?", value=True)
st.sidebar.subheader("Chat history:")
for i in range(max(len(chat_history) - 3, 0), len(chat_history)):
if chat_history[i]["role"] == "user":
st.sidebar.text(f"User: {chat_history[i]['content']}")
else:
st.sidebar.text(f"AI: {chat_history[i]['content']}")
# Add custom CSS
st.markdown(
"""
<style>
body {
background-color: #1c1c1c;
color: #ffffff;
}
/* Header */
header {
background-color: #000000;
color: #ffffff;
padding: 1rem;
border-bottom: 1px solid #ffffff;
}
h1 {
font-size: 2.5rem;
font-weight: bold;
color: #00ff7f;
text-shadow: 2px 2px 0 #000000;
text-transform: uppercase;
letter-spacing: 0.1em;
margin-bottom: 0.5rem;
}
/* Sidebar */
.sidebar {
background-color: #2d2d2d;
color: #ffffff;
padding: 1rem;
}
.sidebar h2 {
font-size: 1.5rem;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 0.1em;
margin-bottom: 0.5rem;
}
.sidebar p {
margin-bottom: 1rem;
}
.sidebar ul {
list-style: none;
padding: 0;
margin: 0;
}
.sidebar li {
margin-bottom: 0.5rem;
}
.sidebar a {
color: #ffffff;
text-decoration: none;
font-weight: bold;
}
/* Main content */
.main {
background-color: #1c1c1c;
color: #ffffff;
padding: 1rem;
}
.main h2 {
font-size: 1.5rem;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 0.1em;
margin-bottom: 0.5rem;
}
.main p {
margin-bottom: 1rem;
}
.main a {
color: #39ff14;
text-decoration: none;
font-weight: bold;
}
/* Buttons */
button {
background-color: #39ff14;
color: #000000;
border: none;
padding: 0.5rem 1rem;
font-size: 1rem;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 0.1em;
cursor: pointer;
}
button:hover {
background-color: #38ff13;
}
button:active {
background-color: #37ff12;
}
button:focus {
outline: none;
}
/* Form */
form label {
display: block;
font-size: 1rem;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 0.1em;
margin-bottom: 0.5rem;
}
form input[type="text"],
form input[type="email"],
form input[type="password"],
form textarea {
background-color: #2d2d2d;
color: #ffffff;
border: none;
padding: 0.5rem;
margin-bottom: 1rem;
width: 100%;
font-size: 1rem;
font-weight: bold;
letter-spacing: 0.1em;
}
form input[type="text"]:focus,
form input[type="email"]:focus,
form input[type="password"]:focus,
form textarea:focus {
outline: none;
box-shadow: 0 0 0 2px #39ff14;
}
.title {
font-size: 48px;
color: #39ff14;
text-align: center;
margin-top: 0;
margin-bottom: 1rem;
text-transform: uppercase;
}
form button[type="submit"] {
background-color: #39ff14;
color: #000000;
border: none;
padding: 0.5rem 1rem;
font-size: 1rem;
font-weight: bold;
text-transform: uppercase;
letter-spacing:
</style>
""",
unsafe_allow_html=True,
)
# Add content to your app
st.title("JChat Real Estate Investing Chatbot")
st.write("Ask anything you want about investing in real estate")
input_text = st.text_input("Ask your question")
if input_text:
output_text = chatbot(input_text)
st.write(output_text)
if __name__ == '__main__':
main()
|