Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from peft import PeftModel
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
|
6 |
+
model_name = 'intfloat/multilingual-e5-large'
|
7 |
+
adapters_name = './checkpoint-21170'
|
8 |
+
|
9 |
+
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
11 |
+
model = PeftModel.from_pretrained(model, adapters_name)
|
12 |
+
model = model.merge_and_unload()
|
13 |
+
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
15 |
+
|
16 |
+
description = st.text_input("Product description")
|
17 |
+
review = st.text_input("Review")
|
18 |
+
|
19 |
+
if description and review:
|
20 |
+
input_texts = [
|
21 |
+
f'query: {review}',
|
22 |
+
f'passage: {description}'
|
23 |
+
]
|
24 |
+
batch_dict = tokenizer(input_texts, max_length=512,
|
25 |
+
padding=True, truncation=True, return_tensors='pt')
|
26 |
+
|
27 |
+
query_embedding, doc_embedding = model(**batch_dict).pooler_output
|
28 |
+
|
29 |
+
similarity = torch.nn.functional.cosine_similarity(
|
30 |
+
query_embedding, doc_embedding)
|
31 |
+
|
32 |
+
threshold = 0.7
|
33 |
+
|
34 |
+
if similarity > threshold:
|
35 |
+
st.write('Relevant')
|
36 |
+
else:
|
37 |
+
st.write('Irrelevant')
|