Update app.py
Browse files
app.py
CHANGED
@@ -6,13 +6,13 @@ from datetime import datetime
|
|
6 |
from openai import OpenAI
|
7 |
|
8 |
# Streamlit Page Config
|
9 |
-
st.set_page_config(page_title="
|
10 |
|
11 |
# Authentication
|
12 |
AUTHORIZED_USERS = {
|
13 |
"[email protected]": "Pass.123",
|
14 |
"[email protected]": "Pass.123",
|
15 |
-
"test@test
|
16 |
}
|
17 |
|
18 |
if "authenticated" not in st.session_state:
|
@@ -33,22 +33,22 @@ if not st.session_state["authenticated"]:
|
|
33 |
st.button("Login", on_click=login)
|
34 |
st.stop()
|
35 |
|
36 |
-
#
|
37 |
-
st.title("
|
38 |
-
st.caption("
|
39 |
|
40 |
-
# Load API Key
|
41 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
42 |
-
|
43 |
-
ASSISTANT_TECHNICAL_ID = os.getenv("ASSISTANT_TECHNICAL_ID")
|
44 |
|
45 |
-
if not OPENAI_API_KEY
|
46 |
-
st.error("Missing
|
47 |
st.stop()
|
48 |
|
49 |
-
# Tabs
|
50 |
-
|
51 |
|
|
|
52 |
FLAGGED_RESPONSES_DIR = "flagged_responses"
|
53 |
os.makedirs(FLAGGED_RESPONSES_DIR, exist_ok=True)
|
54 |
|
@@ -60,43 +60,38 @@ def save_flagged_response(user_query, ai_response):
|
|
60 |
"response": ai_response,
|
61 |
"timestamp": datetime.now().isoformat()
|
62 |
}
|
63 |
-
|
64 |
with open(filename, "w") as file:
|
65 |
json.dump(flagged_data, file, indent=4)
|
66 |
-
|
67 |
st.success(f"Response flagged and saved as {filename}.")
|
68 |
|
69 |
-
|
70 |
-
def contract_chat_section(tab, assistant_id, session_key, input_key):
|
71 |
with tab:
|
72 |
-
st.subheader("Chat")
|
73 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
74 |
|
75 |
if session_key not in st.session_state:
|
76 |
st.session_state[session_key] = []
|
77 |
|
78 |
-
if st.button("Clear Chat", key=
|
79 |
st.session_state[session_key] = []
|
80 |
st.rerun()
|
81 |
|
82 |
-
for idx,
|
83 |
-
role, content =
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
else:
|
91 |
-
st.chat_message(role).write(content)
|
92 |
-
|
93 |
-
if prompt := st.chat_input("Enter your message:", key=input_key):
|
94 |
st.session_state[session_key].append({"role": "user", "content": prompt})
|
95 |
st.chat_message("user").write(prompt)
|
96 |
|
97 |
try:
|
98 |
thread = client.beta.threads.create()
|
99 |
thread_id = thread.id
|
|
|
100 |
client.beta.threads.messages.create(
|
101 |
thread_id=thread_id,
|
102 |
role="user",
|
@@ -105,39 +100,37 @@ def contract_chat_section(tab, assistant_id, session_key, input_key):
|
|
105 |
|
106 |
run = client.beta.threads.runs.create(
|
107 |
thread_id=thread_id,
|
108 |
-
assistant_id=
|
109 |
)
|
110 |
|
111 |
while True:
|
112 |
-
|
113 |
-
if
|
114 |
break
|
115 |
time.sleep(1)
|
116 |
|
117 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
118 |
-
|
119 |
-
st.session_state[session_key].append({"role": "assistant", "content":
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
except Exception as e:
|
125 |
-
st.error(f"Error: {str(e)}")
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
flagged_responses = file.read()
|
141 |
-
st.download_button("Download Selected Flagged Responses", data=flagged_responses, file_name=selected_file, mime="application/json")
|
142 |
else:
|
143 |
-
st.info("No flagged responses
|
|
|
6 |
from openai import OpenAI
|
7 |
|
8 |
# Streamlit Page Config
|
9 |
+
st.set_page_config(page_title="Ctrack AI Assistant", layout="wide")
|
10 |
|
11 |
# Authentication
|
12 |
AUTHORIZED_USERS = {
|
13 |
"[email protected]": "Pass.123",
|
14 |
"[email protected]": "Pass.123",
|
15 |
+
"test@test.com": "Pass.123"
|
16 |
}
|
17 |
|
18 |
if "authenticated" not in st.session_state:
|
|
|
33 |
st.button("Login", on_click=login)
|
34 |
st.stop()
|
35 |
|
36 |
+
# Page Header
|
37 |
+
st.title("🚚 Ctrack Virtual Assistant")
|
38 |
+
st.caption("Official AI assistant for Ctrack’s fleet management solutions")
|
39 |
|
40 |
+
# Load OpenAI API Key
|
41 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
42 |
+
CTRACK_ASSISTANT_ID = "asst_ItfaIt2UF2nN1UpOS5xa6l5p"
|
|
|
43 |
|
44 |
+
if not OPENAI_API_KEY:
|
45 |
+
st.error("Missing OPENAI_API_KEY environment variable.")
|
46 |
st.stop()
|
47 |
|
48 |
+
# Create Tabs
|
49 |
+
main_tab, flagged_tab = st.tabs(["Ctrack Assistant", "Flagged Responses"])
|
50 |
|
51 |
+
# Directory to store flagged responses
|
52 |
FLAGGED_RESPONSES_DIR = "flagged_responses"
|
53 |
os.makedirs(FLAGGED_RESPONSES_DIR, exist_ok=True)
|
54 |
|
|
|
60 |
"response": ai_response,
|
61 |
"timestamp": datetime.now().isoformat()
|
62 |
}
|
|
|
63 |
with open(filename, "w") as file:
|
64 |
json.dump(flagged_data, file, indent=4)
|
|
|
65 |
st.success(f"Response flagged and saved as {filename}.")
|
66 |
|
67 |
+
def ctrack_chat(tab):
|
|
|
68 |
with tab:
|
69 |
+
st.subheader("🤖 Chat with Ctrack AI")
|
70 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
71 |
+
session_key = "ctrack_chat"
|
72 |
|
73 |
if session_key not in st.session_state:
|
74 |
st.session_state[session_key] = []
|
75 |
|
76 |
+
if st.button("Clear Chat", key="clear_chat"):
|
77 |
st.session_state[session_key] = []
|
78 |
st.rerun()
|
79 |
|
80 |
+
for idx, msg in enumerate(st.session_state[session_key]):
|
81 |
+
role, content = msg["role"], msg["content"]
|
82 |
+
st.chat_message(role).write(content)
|
83 |
+
if role == "assistant" and st.button("🚩 Flag", key=f"flag_{idx}"):
|
84 |
+
user_query = st.session_state[session_key][idx-1]["content"] if idx > 0 else "Unknown"
|
85 |
+
save_flagged_response(user_query, content)
|
86 |
+
|
87 |
+
if prompt := st.chat_input("Ask about Ctrack's services, fleet solutions, or integrations"):
|
|
|
|
|
|
|
|
|
88 |
st.session_state[session_key].append({"role": "user", "content": prompt})
|
89 |
st.chat_message("user").write(prompt)
|
90 |
|
91 |
try:
|
92 |
thread = client.beta.threads.create()
|
93 |
thread_id = thread.id
|
94 |
+
|
95 |
client.beta.threads.messages.create(
|
96 |
thread_id=thread_id,
|
97 |
role="user",
|
|
|
100 |
|
101 |
run = client.beta.threads.runs.create(
|
102 |
thread_id=thread_id,
|
103 |
+
assistant_id=CTRACK_ASSISTANT_ID
|
104 |
)
|
105 |
|
106 |
while True:
|
107 |
+
status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
|
108 |
+
if status.status == "completed":
|
109 |
break
|
110 |
time.sleep(1)
|
111 |
|
112 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
113 |
+
response = messages.data[0].content[0].text.value
|
114 |
+
st.session_state[session_key].append({"role": "assistant", "content": response})
|
115 |
+
st.chat_message("assistant").write(response)
|
116 |
+
if st.button("🚩 Flag", key=f"flag_response_{len(st.session_state[session_key])}"):
|
117 |
+
save_flagged_response(prompt, response)
|
118 |
+
|
119 |
except Exception as e:
|
120 |
+
st.error(f"OpenAI Error: {str(e)}")
|
121 |
+
|
122 |
+
ctrack_chat(main_tab)
|
123 |
+
|
124 |
+
# Flagged Response Viewer
|
125 |
+
with flagged_tab:
|
126 |
+
st.subheader("🚩 Flagged Responses")
|
127 |
+
files = [f for f in os.listdir(FLAGGED_RESPONSES_DIR) if f.endswith(".json")]
|
128 |
+
|
129 |
+
if files:
|
130 |
+
selected = st.selectbox("Select a flagged response to download:", files)
|
131 |
+
if selected:
|
132 |
+
with open(os.path.join(FLAGGED_RESPONSES_DIR, selected), "r") as file:
|
133 |
+
flagged = file.read()
|
134 |
+
st.download_button("📥 Download", data=flagged, file_name=selected, mime="application/json")
|
|
|
|
|
135 |
else:
|
136 |
+
st.info("No flagged responses yet.")
|