File size: 3,606 Bytes
5ecde30 |
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 89 90 |
import os
from openai import OpenAI
import pandas as pd
import numpy as np
import faiss
import streamlit as st
from dotenv import load_dotenv
from pathlib import Path
from src.E_openAI_embeddings import calculate_openai_similarity, load_openai_embeddings
# Streamlit UI
st.title("Sentence Similarity Finder")
# Initialize session state for embeddings
if 'embeddings' not in st.session_state:
st.session_state.embeddings = None
if 'api_key' not in st.session_state:
st.session_state.api_key = None
try:
api_key = os.getenv('OPENAI_API_KEY')
# session_state.api_key = api_key
st.session_state.api_key = api_key
if not api_key:
st.error("OpenAI API key not found. Please set it in the .env file.")
else:
st.success("OpenAI API key loaded successfully.")
api_key = st.session_state.api_key
openai = OpenAI()
openai.api_key = api_key # Initialize OpenAI API
except Exception as e:
st.error(f"An error occurred while loading the OpenAI API: {e}")
# Button to load OpenAI embeddings
if st.button("Get OpenAI embeddings"):
embeddings_path = Path("embeddings") / "openai_embeddings.csv"
if not embeddings_path.exists():
st.error(f"Embeddings file not found at {embeddings_path}.")
else:
# Load embeddings and store in session state
embeddings = load_openai_embeddings(str(embeddings_path))
st.session_state.embeddings = embeddings
st.success(f"Embeddings loaded from {embeddings_path}.")
# User input for sentence similarity
user_input = st.text_input("Enter a sentence:")
# Button to calculate similarity
similarity_button = st.button("Calculate similarity")
if similarity_button:
if st.session_state.embeddings is None:
st.warning("Please load embeddings by clicking the 'Get OpenAI embeddings' button.")
elif not user_input.strip():
st.warning("Please enter a valid sentence for similarity calculation.")
else:
top_similar_sentences = get_openai_similarity(user_input, st.session_state.embeddings, top_n=5)
st.write(f'top_similar_sentences: {top_similar_sentences[:1]}')
if top_similar_sentences:
st.write("**Top 5 similar sentences:**")
for i, (sentence, score) in enumerate(top_similar_sentences, 1):
st.write(f"{i}. **Sentence:** {sentence}")
st.write(f" **Similarity score:** {score:.4f}")
else:
st.info("No similar sentences found.")
stop()
# Button to calculate similarity
if st.button("Calculate similarity"):
try:
if st.session_state.embeddings is None:
st.warning("Please load embeddings by clicking the 'Get OpenAI embeddings' button.")
elif not user_input.strip():
st.warning("Please enter a valid sentence for similarity calculation.")
else:
top_similar_sentences = calculate_openai_similarity(user_input, st.session_state.embeddings, top_n=5)
st.write(f'top_similar_sentences: {top_similar_sentences[:1]}')
if top_similar_sentences:
st.write("**Top 5 similar sentences:**")
for i, (sentence, score) in enumerate(top_similar_sentences, 1):
st.write(f"{i}. **Sentence:** {sentence}")
st.write(f" **Similarity score:** {score:.4f}")
else:
st.info("No similar sentences found.")
except Exception as e:
print(f"An error occurred during similarity calculation: {e}")
st.error(f"An error occurred during similarity calculation: {e}") |