IAMTFRMZA commited on
Commit
4ef479c
ยท
verified ยท
1 Parent(s): 557a72a

Update app,py

Browse files
Files changed (1) hide show
  1. app.py +41 -45
app.py CHANGED
@@ -3,18 +3,33 @@ from openai import OpenAI
3
  import time
4
  import os
5
  import pandas as pd
6
- import datetime
7
- from huggingface_hub import HfApi, upload_file
8
 
9
- # Environment variables
10
  generated_user = os.getenv("User")
11
  generated_password = os.getenv("Password")
12
  openai_key = os.getenv("openai_key")
13
  assistant_id = os.getenv("ASSISTANT_ID")
14
- hf_token = os.getenv("HF_TOKEN")
15
 
 
16
  transcript_file = "transcripts.xlsx"
 
 
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
19
  st.markdown("<h1 style='text-align: center;'>๐Ÿš— Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
20
  st.caption("Chat with Carfind.co.za and find your next car fast")
@@ -27,37 +42,6 @@ st.markdown("""
27
  </style>
28
  """, unsafe_allow_html=True)
29
 
30
- # Save chat to Excel and push to Hugging Face repo
31
- def append_to_transcript(thread_id, role, message):
32
- timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
33
- new_entry = {
34
- "Timestamp": timestamp,
35
- "Thread_ID": thread_id,
36
- "Role": role,
37
- "Message": message
38
- }
39
- try:
40
- if os.path.exists(transcript_file):
41
- df = pd.read_excel(transcript_file)
42
- df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
43
- else:
44
- df = pd.DataFrame([new_entry])
45
- df.to_excel(transcript_file, index=False)
46
-
47
- # Upload updated transcripts.xlsx to Hugging Face
48
- api = HfApi(token=hf_token)
49
- upload_file(
50
- path_or_fileobj=transcript_file,
51
- path_in_repo="transcripts.xlsx",
52
- repo_id="IAMTFRMZA/cfaiassistant",
53
- repo_type="space",
54
- token=hf_token,
55
- commit_message=f"Transcript update {timestamp}"
56
- )
57
- except Exception as e:
58
- st.error(f"Transcript logging failed: {str(e)}")
59
-
60
- # Authentication
61
  if "authenticated" not in st.session_state:
62
  st.session_state.authenticated = False
63
 
@@ -65,6 +49,7 @@ if not st.session_state.authenticated:
65
  st.subheader("๐Ÿ”‘ Login")
66
  username = st.text_input("Username")
67
  password = st.text_input("Password", type="password")
 
68
  if username and password:
69
  if username == generated_user and password == generated_password:
70
  st.session_state.authenticated = True
@@ -72,8 +57,7 @@ if not st.session_state.authenticated:
72
  time.sleep(1)
73
  st.rerun()
74
  else:
75
- st.error("Incorrect username or password.")
76
-
77
  else:
78
  st.divider()
79
 
@@ -90,15 +74,16 @@ else:
90
  st.success("Chat cleared.")
91
  st.rerun()
92
 
93
- if openai_key and assistant_id and hf_token:
94
  client = OpenAI(api_key=openai_key)
95
 
96
  if user_input:
 
97
  if not st.session_state["thread_id"]:
98
  thread = client.beta.threads.create()
99
  st.session_state["thread_id"] = thread.id
100
 
101
- # Add user message and log
102
  client.beta.threads.messages.create(
103
  thread_id=st.session_state["thread_id"], role="user", content=user_input
104
  )
@@ -117,19 +102,22 @@ else:
117
  break
118
  time.sleep(1)
119
 
 
120
  messages_response = client.beta.threads.messages.list(
121
  thread_id=st.session_state["thread_id"]
122
  )
123
 
124
  assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
125
 
126
- # Display chat conversation
127
- for msg in reversed(messages_response.data):
 
128
  if msg.role == "user":
129
  st.markdown(
130
  f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
131
  f"๐Ÿ‘ค <strong>You:</strong> {msg.content[0].text.value}"
132
- f"</div>", unsafe_allow_html=True
 
133
  )
134
  else:
135
  response_text = msg.content[0].text.value
@@ -137,10 +125,18 @@ else:
137
  st.markdown(
138
  f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
139
  f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {response_text}"
140
- f"</div>", unsafe_allow_html=True
 
141
  )
 
 
 
 
 
 
 
 
142
  except Exception as e:
143
  st.error(f"An error occurred: {str(e)}")
144
  else:
145
- st.error("โš ๏ธ Missing OpenAI key, Assistant ID or HF token. Please check Hugging Face secrets.")
146
-
 
3
  import time
4
  import os
5
  import pandas as pd
6
+ from datetime import datetime
 
7
 
8
+ # Get environment variables
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
+ # Load or create transcript file
15
  transcript_file = "transcripts.xlsx"
16
+ if not os.path.exists(transcript_file):
17
+ df = pd.DataFrame(columns=["timestamp", "thread_id", "role", "message"])
18
+ df.to_excel(transcript_file, index=False)
19
 
20
+ # Function to append to transcript
21
+ def append_to_transcript(thread_id, role, message):
22
+ df_existing = pd.read_excel(transcript_file)
23
+ new_entry = pd.DataFrame({
24
+ "timestamp": [datetime.now().strftime("%Y-%m-%d %H:%M:%S")],
25
+ "thread_id": [thread_id],
26
+ "role": [role],
27
+ "message": [message]
28
+ })
29
+ updated_df = pd.concat([df_existing, new_entry], ignore_index=True)
30
+ updated_df.to_excel(transcript_file, index=False)
31
+
32
+ # Streamlit UI setup
33
  st.set_page_config(page_title="Carfind.co.za AI Assistant", layout="wide")
34
  st.markdown("<h1 style='text-align: center;'>๐Ÿš— Carfind.co.za AI Assistant</h1>", unsafe_allow_html=True)
35
  st.caption("Chat with Carfind.co.za and find your next car fast")
 
42
  </style>
43
  """, unsafe_allow_html=True)
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  if "authenticated" not in st.session_state:
46
  st.session_state.authenticated = False
47
 
 
49
  st.subheader("๐Ÿ”‘ Login")
50
  username = st.text_input("Username")
51
  password = st.text_input("Password", type="password")
52
+
53
  if username and password:
54
  if username == generated_user and password == generated_password:
55
  st.session_state.authenticated = True
 
57
  time.sleep(1)
58
  st.rerun()
59
  else:
60
+ st.error("Incorrect username or password. Please try again.")
 
61
  else:
62
  st.divider()
63
 
 
74
  st.success("Chat cleared.")
75
  st.rerun()
76
 
77
+ if openai_key and assistant_id:
78
  client = OpenAI(api_key=openai_key)
79
 
80
  if user_input:
81
+ # Create thread if not exists
82
  if not st.session_state["thread_id"]:
83
  thread = client.beta.threads.create()
84
  st.session_state["thread_id"] = thread.id
85
 
86
+ # Add user message
87
  client.beta.threads.messages.create(
88
  thread_id=st.session_state["thread_id"], role="user", content=user_input
89
  )
 
102
  break
103
  time.sleep(1)
104
 
105
+ # Get full conversation
106
  messages_response = client.beta.threads.messages.list(
107
  thread_id=st.session_state["thread_id"]
108
  )
109
 
110
  assistant_icon_html = "<img src='https://www.carfind.co.za/images/Carfind-Icon.svg' width='22' style='vertical-align:middle;'/>"
111
 
112
+ # Display newest-first
113
+ messages_sorted = sorted(messages_response.data, key=lambda x: x.created_at, reverse=True)
114
+ for msg in messages_sorted:
115
  if msg.role == "user":
116
  st.markdown(
117
  f"<div style='background-color:#f0f0f0; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
118
  f"๐Ÿ‘ค <strong>You:</strong> {msg.content[0].text.value}"
119
+ f"</div>",
120
+ unsafe_allow_html=True
121
  )
122
  else:
123
  response_text = msg.content[0].text.value
 
125
  st.markdown(
126
  f"<div style='background-color:#D6E9FE; color:#000000; padding:10px; border-radius:8px; margin:5px 0;'>"
127
  f"{assistant_icon_html} <strong>Carfind Assistant:</strong> {response_text}"
128
+ f"</div>",
129
+ unsafe_allow_html=True
130
  )
131
+
132
+ # Scroll to top automatically
133
+ st.markdown("""
134
+ <script>
135
+ window.scrollTo(0, 0);
136
+ </script>
137
+ """, unsafe_allow_html=True)
138
+
139
  except Exception as e:
140
  st.error(f"An error occurred: {str(e)}")
141
  else:
142
+ st.error("โš ๏ธ OpenAI key or Assistant ID not found. Please ensure both are set as Hugging Face secrets.")