IAMTFRMZA commited on
Commit
4867300
·
verified ·
1 Parent(s): 4a5ace4

Update app.py

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