Spaces:
Sleeping
Sleeping
File size: 2,334 Bytes
91ed67a fd4fd19 91ed67a fd4fd19 91ed67a |
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import streamlit as st
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
import torch
import torch.nn.functional as F
import zipfile
import shutil
import os
def unzip_and_save(zip_file_path, extraction_path):
# Create the extraction directory if it doesn't exist
os.makedirs(extraction_path, exist_ok=True)
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
folder_name = os.path.basename(zip_file_path).split('.')[0]
zip_ref.extractall(extraction_path)
source_path = os.path.join(extraction_path, folder_name)
destination_path = os.path.join(extraction_path, folder_name)
if os.path.exists(destination_path):
print(f"Error: Destination path '{destination_path}' already exists")
else:
shutil.move(source_path, destination_path)
# Example usage:
zip_file_path = 'finetuned_bert_sentiment_harsh.zip' # Path to your ZIP file
extraction_path = 'bert_model_sentiment_v1' # Destination folder for extraction
unzip_and_save(zip_file_path, extraction_path)
# Load the fine-tuned model and tokenizer
model_path = "bert_model_sentiment_v1/finetuned_bert_sentiment_harsh"
tokenizer_path = "bert_model_sentiment_v1/finetuned_bert_sentiment_harsh"
@st.cache_resource
def load_model():
model = DistilBertForSequenceClassification.from_pretrained(model_path)
tokenizer = DistilBertTokenizerFast.from_pretrained(tokenizer_path)
return model, tokenizer
model, tokenizer = load_model()
def predict_sentiment(text):
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
tokenized = tokenizer(text, truncation=True, padding=True, return_tensors='pt').to(device)
outputs = model(**tokenized)
probs = F.softmax(outputs.logits, dim=-1)
preds = torch.argmax(outputs.logits, dim=-1).item()
probs_max = probs.max().detach().cpu().numpy()
prediction = "Positive" if preds == 1 else "Negative"
return prediction, probs_max * 100
st.title("Sentiment Analysis App")
text = st.text_area("Enter your text:")
if st.button("Predict Sentiment"):
if text:
sentiment, confidence = predict_sentiment(text)
st.write(f"Sentiment: {sentiment}")
st.write(f"Confidence: {confidence:.2f}%")
else:
st.write("Please enter some text.") |