Nechba's picture
Update app.py
a1fc5e8 verified
from utlis.helper import *
import sqlite3
import hashlib
import io
initialize_session_state()
with st.sidebar:
st.image("logo.png", width=170)
st.title("Config Settings")
# Get List of models
st.session_state.genre = st.radio(
"Choose option",
[ "Select Service", "Add service"])#,"Delete service"])
if st.session_state.genre=="Add service":
st.session_state.results_str=False
st.title('Add service')
# Check service status
# Get all available services
add_new_service = st.checkbox("Add new service")
if add_new_service:
new_service = st.text_input("Enter service name")
# Get list of Embedding models
if new_service and st.button('Add'):
add_service(st.session_state.token,new_service)
data = {"token": st.session_state.token}
json_data = json.dumps(data)
headers = {'Content-Type': 'application/json'}
services = requests.get(SERVICES_API,data=json_data, headers=headers)
services =json.loads(services.text)
if len(services)>0:
st.session_state.service = st.selectbox("Choose Service",services)
st.session_state.uploaded_files = st.file_uploader("Upload CVs", type=["pdf","doc"], accept_multiple_files=True)
if st.session_state.uploaded_files:
st.session_state.process = st.button('Process')
if st.session_state.process:
with st.spinner("Processing..."):
add_document(st.session_state.token,st.session_state.service)
elif st.session_state.genre=="Select Service":
st.title('Choose Service')
data = {"token": st.session_state.token}
json_data = json.dumps(data)
headers = {'Content-Type': 'application/json'}
services = requests.get(SERVICES_API,data=json_data, headers=headers)
services =json.loads(services.text)
if len(services)>0:
st.session_state.service_slected_to_chat = st.selectbox(" ",services)
st.session_state.top_k=st.slider("Number of Candidates", min_value=1, max_value=10, value=3, step=1)
# data = {"token": st.session_state.token, "servicename": st.session_state.service_slected_to_chat}
# json_data = json.dumps(data)
# headers = {'Content-Type': 'application/json'}
# history_document = requests.get(DOCUMENT_API,data=json_data, headers=headers)
# history_document =json.loads(history_document.text).get("documents",[])
# history_document = [doc["documentname"] for doc in history_document]
# elif st.session_state.genre == "Delete service":
# st.title('Delete Service')
# data = {"token": st.session_state.token}
# json_data = json.dumps(data)
# headers = {'Content-Type': 'application/json'}
# services = requests.get(SERVICES_API,data=json_data, headers=headers)
# services =json.loads(services.text)
# if len(services)>=2:
# services.append("ALL")
# # Get list of documents from histrory
# if "ALL" in services:
# service_slected = st.multiselect(
# "",services ,default="ALL"
# )
# elif len(services)==1:
# service_slected = st.multiselect(
# "",services,default=services[0]
# )
# else:
# service_slected = st.multiselect(
# "",services
# )
# if "ALL" in service_slected:
# service_slected = services
# service_slected.remove("ALL")
# st.write("You selected:", service_slected)
# if len(service_slected) > 0:
# st.session_state.delete = st.button('Delete')
# if st.session_state.delete:
# delete_service(st.session_state.token ,service_slected)
# elif st.session_state.genre == "Delete CV(s)":
# st.title('Delete CV(s)')
# data = {"token": st.session_state.token}
# json_data = json.dumps(data)
# headers = {'Content-Type': 'application/json'}
# services = requests.get(SERVICES_API,data=json_data, headers=headers)
# services =json.loads(services.text)
# if len(services)>0:
# service = st.selectbox("Choose Service",services)
# data = {"token": st.session_state.token, "servicename": service}
# json_data = json.dumps(data)
# headers = {'Content-Type': 'application/json'}
# st.write("You selected:", document_slected_to_delete)
# if len(document_slected_to_delete) > 0:
# st.session_state.delete = st.button('Delete')
# if st.session_state.delete:
# delete_document(st.session_state.token,st.session_state.service ,document_slected_to_delete)
# css_style = """
# <style>
# .title {
# white-space: nowrap;
# }
# </style>
# """
# st.markdown(css_style, unsafe_allow_html=True)
st.markdown("""
<style>
.st-bm {
color: #1E90FF; /* DodgerBlue color */
}
.card {
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
padding: 15px;
margin: 10px 0;
transition: box-shadow 0.3s ease-in-out;
}
.card:hover {
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
.highlight {
color: #1E90FF; /* Custom color */
}
</style>
""", unsafe_allow_html=True)
with st.container():
st.markdown('<h1 class="title">SmartHire Matcher</h1>', unsafe_allow_html=True)
col1, col2 = st.columns([3, 1])
if st.session_state.genre=="Select Service" and st.session_state.service_slected_to_chat:
query = st.text_area("Add description of your offer:", height=300)
if query and st.button('Process') :
with st.spinner("Finding Matching CVs..."):
results = search_document( index_name= "cvindex",token= "abcd",service_name= st.session_state.service_slected_to_chat,query= query,top_k= st.session_state.top_k)
st.session_state.results_str = results.decode('utf-8')
# Displaying results
try:
if st.session_state.results_str: # check if there are results
results = json.loads(st.session_state.results_str)
for index, item in enumerate(results):
with st.container():
col1, col2 = st.columns([3, 1])
with col1:
st.markdown(f"<div class='card'><h3><span class='highlight'>Top:</span> {index+1}</h3><p><span class='highlight'>Score:</span> {round(item['score']*100, 3)}%</p><p><span class='highlight'>Document Name:</span> {item['documentname'].replace('_pdf', ' ')}</p></div>", unsafe_allow_html=True)
#st.markdown(f"**Tag:** {item['tag']}")
#st.markdown(f"**Score:** {round(item['score'], 3)}")
#st.markdown(f"**Document Name:** {item['documentname'].replace('_pdf', ' ')}")
with col2:
b64_pdf = item['encoded_cv']
pdf = base64.b64decode(b64_pdf)
pdf_file = io.BytesIO(pdf)
st.download_button("Download CV", data=pdf_file, file_name=item['documentname'].replace('_', '.'), mime='application/pdf', key=f"download_{index}_{item['documentname']}")
if not results:
st.error("No results found.")
except Exception as e:
st.error("Failed to load results. Please try again later."+ str(e))