Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,6 @@ import pandas as pd
|
|
6 |
from datetime import datetime
|
7 |
|
8 |
# Get environment variables
|
9 |
-
generated_user = os.getenv("User")
|
10 |
-
generated_password = os.getenv("Password")
|
11 |
openai_key = os.getenv("openai_key")
|
12 |
assistant_id = os.getenv("ASSISTANT_ID")
|
13 |
|
@@ -42,101 +40,84 @@ st.markdown("""
|
|
42 |
</style>
|
43 |
""", unsafe_allow_html=True)
|
44 |
|
45 |
-
|
46 |
-
st.session_state.authenticated = False
|
47 |
-
|
48 |
-
if not st.session_state.authenticated:
|
49 |
-
st.subheader("🔑 Login")
|
50 |
-
username = st.text_input("Username")
|
51 |
-
password = st.text_input("Password", type="password")
|
52 |
-
|
53 |
-
if username and password:
|
54 |
-
if username == generated_user and password == generated_password:
|
55 |
-
st.session_state.authenticated = True
|
56 |
-
st.success("Login successful! Redirecting...")
|
57 |
-
time.sleep(1)
|
58 |
-
st.rerun()
|
59 |
-
else:
|
60 |
-
st.error("Incorrect username or password. Please try again.")
|
61 |
-
else:
|
62 |
-
st.divider()
|
63 |
-
|
64 |
-
if "thread_id" not in st.session_state:
|
65 |
-
st.session_state["thread_id"] = None
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
user_input = st.text_input("Type your message here...", key="chat_input")
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
st.success("Chat cleared.")
|
75 |
-
st.rerun()
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
#
|
87 |
-
client.beta.threads.messages.
|
88 |
-
thread_id=st.session_state["thread_id"]
|
89 |
)
|
90 |
-
append_to_transcript(st.session_state["thread_id"], "user", user_input)
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
)
|
97 |
-
while True:
|
98 |
-
run_status = client.beta.threads.runs.retrieve(
|
99 |
-
thread_id=st.session_state["thread_id"], run_id=run.id
|
100 |
-
)
|
101 |
-
if run_status.status == "completed":
|
102 |
-
break
|
103 |
-
time.sleep(1)
|
104 |
-
|
105 |
-
# Get full conversation
|
106 |
-
messages_response = client.beta.threads.messages.list(
|
107 |
-
thread_id=st.session_state["thread_id"]
|
108 |
-
)
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
)
|
122 |
-
else:
|
123 |
-
response_text = msg.content[0].text.value
|
124 |
-
append_to_transcript(st.session_state["thread_id"], "assistant", response_text)
|
125 |
-
st.markdown(
|
126 |
-
f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
127 |
-
f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {response_text}"
|
128 |
-
f"</div>",
|
129 |
-
unsafe_allow_html=True
|
130 |
-
)
|
131 |
-
|
132 |
-
# Scroll to top automatically
|
133 |
-
st.markdown("""
|
134 |
-
<script>
|
135 |
-
window.scrollTo(0, 0);
|
136 |
-
</script>
|
137 |
-
""", unsafe_allow_html=True)
|
138 |
-
|
139 |
-
except Exception as e:
|
140 |
-
st.error(f"An error occurred: {str(e)}")
|
141 |
-
else:
|
142 |
-
st.error("⚠️ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")
|
|
|
6 |
from datetime import datetime
|
7 |
|
8 |
# Get environment variables
|
|
|
|
|
9 |
openai_key = os.getenv("openai_key")
|
10 |
assistant_id = os.getenv("ASSISTANT_ID")
|
11 |
|
|
|
40 |
</style>
|
41 |
""", unsafe_allow_html=True)
|
42 |
|
43 |
+
st.divider()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
if "thread_id" not in st.session_state:
|
46 |
+
st.session_state["thread_id"] = None
|
|
|
47 |
|
48 |
+
input_col, clear_col = st.columns([8, 1])
|
49 |
+
with input_col:
|
50 |
+
user_input = st.text_input("Type your message here...", key="chat_input")
|
|
|
|
|
51 |
|
52 |
+
with clear_col:
|
53 |
+
if st.button("🗑️", help="Clear Chat"):
|
54 |
+
st.session_state["thread_id"] = None
|
55 |
+
st.success("Chat cleared.")
|
56 |
+
st.rerun()
|
57 |
+
|
58 |
+
if openai_key and assistant_id:
|
59 |
+
client = OpenAI(api_key=openai_key)
|
60 |
+
|
61 |
+
if user_input:
|
62 |
+
# Create thread if not exists
|
63 |
+
if not st.session_state["thread_id"]:
|
64 |
+
thread = client.beta.threads.create()
|
65 |
+
st.session_state["thread_id"] = thread.id
|
66 |
+
|
67 |
+
# Add user message
|
68 |
+
client.beta.threads.messages.create(
|
69 |
+
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
70 |
+
)
|
71 |
+
append_to_transcript(st.session_state["thread_id"], "user", user_input)
|
72 |
+
|
73 |
+
try:
|
74 |
+
with st.spinner("Thinking and typing... 💭"):
|
75 |
+
run = client.beta.threads.runs.create(
|
76 |
+
thread_id=st.session_state["thread_id"], assistant_id=assistant_id
|
77 |
+
)
|
78 |
+
while True:
|
79 |
+
run_status = client.beta.threads.runs.retrieve(
|
80 |
+
thread_id=st.session_state["thread_id"], run_id=run.id
|
81 |
+
)
|
82 |
+
if run_status.status == "completed":
|
83 |
+
break
|
84 |
+
time.sleep(1)
|
85 |
|
86 |
+
# Get full conversation
|
87 |
+
messages_response = client.beta.threads.messages.list(
|
88 |
+
thread_id=st.session_state["thread_id"]
|
89 |
)
|
|
|
90 |
|
91 |
+
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
|
92 |
+
|
93 |
+
# Display newest-first
|
94 |
+
messages_sorted = sorted(messages_response.data, key=lambda x: x.created_at, reverse=True)
|
95 |
+
for msg in messages_sorted:
|
96 |
+
if msg.role == "user":
|
97 |
+
st.markdown(
|
98 |
+
f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
99 |
+
f"👤 <strong>You:</strong> {msg.content[0].text.value}"
|
100 |
+
f"</div>",
|
101 |
+
unsafe_allow_html=True
|
102 |
+
)
|
103 |
+
else:
|
104 |
+
response_text = msg.content[0].text.value
|
105 |
+
append_to_transcript(st.session_state["thread_id"], "assistant", response_text)
|
106 |
+
st.markdown(
|
107 |
+
f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
108 |
+
f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {response_text}"
|
109 |
+
f"</div>",
|
110 |
+
unsafe_allow_html=True
|
111 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
+
# Scroll to top automatically
|
114 |
+
st.markdown("""
|
115 |
+
<script>
|
116 |
+
window.scrollTo(0, 0);
|
117 |
+
</script>
|
118 |
+
""", unsafe_allow_html=True)
|
119 |
+
|
120 |
+
except Exception as e:
|
121 |
+
st.error(f"An error occurred: {str(e)}")
|
122 |
+
else:
|
123 |
+
st.error("⚠️ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|