mwells commited on
Commit
dd48f96
Β·
verified Β·
1 Parent(s): 102e30f

Initial commit

Browse files
Files changed (3) hide show
  1. app.py +71 -0
  2. menu.py +76 -0
  3. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ import os
4
+ import urllib.request
5
+ import json
6
+ import ssl
7
+ from menu import menu
8
+
9
+
10
+ menu()
11
+
12
+ # Retrieve Answer through API -----------------------------------------------
13
+ def chat_api(history, question):
14
+ data = {'chat_history': history,"question": question}
15
+ body = str.encode(json.dumps(data))
16
+ url = st.secrets["url"]
17
+ api_key = st.secrets["ms_api_key"]
18
+ # Replace this with the primary/secondary key, AMLToken, or Microsoft Entra ID token for the endpoint
19
+ if not api_key:
20
+ raise Exception("A key should be provided to invoke the endpoint")
21
+
22
+
23
+ headers = {'Content-Type':'application/json', 'Accept': 'application/json', 'Authorization':('Bearer '+ api_key)}
24
+
25
+ req = urllib.request.Request(url, body, headers)
26
+ response = urllib.request.urlopen(req)
27
+ result = response.read()
28
+ answer = json.loads(result)["answer"]
29
+
30
+ return(answer)
31
+
32
+
33
+ # Header UI ------------------------------------------------------------------
34
+ # st.image('./Cognizant_Logo.jpg', width=300)
35
+ st.header("RCM Chat", divider = 'blue')
36
+
37
+ # Chat UI --------------------------------------------------------------------
38
+ st.write("This chat as access to search the web to answer your quesitons.")
39
+ st.header("", divider = 'blue')
40
+
41
+ # Initialize chat history
42
+ if "messages" not in st.session_state:
43
+ st.session_state.messages = []
44
+
45
+ # Display chat messages from history on app rerun
46
+ for message in st.session_state.messages:
47
+ with st.chat_message(message["role"]):
48
+ st.markdown(message["content"])
49
+
50
+ # Accept user input
51
+ if prompt := st.chat_input("What is your question?"):
52
+ # Add user message to chat history
53
+ st.session_state.messages.append({"role": "user", "content": prompt})
54
+ # Display user message in chat message container
55
+ with st.chat_message("user"):
56
+ st.markdown(prompt)
57
+
58
+ # Display assistant response in chat message container
59
+ with st.chat_message("assistant"):
60
+ message_placeholder = st.empty()
61
+ full_response = ""
62
+ with st.status("Getting the answer...") as status:
63
+ #assistant_response = get_completion(prompt, ft_model)
64
+ chat_history = st.session_state.messages
65
+ print(chat_history)
66
+ assistant_response = chat_api(history = chat_history, question = prompt)
67
+ full_response = assistant_response
68
+ status.update(label="Done.", state = "complete")
69
+ message_placeholder.markdown(full_response)
70
+ # Add assistant response to chat history
71
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
menu.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import hmac
3
+
4
+
5
+ def _check_password():
6
+ """Authenticate user and manage login state."""
7
+ if st.session_state.get("authenticated"):
8
+ return True
9
+
10
+ with st.form("Credentials"):
11
+ username = st.text_input("Username")
12
+ password = st.text_input("Password", type="password")
13
+ submitted = st.form_submit_button("Log in")
14
+
15
+ if submitted:
16
+ if username in st.secrets["passwords"] and hmac.compare_digest(
17
+ password,
18
+ st.secrets.passwords[username],
19
+ ):
20
+ st.session_state.authenticated = True
21
+ st.session_state.username = username
22
+ st.session_state.role = st.secrets.roles.get(username, "user") # Default to "user" if role not specified
23
+ st.rerun()
24
+ else:
25
+ st.error("User not known or password incorrect.")
26
+
27
+ return False
28
+
29
+
30
+ def _authenticated_menu():
31
+ # Show a navigation menu for authenticated users
32
+ st.sidebar.page_link("app.py", label="Home Page", icon="🏑")
33
+ # st.sidebar.page_link("pages/composer.py", label="Insight Composer", icon ="🎨")
34
+ # st.sidebar.page_link("pages/user.py", label="Regular User Page", icon="🚎")
35
+ if st.session_state.role in ["admin", "super-admin"]:
36
+ st.sidebar.page_link("pages/admin.py", label="Admin User Page", icon="πŸ”§")
37
+ st.sidebar.page_link(
38
+ "pages/super-admin.py",
39
+ label="Super Admin User Page",
40
+ icon="πŸ”§",
41
+ disabled=st.session_state.role != "super-admin",
42
+ )
43
+ st.sidebar.divider()
44
+ st.sidebar.write(f"Welcome, {st.session_state.username}!")
45
+ st.sidebar.write(f"Your role is: {st.session_state.role}")
46
+ if st.sidebar.button("Logout"):
47
+ _logout()
48
+
49
+
50
+ def _unauthenticated_menu():
51
+ # Show a navigation menu for unauthenticated users
52
+ # st.sidebar.page_link("home.py", label="Log in")
53
+ pass
54
+
55
+ def _logout():
56
+ """Log out the current user."""
57
+ st.session_state.clear()
58
+ st.success("You have been logged out successfully.")
59
+ st.rerun()
60
+
61
+
62
+ def menu():
63
+ # Determine if a user is logged in or not, then show the correct
64
+ if not _check_password():
65
+ _unauthenticated_menu()
66
+ st.stop()
67
+ else:
68
+ _authenticated_menu()
69
+
70
+
71
+ def menu_with_redirect():
72
+ # Redirect users to the main page if not logged in, otherwise continue to
73
+ # render the navigation menu
74
+ if not _check_password():
75
+ st.switch_page("app.py")
76
+ menu()
requirements.txt ADDED
Binary file (1.36 kB). View file