Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,10 @@ import streamlit as st
|
|
2 |
from openai import OpenAI
|
3 |
import time
|
4 |
import datetime
|
|
|
5 |
|
6 |
-
#
|
7 |
-
openai_key = "
|
8 |
|
9 |
st.set_page_config(page_title="Carfind.co.za AI Assistant")
|
10 |
|
@@ -32,67 +33,70 @@ else:
|
|
32 |
st.success("Chat history cleared.")
|
33 |
|
34 |
# Initialize OpenAI client
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
2 |
from openai import OpenAI
|
3 |
import time
|
4 |
import datetime
|
5 |
+
import os
|
6 |
|
7 |
+
# Get OpenAI API key from Hugging Face secret environment variable
|
8 |
+
openai_key = os.getenv("openai_key")
|
9 |
|
10 |
st.set_page_config(page_title="Carfind.co.za AI Assistant")
|
11 |
|
|
|
33 |
st.success("Chat history cleared.")
|
34 |
|
35 |
# Initialize OpenAI client
|
36 |
+
if not openai_key:
|
37 |
+
st.error("OpenAI key not found. Please make sure it is set as a secret variable in Hugging Face.")
|
38 |
+
else:
|
39 |
+
client = OpenAI(api_key=openai_key)
|
40 |
+
|
41 |
+
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
42 |
+
|
43 |
+
# Initialize session state for chat history
|
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, content = message["role"], message["content"]
|
50 |
+
st.chat_message(role).write(content)
|
51 |
+
|
52 |
+
# Helper function to save chat transcripts (not displayed, backend only)
|
53 |
+
def save_transcript(messages):
|
54 |
+
with open("chat_logs.txt", "a") as f:
|
55 |
+
f.write(f"\n--- New Chat ({datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}) ---\n")
|
56 |
+
for msg in messages:
|
57 |
+
f.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
58 |
+
f.write("--- End Chat ---\n")
|
59 |
+
|
60 |
+
# Process user input
|
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 |
+
# Create a new thread
|
67 |
+
thread = client.beta.threads.create()
|
68 |
+
thread_id = thread.id
|
69 |
+
|
70 |
+
# Send user message
|
71 |
+
client.beta.threads.messages.create(
|
72 |
+
thread_id=thread_id,
|
73 |
+
role="user",
|
74 |
+
content=prompt
|
75 |
+
)
|
76 |
+
|
77 |
+
# Run assistant
|
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(thread_id=thread_id, run_id=run.id)
|
86 |
+
if run_status.status == "completed":
|
87 |
+
break
|
88 |
+
time.sleep(1)
|
89 |
+
|
90 |
+
# Get assistant response
|
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 conversation transcript silently
|
99 |
+
save_transcript(st.session_state.messages)
|
100 |
+
|
101 |
+
except Exception as e:
|
102 |
+
st.error(f"Error: {str(e)}")
|