IAMTFRMZA commited on
Commit
6bccdc8
ยท
verified ยท
1 Parent(s): 76c8330

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -5,21 +5,21 @@ import time
5
  import datetime
6
  import os
7
 
8
- # Load secrets from Hugging Face environment
9
  generated_user = os.getenv("User")
10
  generated_password = os.getenv("Password")
11
  openai_key = os.getenv("openai_key")
12
- assistant_id = os.getenv("ASSISTANT_ID") # Now using the Hugging Face secret!
13
 
14
- # Streamlit page setup
15
  st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
16
  st.markdown("<h1 style='text-align: center;'>๐Ÿš— Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
17
  st.caption("Chat with Carfind.co.za and find your next car fast")
18
 
19
- # Light / Dark mode toggle
20
  dark_mode = st.toggle("๐ŸŒ™ Switch to Light Mode", value=True)
21
 
22
- # Add custom CSS for styling bubbles
23
  st.markdown("""
24
  <style>
25
  .stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; }
@@ -28,7 +28,7 @@ st.markdown("""
28
  </style>
29
  """, unsafe_allow_html=True)
30
 
31
- # Login authentication
32
  if "authenticated" not in st.session_state:
33
  st.session_state.authenticated = False
34
 
@@ -46,7 +46,7 @@ if not st.session_state.authenticated:
46
  else:
47
  st.error("Incorrect username or password. Please try again.")
48
 
49
- # Main chat after login
50
  else:
51
  st.divider()
52
 
@@ -56,11 +56,11 @@ else:
56
  # Display chat history
57
  for msg in st.session_state["messages"]:
58
  if msg["role"] == "assistant":
59
- message(msg["content"], is_user=False, avatar="https://www.carfind.co.za/images/Carfind-Icon.svg")
60
  else:
61
  message(msg["content"], is_user=True, avatar_style="adventurer")
62
 
63
- # Chat input and clear button side-by-side
64
  input_col, clear_col = st.columns([8, 1])
65
  with input_col:
66
  user_input = st.text_input("Type your message here...", key="chat_input")
@@ -71,6 +71,7 @@ else:
71
  st.success("Chat cleared.")
72
  st.rerun()
73
 
 
74
  if openai_key and assistant_id:
75
  client = OpenAI(api_key=openai_key)
76
 
@@ -82,12 +83,10 @@ else:
82
  log.write("--- End Chat ---\n")
83
 
84
  if user_input:
85
- # Display user message
86
  st.session_state.messages.append({"role": "user", "content": user_input})
87
  message(user_input, is_user=True, avatar_style="adventurer")
88
 
89
  try:
90
- # Start OpenAI thread and show typing spinner
91
  with st.spinner("Thinking and typing... ๐Ÿ’ญ"):
92
  thread = client.beta.threads.create()
93
  client.beta.threads.messages.create(thread_id=thread.id, role="user", content=user_input)
@@ -100,18 +99,17 @@ else:
100
  break
101
  time.sleep(1)
102
 
103
- # Get assistant reply
104
  response_messages = client.beta.threads.messages.list(thread_id=thread.id)
105
  assistant_reply = response_messages.data[0].content[0].text.value
106
 
107
  st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
108
- message(assistant_reply, is_user=False, avatar="https://www.carfind.co.za/images/Carfind-Icon.svg")
109
 
110
- # Save chat log
111
  save_transcript(st.session_state.messages)
112
 
113
  except Exception as e:
114
  st.error(f"An error occurred: {str(e)}")
115
 
116
  else:
117
- st.error("โš ๏ธ API key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")
 
5
  import datetime
6
  import os
7
 
8
+ # Load Hugging Face secrets
9
  generated_user = os.getenv("User")
10
  generated_password = os.getenv("Password")
11
  openai_key = os.getenv("openai_key")
12
+ assistant_id = os.getenv("ASSISTANT_ID")
13
 
14
+ # Streamlit UI setup
15
  st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
16
  st.markdown("<h1 style='text-align: center;'>๐Ÿš— Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
17
  st.caption("Chat with Carfind.co.za and find your next car fast")
18
 
19
+ # Light/Dark mode toggle
20
  dark_mode = st.toggle("๐ŸŒ™ Switch to Light Mode", value=True)
21
 
22
+ # Custom chat bubble styling
23
  st.markdown("""
24
  <style>
25
  .stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; }
 
28
  </style>
29
  """, unsafe_allow_html=True)
30
 
31
+ # Authentication
32
  if "authenticated" not in st.session_state:
33
  st.session_state.authenticated = False
34
 
 
46
  else:
47
  st.error("Incorrect username or password. Please try again.")
48
 
49
+ # Main chat interface
50
  else:
51
  st.divider()
52
 
 
56
  # Display chat history
57
  for msg in st.session_state["messages"]:
58
  if msg["role"] == "assistant":
59
+ message(f"![carfind-logo](https://www.carfind.co.za/images/Carfind-Icon.svg) **Carfind Assistant:** {msg['content']}", is_user=False)
60
  else:
61
  message(msg["content"], is_user=True, avatar_style="adventurer")
62
 
63
+ # Input + clear button
64
  input_col, clear_col = st.columns([8, 1])
65
  with input_col:
66
  user_input = st.text_input("Type your message here...", key="chat_input")
 
71
  st.success("Chat cleared.")
72
  st.rerun()
73
 
74
+ # Handle chat
75
  if openai_key and assistant_id:
76
  client = OpenAI(api_key=openai_key)
77
 
 
83
  log.write("--- End Chat ---\n")
84
 
85
  if user_input:
 
86
  st.session_state.messages.append({"role": "user", "content": user_input})
87
  message(user_input, is_user=True, avatar_style="adventurer")
88
 
89
  try:
 
90
  with st.spinner("Thinking and typing... ๐Ÿ’ญ"):
91
  thread = client.beta.threads.create()
92
  client.beta.threads.messages.create(thread_id=thread.id, role="user", content=user_input)
 
99
  break
100
  time.sleep(1)
101
 
102
+ # Get the assistant reply
103
  response_messages = client.beta.threads.messages.list(thread_id=thread.id)
104
  assistant_reply = response_messages.data[0].content[0].text.value
105
 
106
  st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
107
+ message(f"![carfind-logo](https://www.carfind.co.za/images/Carfind-Icon.svg) **Carfind Assistant:** {assistant_reply}", is_user=False)
108
 
 
109
  save_transcript(st.session_state.messages)
110
 
111
  except Exception as e:
112
  st.error(f"An error occurred: {str(e)}")
113
 
114
  else:
115
+ st.error("โš ๏ธ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")