File size: 2,847 Bytes
ae266f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os

import cohere
import openai
import pandas as pd
import streamlit as st
from dotenv import load_dotenv

import helpers

load_dotenv()


# Function to initialize APIs
def initialize_apis():
    if "openai_api_key" in st.session_state and "cohere_api_key" in st.session_state:
        co = cohere.Client(st.session_state["cohere_api_key"])
        index = helpers.initialize_pinecone(
            st.session_state["api_key"], st.session_state["env"], "coherererank", 1536
        )
        return co, index
    return None, None


with st.sidebar:
    api_key = st.text_input(
        "Enter Pinecone API key:", value=os.getenv("PINECONE_API_KEY", "")
    )
    env = st.text_input(
        "Enter Pinecone environment:", value=os.getenv("PINECONE_ENVIRONMENT", "")
    )
    openai_api_key = st.text_input(
        "Enter OpenAI API key:", value=os.getenv("OPENAI_API_KEY", "")
    )
    cohere_api_key = st.text_input(
        "Enter Cohere API key:", value=os.getenv("COHERE_API_KEY", "")
    )

    if st.button("Submit API Keys"):
        st.session_state["api_key"] = api_key
        st.session_state["env"] = env
        st.session_state["openai_api_key"] = openai_api_key
        st.session_state["cohere_api_key"] = cohere_api_key

# Check if API keys are set
if all(
    key in st.session_state
    for key in ["api_key", "env", "openai_api_key", "cohere_api_key"]
):
    co, index = initialize_apis()
    if co and index:
        query = st.text_input("Enter search query:")
        top_k = st.number_input(
            "Top K resumes to fetch:", min_value=1, max_value=50, value=10
        )
        rerank_top_n = st.number_input(
            "Top N resumes to rerank:", min_value=1, max_value=top_k, value=5
        )

        if st.button("Search"):
            if query:
                with st.spinner("Fetching and evaluating resumes..."):
                    dataset = helpers.create_dataset()
                    helpers.insert_to_pinecone(index, dataset)
                    evaluation, error = helpers.evaluate_resumes(
                        index, co, query, top_k=top_k, rerank_top_n=rerank_top_n
                    )

                    comparison_data = helpers.compare(
                        index, co, query, top_k=top_k, top_n=rerank_top_n
                    )

                if evaluation:
                    st.markdown("### Evaluation:")
                    st.markdown(evaluation)

                    # Display the comparison results
                    st.markdown("### Original vs Reranked Docs Comparison:")
                    st.write("---")

                    df_comparison = pd.DataFrame(comparison_data)
                    st.table(df_comparison)

                elif error:
                    st.warning(error)
            else:
                st.warning("Please enter a query.")