IAMTFRMZA commited on
Commit
17e4a36
·
verified ·
1 Parent(s): e865a39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -64
app.py CHANGED
@@ -43,8 +43,8 @@ st.divider()
43
  if "thread_id" not in st.session_state:
44
  st.session_state["thread_id"] = None
45
 
46
- if "chat_input" not in st.session_state:
47
- st.session_state["chat_input"] = ""
48
 
49
  input_col, clear_col = st.columns([8, 1])
50
  with input_col:
@@ -56,72 +56,71 @@ with clear_col:
56
  st.success("Chat cleared.")
57
  st.rerun()
58
 
59
- if openai_key and assistant_id:
60
- client = OpenAI(api_key=openai_key)
61
-
62
- if user_input:
63
- # Clear input after submitting
64
- st.session_state["chat_input"] = ""
65
 
66
- # Create thread if not exists
67
- if not st.session_state["thread_id"]:
68
- thread = client.beta.threads.create()
69
- st.session_state["thread_id"] = thread.id
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- # Add user message
72
- client.beta.threads.messages.create(
73
- thread_id=st.session_state["thread_id"], role="user", content=user_input
74
  )
75
- append_to_transcript(st.session_state["thread_id"], "user", user_input)
76
 
77
- try:
78
- with st.spinner("Thinking and typing... 💭"):
79
- run = client.beta.threads.runs.create(
80
- thread_id=st.session_state["thread_id"], assistant_id=assistant_id
 
 
 
 
 
 
81
  )
82
- while True:
83
- run_status = client.beta.threads.runs.retrieve(
84
- thread_id=st.session_state["thread_id"], run_id=run.id
85
- )
86
- if run_status.status == "completed":
87
- break
88
- time.sleep(1)
89
-
90
- # Get full conversation
91
- messages_response = client.beta.threads.messages.list(
92
- thread_id=st.session_state["thread_id"]
93
- )
 
 
 
 
 
 
94
 
95
- assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
96
-
97
- # Display newest-first
98
- messages_sorted = sorted(messages_response.data, key=lambda x: x.created_at, reverse=True)
99
- for msg in messages_sorted:
100
- if msg.role == "user":
101
- st.markdown(
102
- f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
103
- f"👤 <strong>You:</strong> {msg.content[0].text.value}"
104
- f"</div>",
105
- unsafe_allow_html=True
106
- )
107
- else:
108
- response_text = msg.content[0].text.value
109
- append_to_transcript(st.session_state["thread_id"], "assistant", response_text)
110
- st.markdown(
111
- f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
112
- f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {response_text}"
113
- f"</div>",
114
- unsafe_allow_html=True
115
- )
116
-
117
- # Scroll to top automatically
118
- st.markdown("""
119
- <script>
120
- window.scrollTo(0, 0);
121
- </script>
122
- """, unsafe_allow_html=True)
123
-
124
- except Exception as e:
125
- st.error(f"An error occurred: {str(e)}")
126
- else:
127
  st.error("⚠️ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")
 
43
  if "thread_id" not in st.session_state:
44
  st.session_state["thread_id"] = None
45
 
46
+ if "pending_user_message" not in st.session_state:
47
+ st.session_state["pending_user_message"] = None
48
 
49
  input_col, clear_col = st.columns([8, 1])
50
  with input_col:
 
56
  st.success("Chat cleared.")
57
  st.rerun()
58
 
59
+ if user_input:
60
+ st.session_state["pending_user_message"] = user_input
61
+ st.session_state["chat_input"] = ""
62
+ st.experimental_rerun()
 
 
63
 
64
+ if openai_key and assistant_id and st.session_state["pending_user_message"]:
65
+ client = OpenAI(api_key=openai_key)
66
+ user_message = st.session_state["pending_user_message"]
67
+ st.session_state["pending_user_message"] = None
68
+
69
+ if not st.session_state["thread_id"]:
70
+ thread = client.beta.threads.create()
71
+ st.session_state["thread_id"] = thread.id
72
+
73
+ client.beta.threads.messages.create(
74
+ thread_id=st.session_state["thread_id"], role="user", content=user_message
75
+ )
76
+ append_to_transcript(st.session_state["thread_id"], "user", user_message)
77
+
78
+ try:
79
+ with st.spinner("Thinking and typing... 💭"):
80
+ run = client.beta.threads.runs.create(
81
+ thread_id=st.session_state["thread_id"], assistant_id=assistant_id
82
+ )
83
+ while True:
84
+ run_status = client.beta.threads.runs.retrieve(
85
+ thread_id=st.session_state["thread_id"], run_id=run.id
86
+ )
87
+ if run_status.status == "completed":
88
+ break
89
+ time.sleep(1)
90
 
91
+ messages_response = client.beta.threads.messages.list(
92
+ thread_id=st.session_state["thread_id"]
 
93
  )
 
94
 
95
+ assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
96
+
97
+ messages_sorted = sorted(messages_response.data, key=lambda x: x.created_at, reverse=True)
98
+ for msg in messages_sorted:
99
+ if msg.role == "user":
100
+ st.markdown(
101
+ f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
102
+ f"👤 <strong>You:</strong> {msg.content[0].text.value}"
103
+ f"</div>",
104
+ unsafe_allow_html=True
105
  )
106
+ else:
107
+ response_text = msg.content[0].text.value
108
+ append_to_transcript(st.session_state["thread_id"], "assistant", response_text)
109
+ st.markdown(
110
+ f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
111
+ f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {response_text}"
112
+ f"</div>",
113
+ unsafe_allow_html=True
114
+ )
115
+
116
+ st.markdown("""
117
+ <script>
118
+ window.scrollTo(0, 0);
119
+ </script>
120
+ """, unsafe_allow_html=True)
121
+
122
+ except Exception as e:
123
+ st.error(f"An error occurred: {str(e)}")
124
 
125
+ elif not openai_key or not assistant_id:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  st.error("⚠️ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")