CoolFace
Apppublic

exnav29/Real_Estate_Bot_Streamlit_edition

sourceHugging Facecc-by-nd-4.0updated 3y agoView on Hugging Face
0likes
app.py264 linesDownload Raw Back to root
1import os2import openai3import streamlit as st4import json5 6# Set OpenAI API key7openai.api_key = os.environ["OPENAI_API_KEY"]8 9# Set page config10st.set_page_config(page_title="JChat Real Estate Investing Chatbot", page_icon="🏘️", layout="wide",)11 12# Define path for saving chat history13CHAT_HISTORY_PATH = "chat_history.json"14 15# Define chat history object and counter16chat_history = []17counter = 018 19# Load existing chat history if file exists20if os.path.exists(CHAT_HISTORY_PATH):21    use_chat_history = st.sidebar.checkbox("Use existing chat history?")22    if use_chat_history:23        with open(CHAT_HISTORY_PATH, "r") as f:24            chat_history = json.load(f)25else:26    st.sidebar.info("No chat history found.")27 28# Define chatbot function29def chatbot(input):30    global counter31    global chat_history32 33    # Append user input to chat history34    if input:35        chat_history.append({"role": "user", "content": input})36 37    # Get AI response from OpenAI API38    if len(chat_history) >= 2:39        prompt = f"{chat_history[-2]['content']}\nUser: {chat_history[-1]['content']}\nAI:"40    else:41        prompt = f"User: {chat_history[-1]['content']}\nAI:"42    response = openai.Completion.create(43        engine="text-davinci-002",44        prompt=prompt,45        max_tokens=100,46        n=1,47        stop=None,48        temperature=0.2,49    ).choices[0].text50 51    # Append AI response to chat history52    chat_history.append({"role": "assistant", "content": response})53    counter += 154 55    # Save chat history to file56    with open(CHAT_HISTORY_PATH, "w") as f:57        json.dump(chat_history, f)58 59    # Check if it's time to prompt for donation60    if counter >= 3:61        st.experimental_rerun()62        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)63        counter = 064 65    return response66 67# Define main function68def main():69    if not os.path.isfile(CHAT_HISTORY_PATH):70        if st.button("Create chat history file"):71            with open(CHAT_HISTORY_PATH, "w") as f:72                f.write('')73                st.write("Chat history file created successfully!")74        else:75            st.warning("Without a chat history file, your chat history will not be saved.")76 77    st.sidebar.checkbox("Use existing chat history?", value=True)78    st.sidebar.subheader("Chat history:")79    for i in range(max(len(chat_history) - 3, 0), len(chat_history)):80        if chat_history[i]["role"] == "user":81            st.sidebar.text(f"User: {chat_history[i]['content']}")82        else:83            st.sidebar.text(f"AI: {chat_history[i]['content']}")84 85# Add custom CSS86st.markdown(87    """88    <style>89    body {90  background-color: #1c1c1c;91  color: #ffffff;92}93 94/* Header */95header {96  background-color: #000000;97  color: #ffffff;98  padding: 1rem;99  border-bottom: 1px solid #ffffff;100}101 102h1 {103  font-size: 2.5rem;104  font-weight: bold;105  color: #00ff7f;106  text-shadow: 2px 2px 0 #000000;107  text-transform: uppercase;108  letter-spacing: 0.1em;109  margin-bottom: 0.5rem;110}111 112/* Sidebar */113.sidebar {114  background-color: #2d2d2d;115  color: #ffffff;116  padding: 1rem;117}118 119.sidebar h2 {120  font-size: 1.5rem;121  font-weight: bold;122  text-transform: uppercase;123  letter-spacing: 0.1em;124  margin-bottom: 0.5rem;125}126 127.sidebar p {128  margin-bottom: 1rem;129}130 131.sidebar ul {132  list-style: none;133  padding: 0;134  margin: 0;135}136 137.sidebar li {138  margin-bottom: 0.5rem;139}140 141.sidebar a {142  color: #ffffff;143  text-decoration: none;144  font-weight: bold;145}146 147/* Main content */148.main {149  background-color: #1c1c1c;150  color: #ffffff;151  padding: 1rem;152}153 154.main h2 {155  font-size: 1.5rem;156  font-weight: bold;157  text-transform: uppercase;158  letter-spacing: 0.1em;159  margin-bottom: 0.5rem;160}161 162.main p {163  margin-bottom: 1rem;164}165 166.main a {167  color: #39ff14;168  text-decoration: none;169  font-weight: bold;170}171 172/* Buttons */173button {174  background-color: #39ff14;175  color: #000000;176  border: none;177  padding: 0.5rem 1rem;178  font-size: 1rem;179  font-weight: bold;180  text-transform: uppercase;181  letter-spacing: 0.1em;182  cursor: pointer;183}184 185button:hover {186  background-color: #38ff13;187}188 189button:active {190  background-color: #37ff12;191}192 193button:focus {194  outline: none;195}196 197/* Form */198form label {199  display: block;200  font-size: 1rem;201  font-weight: bold;202  text-transform: uppercase;203  letter-spacing: 0.1em;204  margin-bottom: 0.5rem;205}206 207form input[type="text"],208form input[type="email"],209form input[type="password"],210form textarea {211  background-color: #2d2d2d;212  color: #ffffff;213  border: none;214  padding: 0.5rem;215  margin-bottom: 1rem;216  width: 100%;217  font-size: 1rem;218  font-weight: bold;219  letter-spacing: 0.1em;220}221 222form input[type="text"]:focus,223form input[type="email"]:focus,224form input[type="password"]:focus,225form textarea:focus {226  outline: none;227  box-shadow: 0 0 0 2px #39ff14;228}229 230.title {231  font-size: 48px;232  color: #39ff14;233  text-align: center;234  margin-top: 0;235  margin-bottom: 1rem;236  text-transform: uppercase;237}238 239form button[type="submit"] {240  background-color: #39ff14;241  color: #000000;242  border: none;243  padding: 0.5rem 1rem;244  font-size: 1rem;245  font-weight: bold;246  text-transform: uppercase;247  letter-spacing: 248 249    </style>250    """,251    unsafe_allow_html=True,252)253 254# Add content to your app255st.title("JChat Real Estate Investing Chatbot")256st.write("Ask anything you want about investing in real estate")257input_text = st.text_input("Ask your question")258if input_text:259    output_text = chatbot(input_text)260    st.write(output_text)261 262if __name__ == '__main__':263    main()264