Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,16 @@ import time
|
|
4 |
import os
|
5 |
import pandas as pd
|
6 |
import datetime
|
|
|
7 |
|
|
|
8 |
generated_user = os.getenv("User")
|
9 |
generated_password = os.getenv("Password")
|
10 |
openai_key = os.getenv("openai_key")
|
11 |
assistant_id = os.getenv("ASSISTANT_ID")
|
|
|
|
|
|
|
12 |
|
13 |
st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
|
14 |
st.markdown("<h1 style='text-align: center;'>π Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
|
@@ -22,9 +27,7 @@ st.markdown("""
|
|
22 |
</style>
|
23 |
""", unsafe_allow_html=True)
|
24 |
|
25 |
-
#
|
26 |
-
transcript_file = "transcripts.xlsx"
|
27 |
-
|
28 |
def append_to_transcript(thread_id, role, message):
|
29 |
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
30 |
new_entry = {
|
@@ -33,18 +36,28 @@ def append_to_transcript(thread_id, role, message):
|
|
33 |
"Role": role,
|
34 |
"Message": message
|
35 |
}
|
36 |
-
|
37 |
try:
|
38 |
if os.path.exists(transcript_file):
|
39 |
df = pd.read_excel(transcript_file)
|
40 |
df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
|
41 |
else:
|
42 |
df = pd.DataFrame([new_entry])
|
43 |
-
|
44 |
df.to_excel(transcript_file, index=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
except Exception as e:
|
46 |
-
st.error(f"
|
47 |
|
|
|
48 |
if "authenticated" not in st.session_state:
|
49 |
st.session_state.authenticated = False
|
50 |
|
@@ -52,7 +65,6 @@ if not st.session_state.authenticated:
|
|
52 |
st.subheader("π Login")
|
53 |
username = st.text_input("Username")
|
54 |
password = st.text_input("Password", type="password")
|
55 |
-
|
56 |
if username and password:
|
57 |
if username == generated_user and password == generated_password:
|
58 |
st.session_state.authenticated = True
|
@@ -60,7 +72,7 @@ if not st.session_state.authenticated:
|
|
60 |
time.sleep(1)
|
61 |
st.rerun()
|
62 |
else:
|
63 |
-
st.error("Incorrect username or password.
|
64 |
|
65 |
else:
|
66 |
st.divider()
|
@@ -78,7 +90,7 @@ else:
|
|
78 |
st.success("Chat cleared.")
|
79 |
st.rerun()
|
80 |
|
81 |
-
if openai_key and assistant_id:
|
82 |
client = OpenAI(api_key=openai_key)
|
83 |
|
84 |
if user_input:
|
@@ -86,17 +98,17 @@ else:
|
|
86 |
thread = client.beta.threads.create()
|
87 |
st.session_state["thread_id"] = thread.id
|
88 |
|
|
|
89 |
client.beta.threads.messages.create(
|
90 |
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
91 |
)
|
92 |
-
append_to_transcript(st.session_state["thread_id"], "
|
93 |
|
94 |
try:
|
95 |
with st.spinner("Thinking and typing... π"):
|
96 |
run = client.beta.threads.runs.create(
|
97 |
thread_id=st.session_state["thread_id"], assistant_id=assistant_id
|
98 |
)
|
99 |
-
|
100 |
while True:
|
101 |
run_status = client.beta.threads.runs.retrieve(
|
102 |
thread_id=st.session_state["thread_id"], run_id=run.id
|
@@ -111,26 +123,24 @@ else:
|
|
111 |
|
112 |
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
|
113 |
|
|
|
114 |
for msg in reversed(messages_response.data):
|
115 |
-
content = msg.content[0].text.value
|
116 |
if msg.role == "user":
|
117 |
st.markdown(
|
118 |
f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
119 |
-
f"π€ <strong>You:</strong> {content}"
|
120 |
-
f"</div>",
|
121 |
-
unsafe_allow_html=True
|
122 |
)
|
123 |
else:
|
|
|
|
|
124 |
st.markdown(
|
125 |
f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
126 |
-
f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {
|
127 |
-
f"</div>",
|
128 |
-
unsafe_allow_html=True
|
129 |
)
|
130 |
-
append_to_transcript(st.session_state["thread_id"], "Assistant", content)
|
131 |
-
|
132 |
except Exception as e:
|
133 |
st.error(f"An error occurred: {str(e)}")
|
134 |
-
|
135 |
else:
|
136 |
-
st.error("β οΈ OpenAI key
|
|
|
|
4 |
import os
|
5 |
import pandas as pd
|
6 |
import datetime
|
7 |
+
from huggingface_hub import HfApi, upload_file
|
8 |
|
9 |
+
# Environment variables
|
10 |
generated_user = os.getenv("User")
|
11 |
generated_password = os.getenv("Password")
|
12 |
openai_key = os.getenv("openai_key")
|
13 |
assistant_id = os.getenv("ASSISTANT_ID")
|
14 |
+
hf_token = os.getenv("HF_TOKEN")
|
15 |
+
|
16 |
+
transcript_file = "transcripts.xlsx"
|
17 |
|
18 |
st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
|
19 |
st.markdown("<h1 style='text-align: center;'>π Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
|
|
|
27 |
</style>
|
28 |
""", unsafe_allow_html=True)
|
29 |
|
30 |
+
# Save chat to Excel and push to Hugging Face repo
|
|
|
|
|
31 |
def append_to_transcript(thread_id, role, message):
|
32 |
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
33 |
new_entry = {
|
|
|
36 |
"Role": role,
|
37 |
"Message": message
|
38 |
}
|
|
|
39 |
try:
|
40 |
if os.path.exists(transcript_file):
|
41 |
df = pd.read_excel(transcript_file)
|
42 |
df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
|
43 |
else:
|
44 |
df = pd.DataFrame([new_entry])
|
|
|
45 |
df.to_excel(transcript_file, index=False)
|
46 |
+
|
47 |
+
# Upload updated transcripts.xlsx to Hugging Face
|
48 |
+
api = HfApi(token=hf_token)
|
49 |
+
upload_file(
|
50 |
+
path_or_fileobj=transcript_file,
|
51 |
+
path_in_repo="transcripts.xlsx",
|
52 |
+
repo_id="IAMTFRMZA/cfaiassistant",
|
53 |
+
repo_type="space",
|
54 |
+
token=hf_token,
|
55 |
+
commit_message=f"Transcript update {timestamp}"
|
56 |
+
)
|
57 |
except Exception as e:
|
58 |
+
st.error(f"Transcript logging failed: {str(e)}")
|
59 |
|
60 |
+
# Authentication
|
61 |
if "authenticated" not in st.session_state:
|
62 |
st.session_state.authenticated = False
|
63 |
|
|
|
65 |
st.subheader("π Login")
|
66 |
username = st.text_input("Username")
|
67 |
password = st.text_input("Password", type="password")
|
|
|
68 |
if username and password:
|
69 |
if username == generated_user and password == generated_password:
|
70 |
st.session_state.authenticated = True
|
|
|
72 |
time.sleep(1)
|
73 |
st.rerun()
|
74 |
else:
|
75 |
+
st.error("Incorrect username or password.")
|
76 |
|
77 |
else:
|
78 |
st.divider()
|
|
|
90 |
st.success("Chat cleared.")
|
91 |
st.rerun()
|
92 |
|
93 |
+
if openai_key and assistant_id and hf_token:
|
94 |
client = OpenAI(api_key=openai_key)
|
95 |
|
96 |
if user_input:
|
|
|
98 |
thread = client.beta.threads.create()
|
99 |
st.session_state["thread_id"] = thread.id
|
100 |
|
101 |
+
# Add user message and log
|
102 |
client.beta.threads.messages.create(
|
103 |
thread_id=st.session_state["thread_id"], role="user", content=user_input
|
104 |
)
|
105 |
+
append_to_transcript(st.session_state["thread_id"], "user", user_input)
|
106 |
|
107 |
try:
|
108 |
with st.spinner("Thinking and typing... π"):
|
109 |
run = client.beta.threads.runs.create(
|
110 |
thread_id=st.session_state["thread_id"], assistant_id=assistant_id
|
111 |
)
|
|
|
112 |
while True:
|
113 |
run_status = client.beta.threads.runs.retrieve(
|
114 |
thread_id=st.session_state["thread_id"], run_id=run.id
|
|
|
123 |
|
124 |
assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
|
125 |
|
126 |
+
# Display chat conversation
|
127 |
for msg in reversed(messages_response.data):
|
|
|
128 |
if msg.role == "user":
|
129 |
st.markdown(
|
130 |
f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
131 |
+
f"π€ <strong>You:</strong> {msg.content[0].text.value}"
|
132 |
+
f"</div>", unsafe_allow_html=True
|
|
|
133 |
)
|
134 |
else:
|
135 |
+
response_text = msg.content[0].text.value
|
136 |
+
append_to_transcript(st.session_state["thread_id"], "assistant", response_text)
|
137 |
st.markdown(
|
138 |
f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
|
139 |
+
f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {response_text}"
|
140 |
+
f"</div>", unsafe_allow_html=True
|
|
|
141 |
)
|
|
|
|
|
142 |
except Exception as e:
|
143 |
st.error(f"An error occurred: {str(e)}")
|
|
|
144 |
else:
|
145 |
+
st.error("β οΈ Missing OpenAI key, Assistant ID or HF token. Please check Hugging Face secrets.")
|
146 |
+
|