File size: 1,252 Bytes
75a1cfc
 
 
0a745c3
75a1cfc
 
 
 
3ad6269
75a1cfc
 
 
6beea90
3ad6269
 
 
 
 
 
75a1cfc
 
 
 
 
 
 
9141fcd
75a1cfc
6beea90
 
 
75a1cfc
 
 
6beea90
75a1cfc
 
 
 
 
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
import streamlit as st
import torch
from peft import PeftModel
from transformers import AutoModel, AutoTokenizer

model_name = 'intfloat/multilingual-e5-large'
adapters_name = './checkpoint-21170'

st.header(":shopping_trolley: Irrelevant Reviews Detection :sunglasses:")
description = st.text_input("Product description")
review = st.text_input("Review")

if st.button("Detect") and description and review:
    model = AutoModel.from_pretrained(model_name)
    model = PeftModel.from_pretrained(model, adapters_name)
    model = model.merge_and_unload()
    
    tokenizer = AutoTokenizer.from_pretrained(model_name)

    input_texts = [
        f'query: {review}',
        f'passage: {description}'
    ]
    batch_dict = tokenizer(input_texts, max_length=512,
                           padding=True, truncation=True, return_tensors='pt')

    query_embedding, doc_embedding = model(**batch_dict, return_dict=True).pooler_output

    query_embedding = query_embedding.unsqueeze(0)
    doc_embedding = doc_embedding.unsqueeze(0)

    similarity = torch.nn.functional.cosine_similarity(
        query_embedding, doc_embedding)

    threshold = 0.83

    if similarity > threshold:
        st.write('Relevant')
    else:
        st.write('Irrelevant')