Annikaijak's picture
Update app.py
c64d31c verified
# Importing required packages
import pandas as pd
import numpy as np
from sentence_transformers import util
from sentence_transformers import SentenceTransformer
# Loading the data from Mikkel's github repository
df = pd.read_csv('https://github.com/MikkelONielsen/deeplearning_assignment_2/raw/main/t_bbe.csv')
# Loading the Simple Sentence Transformer model and storing it as "model"
model = SentenceTransformer('all-MiniLM-L6-v2')
# Adding a variable showing if the book is from the Old or New testament
df['t'] = df['t'].astype('str')
df.loc[df['b'] <= 39, 'Testament'] = 'Old'
df.loc[df['b'] > 39, 'Testament'] = 'New'
df.head()
# Defining a dataframe for each testament
df_old = df[df['Testament'] == 'Old']
df_new = df[df['Testament'] == 'New']
# Defining a variable containing all the verses in a list.
documents = df_new['t'].tolist()
# converting our text data into sentence embeddings
doc_embeddings = model.encode(documents)
def semantic_search(query, doc_embeddings, documents):
query_embedding= model.encode(query) # Create the sentence embedding for the query
cosine_similarities = util.pytorch_cos_sim(query_embedding, doc_embeddings)[0] # Calculate the cosine similarity and look up the first one
closest = np.argmax(cosine_similarities) # Search for the closest embedding
return documents[closest]
# For creating interface
import gradio as gr
def find_similar(query):
vers = semantic_search(query, doc_embeddings, documents)
return vers
markdown = '''
# Use your favorite inspiriational quotes to find the best suiting bible verse!
This app performs semantic search to find the most relevant bible verse to your inspirational instagram quote.
'''
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
gr.Markdown(markdown)
gr.Image("https://m.media-amazon.com/images/I/71HrIj6FUhL.jpg")
with gr.Column():
gr.Markdown("""
## Semantic Search
""")
Text = gr.Text(label="Enter your inspirational instagram quote:")
btn = gr.Button("Find my bible verse!")
similar = gr.Textbox(label='Most similar bible verse:')
gr.Examples([["Live, Love, Laugh"], ["Life is a canvas"], ["Embrace the journey"]], inputs=[Text], outputs=[similar])
btn.click(
find_similar,
inputs=[Text],
outputs=[similar],
)
if __name__ == "__main__":
demo.launch()