emiliosheinz commited on
Commit
e922469
·
1 Parent(s): 66f1e76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -1,10 +1,13 @@
 
1
  import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
 
4
- model = AutoModelForSequenceClassification.from_pretrained("sentence-transformers/all-distilroberta-v1")
 
 
5
 
6
  # set the app title
7
- st.title("Sentence Similarity Checker")
8
 
9
  # get the input sentences from the user
10
  sentence1 = st.text_input("Enter the first sentence:")
@@ -12,12 +15,16 @@ sentence2 = st.text_input("Enter the second sentence:")
12
 
13
  # check if both sentences are not empty
14
  if sentence1 and sentence2:
15
- # encode the sentences into embeddings
16
- embeddings1 = model.encode(sentence1, convert_to_tensor=True)
17
- embeddings2 = model.encode(sentence2, convert_to_tensor=True)
18
 
19
- # calculate the cosine similarity between the embeddings
20
- similarity_score = float(embeddings1 @ embeddings2.T)
 
21
 
22
- # display the similarity score to the user
23
- st.write("Similarity score:", similarity_score)
 
 
 
 
 
1
+ import torch
2
  import streamlit as st
3
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
 
5
+ # load the pre-trained and fine-tuned model and tokenizer
6
+ tokenizer = AutoTokenizer.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
7
+ model = AutoModelForSequenceClassification.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
8
 
9
  # set the app title
10
+ st.title("Brazilian Portuguese Sentence Similarity Checker")
11
 
12
  # get the input sentences from the user
13
  sentence1 = st.text_input("Enter the first sentence:")
 
15
 
16
  # check if both sentences are not empty
17
  if sentence1 and sentence2:
18
+ # tokenize the sentences and get their IDs
19
+ input_ids = tokenizer.encode(sentence1, sentence2, truncation=True, padding=True, return_tensors='pt')
 
20
 
21
+ # pass the IDs through the model to get the logits
22
+ with torch.no_grad():
23
+ logits = model(input_ids)[0]
24
 
25
+ # apply softmax to the logits to get the predicted probabilities
26
+ probs = torch.softmax(logits, dim=1).squeeze().tolist()
27
+
28
+ # display the predicted probabilities to the user
29
+ st.write("Probability that the sentences are similar:", probs[1])
30
+ st.write("Probability that the sentences are dissimilar:", probs[0])