Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,66 +4,77 @@ import time
|
|
4 |
import datetime
|
5 |
import os
|
6 |
|
7 |
-
#
|
|
|
|
|
8 |
openai_key = os.getenv("openai_key")
|
9 |
|
|
|
10 |
st.set_page_config(page_title="Carfind.co.za AI Assistant")
|
11 |
-
|
12 |
st.title("Carfind.co.za AI Assistant")
|
13 |
-
st.caption("Chat with Carfind.co.za and
|
14 |
|
15 |
-
#
|
16 |
if "authenticated" not in st.session_state:
|
17 |
st.session_state.authenticated = False
|
18 |
|
|
|
19 |
if not st.session_state.authenticated:
|
20 |
st.subheader("Login")
|
21 |
username = st.text_input("Username")
|
22 |
password = st.text_input("Password", type="password")
|
23 |
|
24 |
-
|
25 |
-
|
|
|
26 |
st.session_state.authenticated = True
|
27 |
-
st.success("Login successful!")
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
else:
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
-
#
|
36 |
if not openai_key:
|
37 |
-
st.error("OpenAI key not found. Please
|
38 |
else:
|
39 |
client = OpenAI(api_key=openai_key)
|
40 |
-
|
41 |
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
42 |
|
43 |
-
# Initialize
|
44 |
if "messages" not in st.session_state:
|
45 |
st.session_state["messages"] = []
|
46 |
|
47 |
# Display chat history
|
48 |
for message in st.session_state.messages:
|
49 |
-
role
|
|
|
50 |
st.chat_message(role).write(content)
|
51 |
|
52 |
-
#
|
53 |
def save_transcript(messages):
|
54 |
-
with open("chat_logs.txt", "a") as
|
55 |
-
|
56 |
for msg in messages:
|
57 |
-
|
58 |
-
|
59 |
|
60 |
-
#
|
61 |
if prompt := st.chat_input("Type your message here..."):
|
62 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
63 |
st.chat_message("user").write(prompt)
|
64 |
|
65 |
try:
|
66 |
-
#
|
67 |
thread = client.beta.threads.create()
|
68 |
thread_id = thread.id
|
69 |
|
@@ -74,29 +85,33 @@ else:
|
|
74 |
content=prompt
|
75 |
)
|
76 |
|
77 |
-
#
|
78 |
run = client.beta.threads.runs.create(
|
79 |
thread_id=thread_id,
|
80 |
assistant_id=ASSISTANT_ID
|
81 |
)
|
82 |
|
83 |
-
# Wait for completion
|
84 |
while True:
|
85 |
-
run_status = client.beta.threads.runs.retrieve(
|
|
|
|
|
|
|
86 |
if run_status.status == "completed":
|
87 |
break
|
88 |
time.sleep(1)
|
89 |
|
90 |
-
#
|
91 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
92 |
assistant_message = messages.data[0].content[0].text.value
|
93 |
|
94 |
-
# Display and store response
|
95 |
st.chat_message("assistant").write(assistant_message)
|
96 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
97 |
|
98 |
-
# Save
|
99 |
save_transcript(st.session_state.messages)
|
100 |
|
101 |
except Exception as e:
|
102 |
-
st.error(f"
|
|
|
|
4 |
import datetime
|
5 |
import os
|
6 |
|
7 |
+
# Securely get credentials from Hugging Face secrets
|
8 |
+
generated_user = os.getenv("User")
|
9 |
+
generated_password = os.getenv("Password")
|
10 |
openai_key = os.getenv("openai_key")
|
11 |
|
12 |
+
# Streamlit app configuration
|
13 |
st.set_page_config(page_title="Carfind.co.za AI Assistant")
|
|
|
14 |
st.title("Carfind.co.za AI Assistant")
|
15 |
+
st.caption("Chat with Carfind.co.za and find your next car fast")
|
16 |
|
17 |
+
# Initialize authentication state
|
18 |
if "authenticated" not in st.session_state:
|
19 |
st.session_state.authenticated = False
|
20 |
|
21 |
+
# Login screen
|
22 |
if not st.session_state.authenticated:
|
23 |
st.subheader("Login")
|
24 |
username = st.text_input("Username")
|
25 |
password = st.text_input("Password", type="password")
|
26 |
|
27 |
+
# Auto-login once both fields are filled
|
28 |
+
if username and password:
|
29 |
+
if username == generated_user and password == generated_password:
|
30 |
st.session_state.authenticated = True
|
31 |
+
st.success("Login successful! Redirecting...")
|
32 |
+
time.sleep(1)
|
33 |
+
st.rerun()
|
34 |
+
else:
|
35 |
+
st.error("Incorrect username or password. Please try again.")
|
36 |
+
|
37 |
+
# Main chat interface
|
38 |
else:
|
39 |
+
st.divider()
|
40 |
+
|
41 |
+
# Clear chat button positioned near chat window
|
42 |
+
if st.button("🧹 Clear Chat"):
|
43 |
+
st.session_state.messages = []
|
44 |
+
st.success("Chat history cleared.")
|
45 |
|
46 |
+
# Check for OpenAI key
|
47 |
if not openai_key:
|
48 |
+
st.error("OpenAI key not found. Please ensure it is set as a Hugging Face secret.")
|
49 |
else:
|
50 |
client = OpenAI(api_key=openai_key)
|
|
|
51 |
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
52 |
|
53 |
+
# Initialize message history
|
54 |
if "messages" not in st.session_state:
|
55 |
st.session_state["messages"] = []
|
56 |
|
57 |
# Display chat history
|
58 |
for message in st.session_state.messages:
|
59 |
+
role = message["role"]
|
60 |
+
content = message["content"]
|
61 |
st.chat_message(role).write(content)
|
62 |
|
63 |
+
# Save chat transcript (backend only)
|
64 |
def save_transcript(messages):
|
65 |
+
with open("chat_logs.txt", "a") as log:
|
66 |
+
log.write(f"\n--- New Chat ({datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}) ---\n")
|
67 |
for msg in messages:
|
68 |
+
log.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
69 |
+
log.write("--- End Chat ---\n")
|
70 |
|
71 |
+
# Handle user input
|
72 |
if prompt := st.chat_input("Type your message here..."):
|
73 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
74 |
st.chat_message("user").write(prompt)
|
75 |
|
76 |
try:
|
77 |
+
# Start OpenAI chat thread
|
78 |
thread = client.beta.threads.create()
|
79 |
thread_id = thread.id
|
80 |
|
|
|
85 |
content=prompt
|
86 |
)
|
87 |
|
88 |
+
# Trigger assistant response
|
89 |
run = client.beta.threads.runs.create(
|
90 |
thread_id=thread_id,
|
91 |
assistant_id=ASSISTANT_ID
|
92 |
)
|
93 |
|
94 |
+
# Wait for assistant completion
|
95 |
while True:
|
96 |
+
run_status = client.beta.threads.runs.retrieve(
|
97 |
+
thread_id=thread_id,
|
98 |
+
run_id=run.id
|
99 |
+
)
|
100 |
if run_status.status == "completed":
|
101 |
break
|
102 |
time.sleep(1)
|
103 |
|
104 |
+
# Retrieve assistant message
|
105 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
106 |
assistant_message = messages.data[0].content[0].text.value
|
107 |
|
108 |
+
# Display and store the assistant’s response
|
109 |
st.chat_message("assistant").write(assistant_message)
|
110 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
111 |
|
112 |
+
# Save chat log
|
113 |
save_transcript(st.session_state.messages)
|
114 |
|
115 |
except Exception as e:
|
116 |
+
st.error(f"An error occurred: {str(e)}")
|
117 |
+
|