File size: 1,468 Bytes
9e39ab3
c70333a
8843769
9e39ab3
 
 
 
 
492e26e
 
 
1d07c32
e4249bc
 
1d07c32
 
e4249bc
1d07c32
 
e4249bc
 
 
 
 
 
 
1d07c32
 
113ac86
 
1d07c32
 
 
e9983ad
1d07c32
e9983ad
 
7385116
492e26e
e9983ad
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
from pymongo import MongoClient
import streamlit as st


client = MongoClient("mongodb+srv://Savir:savir2010@m0-savir.8iqdy.mongodb.net/?retryWrites=true&w=majority&appName=M0-Savir",tlsAllowInvalidCertificates=True)

db = client['sentiment-db']
collection = db['sentiment-collection']
# results = collection.find({"name":'savir'})
# for result in results:
#     st.markdown(f"{result}")
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("SavirD/distilbert-base-uncased-lora-text-classification")

# Load the model (specifically for sequence classification)
model = AutoModelForSequenceClassification.from_pretrained("SavirD/distilbert-base-uncased-lora-text-classification")


id2label = {0: "Negative", 1: "Positive"}

st.markdown("## Enter Some Text")
review = st.text_area("", height=150)

inputs = tokenizer(review, return_tensors="pt", padding=True, truncation=True)
outputs = model(**inputs)
logits = outputs.logits

probabilities = torch.softmax(logits, dim=-1)

predictions = torch.argmax(probabilities, dim=1).item()
if st.button("Calculate Sentiment"):
    
    with st.chat_message("assistant"):
        response = "The Following Text " + f'"{review}" is ' + id2label[predictions.tolist()[0]]
        response_dict = {f"{review}":id2label[predictions.tolist()[0]]}
        response_db = collection.insert_one(response_dict)
        st.markdown(response)