Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -30,8 +30,6 @@ if st.session_state.theme == "light":
|
|
30 |
"""
|
31 |
<style>
|
32 |
body { background-color: #f0f0f0; color: #000; }
|
33 |
-
.chat-bubble-user { background-color: #d1e7dd; color: #000; }
|
34 |
-
.chat-bubble-assistant { background-color: #f8d7da; color: #000; }
|
35 |
</style>
|
36 |
""",
|
37 |
unsafe_allow_html=True
|
@@ -41,8 +39,6 @@ else:
|
|
41 |
"""
|
42 |
<style>
|
43 |
body { background-color: #0e0e0e; color: #fff; }
|
44 |
-
.chat-bubble-user { background-color: #004085; color: #fff; }
|
45 |
-
.chat-bubble-assistant { background-color: #155724; color: #fff; }
|
46 |
</style>
|
47 |
""",
|
48 |
unsafe_allow_html=True
|
@@ -74,10 +70,17 @@ else:
|
|
74 |
|
75 |
st.divider()
|
76 |
|
77 |
-
# Display chat history with
|
78 |
for i, msg in enumerate(st.session_state["messages"]):
|
79 |
-
|
80 |
-
message(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
col_input, col_clear = st.columns([10, 1])
|
83 |
|
@@ -89,55 +92,59 @@ else:
|
|
89 |
st.session_state.messages = []
|
90 |
st.rerun()
|
91 |
|
92 |
-
# Handle OpenAI assistant
|
93 |
if user_input and openai_key:
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
log.write(f"{
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
content=user_input
|
115 |
-
)
|
116 |
-
|
117 |
-
run = client.beta.threads.runs.create(
|
118 |
-
thread_id=thread_id,
|
119 |
-
assistant_id=ASSISTANT_ID
|
120 |
-
)
|
121 |
-
|
122 |
-
while True:
|
123 |
-
run_status = client.beta.threads.runs.retrieve(
|
124 |
thread_id=thread_id,
|
125 |
-
|
|
|
126 |
)
|
127 |
-
if run_status.status == "completed":
|
128 |
-
break
|
129 |
-
time.sleep(1)
|
130 |
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
-
|
135 |
-
|
|
|
|
|
136 |
|
137 |
-
|
138 |
|
139 |
-
|
140 |
-
|
141 |
|
142 |
elif not openai_key:
|
143 |
-
st.error("OpenAI key not found. Please ensure it is set as a Hugging Face secret.")
|
|
|
30 |
"""
|
31 |
<style>
|
32 |
body { background-color: #f0f0f0; color: #000; }
|
|
|
|
|
33 |
</style>
|
34 |
""",
|
35 |
unsafe_allow_html=True
|
|
|
39 |
"""
|
40 |
<style>
|
41 |
body { background-color: #0e0e0e; color: #fff; }
|
|
|
|
|
42 |
</style>
|
43 |
""",
|
44 |
unsafe_allow_html=True
|
|
|
70 |
|
71 |
st.divider()
|
72 |
|
73 |
+
# Display chat history with avatars
|
74 |
for i, msg in enumerate(st.session_state["messages"]):
|
75 |
+
avatar_url = "https://www.carfind.co.za/images/Carfind-Icon.svg" if msg["role"] == "assistant" else None
|
76 |
+
message(
|
77 |
+
msg["content"],
|
78 |
+
is_user=(msg["role"] == "user"),
|
79 |
+
key=str(i),
|
80 |
+
avatar_style="adventurer" if msg["role"] == "user" else None,
|
81 |
+
seed="user-seed" if msg["role"] == "user" else None,
|
82 |
+
avatar_url=avatar_url
|
83 |
+
)
|
84 |
|
85 |
col_input, col_clear = st.columns([10, 1])
|
86 |
|
|
|
92 |
st.session_state.messages = []
|
93 |
st.rerun()
|
94 |
|
95 |
+
# Handle OpenAI assistant (with loop protection)
|
96 |
if user_input and openai_key:
|
97 |
+
# Avoid duplicate messages triggering loops
|
98 |
+
if len(st.session_state.messages) == 0 or user_input != st.session_state.messages[-1]["content"]:
|
99 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
100 |
+
|
101 |
+
with st.spinner("Thinking and typing... 💬"):
|
102 |
+
client = OpenAI(api_key=openai_key)
|
103 |
+
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
104 |
+
|
105 |
+
def save_transcript(messages):
|
106 |
+
with open("chat_logs.txt", "a") as log:
|
107 |
+
log.write(f"\n--- New Chat ({datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}) ---\n")
|
108 |
+
for msg in messages:
|
109 |
+
log.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
110 |
+
log.write("--- End Chat ---\n")
|
111 |
+
|
112 |
+
try:
|
113 |
+
thread = client.beta.threads.create()
|
114 |
+
thread_id = thread.id
|
115 |
+
|
116 |
+
client.beta.threads.messages.create(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
thread_id=thread_id,
|
118 |
+
role="user",
|
119 |
+
content=user_input
|
120 |
)
|
|
|
|
|
|
|
121 |
|
122 |
+
run = client.beta.threads.runs.create(
|
123 |
+
thread_id=thread_id,
|
124 |
+
assistant_id=ASSISTANT_ID
|
125 |
+
)
|
126 |
+
|
127 |
+
while True:
|
128 |
+
run_status = client.beta.threads.runs.retrieve(
|
129 |
+
thread_id=thread_id,
|
130 |
+
run_id=run.id
|
131 |
+
)
|
132 |
+
if run_status.status == "completed":
|
133 |
+
break
|
134 |
+
time.sleep(1)
|
135 |
+
|
136 |
+
messages_response = client.beta.threads.messages.list(thread_id=thread_id)
|
137 |
+
assistant_reply = messages_response.data[0].content[0].text.value
|
138 |
|
139 |
+
# Prevent infinite repetition by ignoring if assistant echoes input
|
140 |
+
if assistant_reply.strip().lower() != user_input.strip().lower():
|
141 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|
142 |
+
save_transcript(st.session_state.messages)
|
143 |
|
144 |
+
st.rerun()
|
145 |
|
146 |
+
except Exception as e:
|
147 |
+
st.error(f"An error occurred: {str(e)}")
|
148 |
|
149 |
elif not openai_key:
|
150 |
+
st.error("OpenAI key not found. Please ensure it is set as a Hugging Face secret.")
|