Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -38,28 +38,30 @@ if not st.session_state.authenticated:
|
|
38 |
else:
|
39 |
st.divider()
|
40 |
|
41 |
-
#
|
42 |
-
if
|
43 |
-
st.session_state
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Check for OpenAI key
|
47 |
-
if
|
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:
|
@@ -68,10 +70,10 @@ else:
|
|
68 |
log.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
69 |
log.write("--- End Chat ---\n")
|
70 |
|
71 |
-
# Handle user input
|
72 |
-
if
|
73 |
-
st.session_state.messages.append({"role": "user", "content":
|
74 |
-
st.chat_message("user").write(
|
75 |
|
76 |
try:
|
77 |
# Start OpenAI chat thread
|
@@ -82,7 +84,7 @@ else:
|
|
82 |
client.beta.threads.messages.create(
|
83 |
thread_id=thread_id,
|
84 |
role="user",
|
85 |
-
content=
|
86 |
)
|
87 |
|
88 |
# Trigger assistant response
|
@@ -114,4 +116,6 @@ else:
|
|
114 |
|
115 |
except Exception as e:
|
116 |
st.error(f"An error occurred: {str(e)}")
|
|
|
|
|
117 |
|
|
|
38 |
else:
|
39 |
st.divider()
|
40 |
|
41 |
+
# Display chat history
|
42 |
+
if "messages" not in st.session_state:
|
43 |
+
st.session_state["messages"] = []
|
44 |
+
|
45 |
+
for message in st.session_state.messages:
|
46 |
+
role = message["role"]
|
47 |
+
content = message["content"]
|
48 |
+
st.chat_message(role).write(content)
|
49 |
+
|
50 |
+
# Add Clear Chat button next to input field
|
51 |
+
clear_col, input_col = st.columns([1, 8])
|
52 |
+
with clear_col:
|
53 |
+
if st.button("🧹 Clear Chat"):
|
54 |
+
st.session_state.messages = []
|
55 |
+
st.success("Chat history cleared.")
|
56 |
+
|
57 |
+
with input_col:
|
58 |
+
user_input = st.chat_input("Type your message here...")
|
59 |
|
60 |
# Check for OpenAI key
|
61 |
+
if openai_key:
|
|
|
|
|
62 |
client = OpenAI(api_key=openai_key)
|
63 |
ASSISTANT_ID = "asst_5gQR21fOsmHil11FGBzEArA7"
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
# Save chat transcript (backend only)
|
66 |
def save_transcript(messages):
|
67 |
with open("chat_logs.txt", "a") as log:
|
|
|
70 |
log.write(f"{msg['role'].capitalize()}: {msg['content']}\n")
|
71 |
log.write("--- End Chat ---\n")
|
72 |
|
73 |
+
# Handle user input and keep sticky experience
|
74 |
+
if user_input:
|
75 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
76 |
+
st.chat_message("user").write(user_input)
|
77 |
|
78 |
try:
|
79 |
# Start OpenAI chat thread
|
|
|
84 |
client.beta.threads.messages.create(
|
85 |
thread_id=thread_id,
|
86 |
role="user",
|
87 |
+
content=user_input
|
88 |
)
|
89 |
|
90 |
# Trigger assistant response
|
|
|
116 |
|
117 |
except Exception as e:
|
118 |
st.error(f"An error occurred: {str(e)}")
|
119 |
+
else:
|
120 |
+
st.error("OpenAI key not found. Please ensure it is set as a Hugging Face secret.")
|
121 |
|