|
|
|
import pandas as pd |
|
import numpy as np |
|
from sentence_transformers import util |
|
from sentence_transformers import SentenceTransformer |
|
|
|
|
|
df = pd.read_csv('https://github.com/MikkelONielsen/deeplearning_assignment_2/raw/main/t_bbe.csv') |
|
|
|
|
|
model = SentenceTransformer('all-MiniLM-L6-v2') |
|
|
|
|
|
df['t'] = df['t'].astype('str') |
|
df.loc[df['b'] <= 39, 'Testament'] = 'Old' |
|
df.loc[df['b'] > 39, 'Testament'] = 'New' |
|
df.head() |
|
|
|
|
|
df_old = df[df['Testament'] == 'Old'] |
|
df_new = df[df['Testament'] == 'New'] |
|
|
|
|
|
documents = df_new['t'].tolist() |
|
|
|
|
|
|
|
doc_embeddings = model.encode(documents) |
|
|
|
|
|
def semantic_search(query, doc_embeddings, documents): |
|
query_embedding= model.encode(query) |
|
cosine_similarities = util.pytorch_cos_sim(query_embedding, doc_embeddings)[0] |
|
closest = np.argmax(cosine_similarities) |
|
return documents[closest] |
|
|
|
|
|
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() |