SavirD's picture
Update app.py
113ac86 verified
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)