IAMTFRMZA commited on
Commit
fa08413
·
verified ·
1 Parent(s): 62c66c2

Update app.py

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