import logging import os import gradio as gr import pandas as pd import cfg from cfg import buster logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) USERNAME = os.getenv("BUSTER_USERNAME") PASSWORD = os.getenv("BUSTER_PASSWORD") def check_auth(username: str, password: str) -> bool: valid_user = username == USERNAME valid_password = password == PASSWORD is_auth = valid_user and valid_password logger.info(f"Log-in attempted by {username=}. {is_auth=}") return is_auth def format_sources(matched_documents: pd.DataFrame) -> str: if len(matched_documents) == 0: return "" documents_answer_template: str = "📝 Here are the sources I used to answer your question:\n\n{documents}\n\n{footnote}" document_template: str = "[🔗 {document.title}]({document.url}), relevance: {document.similarity_to_answer:2.1f} %" matched_documents.similarity_to_answer = ( matched_documents.similarity_to_answer * 100 ) documents = "\n".join( [ document_template.format(document=document) for _, document in matched_documents.iterrows() ] ) footnote: str = "I'm a bot 🤖 and not always perfect." return documents_answer_template.format(documents=documents, footnote=footnote) def add_sources(history, completion): if completion.answer_relevant: formatted_sources = format_sources(completion.matched_documents) history.append([None, formatted_sources]) return history def user(user_input, history): """Adds user's question immediately to the chat.""" return "", history + [[user_input, None]] def chat(history): user_input = history[-1][0] completion = buster.process_input(user_input) history[-1][1] = "" for token in completion.answer_generator: history[-1][1] += token yield history, completion block = gr.Blocks(css="#chatbot .overflow-y-auto{height:500px}") with block: with gr.Row(): gr.Markdown( "