|
import streamlit as st |
|
from functions import * |
|
|
|
st.set_page_config(page_title="Earnings Semantic Search", page_icon="π") |
|
st.sidebar.header("Semantic Search") |
|
st.markdown("## Earnings Semantic Search with SBert") |
|
|
|
search_input = st.text_input( |
|
label='Enter Your Search Query, e.g "What challenges did the business face?"', key='search') |
|
|
|
top_k = st.sidebar.slider("Number of Top Hits Generated",min_value=1,max_value=5,value=2) |
|
|
|
window_size = st.sidebar.slider("Number of Sentences Generated in Search Response",min_value=1,max_value=5,value=3) |
|
|
|
earnings_sentiment, earnings_sentences = sentiment_pipe(earnings_passages) |
|
|
|
with st.expander("See Transcribed Earnings Text"): |
|
st.write(f"Number of Sentences: {len(earnings_sentences)}") |
|
|
|
st.write(earnings_passages) |
|
|
|
|
|
|
|
sen_df = pd.DataFrame(earnings_sentiment) |
|
sen_df['text'] = earnings_sentences |
|
grouped = pd.DataFrame(sen_df['label'].value_counts()).reset_index() |
|
grouped.columns = ['sentiment','count'] |
|
|
|
|
|
passages = preprocess_plain_text(st.session_state['earnings_passages'],window_size=window_size) |
|
|
|
|
|
|
|
corpus_embeddings = sbert.encode(passages, convert_to_tensor=True, show_progress_bar=True) |
|
question_embedding = sbert.encode(search_input, convert_to_tensor=True) |
|
question_embedding = question_embedding.cpu() |
|
hits = util.semantic_search(question_embedding, corpus_embeddings, top_k=top_k,score_function=util.dot_score) |
|
hits = hits[0] |
|
|
|
|
|
|
|
cross_inp = [[query, passages[hit['corpus_id']]] for hit in hits] |
|
cross_scores = cross_encoder.predict(cross_inp) |
|
|
|
|
|
for idx in range(len(cross_scores)): |
|
hits[idx]['cross-score'] = cross_scores[idx] |
|
|
|
|
|
st.markdown("\n-------------------------\n") |
|
st.subheader(f"Top-{top_k} Bi-Encoder Retrieval hits") |
|
hits = sorted(hits, key=lambda x: x['score'], reverse=True) |
|
|
|
cross_df = display_df_as_table(hits,top_k) |
|
st.write(cross_df.to_html(index=False), unsafe_allow_html=True) |
|
|
|
|
|
st.markdown("\n-------------------------\n") |
|
st.subheader(f"Top-{top_k} Cross-Encoder Re-ranker hits") |
|
hits = sorted(hits, key=lambda x: x['cross-score'], reverse=True) |
|
|
|
rerank_df = display_df_as_table(hits,top_k,'cross-score') |
|
st.write(rerank_df.to_html(index=False), unsafe_allow_html=True |
|
|