Skip to content

Commit

Permalink
fix: ironed out auth UX
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisVLRT committed Jan 16, 2024
1 parent 6d4c365 commit e65933f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
3 changes: 1 addition & 2 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,9 @@ async def login(form_data: OAuth2PasswordRequestForm = Depends()) -> dict:
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(minutes=60)
user_data = user.model_dump()
del user_data["hashed_password"]
access_token = create_access_token(data=user_data, expires_delta=access_token_expires)
access_token = create_access_token(data=user_data)
return {"access_token": access_token, "token_type": "bearer"}


Expand Down
20 changes: 8 additions & 12 deletions frontend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@
)

if st.session_state.get("session", None) is None:
session = auth()
auth()
st.stop()
else:
logo_chat = Image.open(assets / "logo_chat.png")
logo_user = Image.open(assets / "logo_user.png")

logo_chat = Image.open(assets / "logo_chat.png")
logo_user = Image.open(assets / "logo_user.png")
st.image(Image.open(assets / "logo_title.jpeg"))
st.caption("Learn more about the RAG indus kit here: https://artefactory.github.io/skaff-rag-accelerator")

st.image(Image.open(assets / "logo_title.jpeg"))
st.caption(
"Démo d'un assistant IA, cloud agnostic, permettant de faire du RAG \
sur différent types de document.\
Le backend du ChatBot est APéïsé ce qui permet une meilleure scalabilité et robustesse."
)

sidebar()
chat()
sidebar()
chat()
12 changes: 6 additions & 6 deletions frontend/lib/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ def auth() -> Optional[str]:
default="Login",
)
if tab == "Login":
return login_form()
login_form()
elif tab == "Signup":
return signup_form()
signup_form()
else:
st.error("Invalid auth mode")
return None


def login_form() -> tuple[bool, Optional[str]]:
def login_form():
with st.form("Login"):
username = st.text_input("Username", key="username")
password = st.text_input("Password", type="password")
Expand All @@ -43,10 +43,10 @@ def login_form() -> tuple[bool, Optional[str]]:
st.error("Failed authentication")
st.session_state["session"] = session
st.session_state["email"] = username
return session
st.rerun()


def signup_form() -> tuple[bool, Optional[str]]:
def signup_form():
with st.form("Signup"):
username = st.text_input("Username", key="username")
password = st.text_input("Password", type="password")
Expand All @@ -63,7 +63,7 @@ def signup_form() -> tuple[bool, Optional[str]]:
st.error("Failed signing up")
st.session_state["session"] = auth_session
st.session_state["email"] = username
return auth_session
st.rerun()


def get_token(username: str, password: str) -> Optional[str]:
Expand Down
12 changes: 12 additions & 0 deletions frontend/lib/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import streamlit as st

def query(verb: str, url: str, **kwargs):
session = st.session_state.get("session")
response = getattr(session, verb)(url, **kwargs)

if response.status_code == 401:
st.session_state["session"] = None
st.session_state["email"] = None
st.rerun()

return response
18 changes: 8 additions & 10 deletions frontend/lib/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import streamlit as st
from streamlit_feedback import streamlit_feedback

from frontend.lib.auth import auth
from frontend.lib.backend import query


@dataclass
class Message:
Expand Down Expand Up @@ -63,23 +66,18 @@ def chat():


def new_chat():
session = st.session_state.get("session")
response = session.post("/chat/new")
st.session_state["chat_id"] = response.json()["chat_id"]
chat_id = query("post", "/chat/new").json()["chat_id"]
st.session_state["chat_id"] = chat_id
st.session_state["messages"] = []
return response.json()["chat_id"]
return chat_id


def send_prompt(message: Message):
session = st.session_state.get("session")
response = session.post(f"/chat/{message.chat_id}/user_message", stream=True, json=asdict(message))

response = query("post", f"/chat/{message.chat_id}/user_message", stream=True, json=asdict(message))
for line in response.iter_content(chunk_size=16, decode_unicode=True):
yield line


def send_feedback(message_id: str, feedback: str):
feedback = "thumbs_up" if feedback["score"] == "👍" else "thumbs_down"
session = st.session_state.get("session")
response = session.post(f"/feedback/{message_id}/{feedback}")
return response.text
return query("post", f"/feedback/{message_id}/{feedback}").text
10 changes: 4 additions & 6 deletions frontend/lib/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import humanize
import streamlit as st
from frontend.lib.backend import query

from frontend.lib.chat import Message

Expand Down Expand Up @@ -35,11 +36,8 @@ def sidebar():


def list_chats():
session = st.session_state.get("session")
response = session.get("/chat/list")
return response.json()
return query("get", "/chat/list").json()

def get_chat(chat_id: str):
session = st.session_state.get("session")
response = session.get(f"/chat/{chat_id}")
return response.json()
return query("get", f"/chat/{chat_id}").json()

0 comments on commit e65933f

Please sign in to comment.