DivyaS1's picture
Update app.py
84cd004 verified
import streamlit as st
import pandas as pd
from langchain_core.messages import AIMessage, HumanMessage
from azure_openai import qt, get_response
from retriver import search_and_reconstruct
def read_file(file):
"""
Reads the content of a text file and returns it as a string.
:param approver: The type of approver.
:return: The content of the file as a string.
"""
fp = f"assets/{file}.md"
try:
with open(fp, 'r', encoding='utf-8') as file:
content = file.read()
return content
except FileNotFoundError:
print(f"The file at {fp} was not found.")
except IOError:
print(f"An error occurred while reading the file at {fp}.")
QTESystemMessage = read_file("QTESystemMessage")
RAGSystemMessage = read_file("RAGSystemMessage")
RAGUserMessage = read_file("RAGUserMessage")
k = 5
pagesReturned = 3
temp1 = 0.5
tokens1 = 200
temp2 = 0.6
tokens2 = 2000
asset = "GSKGlossary"
# app config
st.set_page_config(page_title="Medical Sales Toolbox", page_icon="🤖")
st.title("Medical Sales Toolbox :toolbox:")
# session state
if "chat_history" not in st.session_state:
st.session_state.chat_history = [
AIMessage(content="Hello, I am the Medical Sales Assistant. How can I help you?"),
]
# conversation
for message in st.session_state.chat_history:
if isinstance(message, AIMessage):
with st.chat_message("AI"):
st.write(message.content)
elif isinstance(message, HumanMessage):
with st.chat_message("Human"):
st.write(message.content)
# user input
user_query = st.chat_input("Type your message here...")
if user_query is not None and user_query != "":
st.session_state.chat_history.append(HumanMessage(content=user_query))
with st.chat_message("Human"):
st.markdown(user_query)
qte = qt(QTESystemMessage, st.session_state.chat_history, temp1, tokens1, asset)
st.text("Contextualised Query")
st.caption(qte)
knowledge = search_and_reconstruct(qte, k, pagesReturned)
if knowledge:
# Prepare the data for the table
table_data = {
"Title": [entry['Title'] for entry in knowledge],
"Score (%)": [f"{int(entry.get('Score', 0) * 100)}%" for entry in knowledge], # Convert to percentage and remove decimals
"Page": [entry['PageNumber'] for entry in knowledge],
# "Grounding Text": [entry['ReconstructedText'] for entry in knowledge]
}
# Create a dataframe for displaying as a table
df = pd.DataFrame(table_data)
# Calculate the mean score
mean_score = sum(entry.get('Score', 0) for entry in knowledge) / len(knowledge)
# Display the mean score as a Streamlit text element
# Display the table in the sidebar
st.text("Knowledge Base Results")
st.text(f"Average Accuracy Score: {mean_score * 100:.2f}%")
st.dataframe(df) # Adjust height as needed
else:
st.write("No relevant knowledge base results found.")
with st.chat_message("AI"):
response = st.write_stream(get_response(st.session_state.chat_history, qte, knowledge, temp2, tokens2, RAGSystemMessage, RAGUserMessage, asset))
st.session_state.chat_history.append(AIMessage(content=response))