Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +67 -0
- bert_model_dir.zip +3 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
import zipfile
|
| 7 |
+
import shutil
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
def unzip_and_save(zip_file_path, extraction_path):
|
| 11 |
+
# Create the extraction directory if it doesn't exist
|
| 12 |
+
os.makedirs(extraction_path, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
| 15 |
+
folder_name = os.path.basename(zip_file_path).split('.')[0]
|
| 16 |
+
zip_ref.extractall(extraction_path)
|
| 17 |
+
source_path = os.path.join(extraction_path, folder_name)
|
| 18 |
+
destination_path = os.path.join(extraction_path, folder_name)
|
| 19 |
+
if os.path.exists(destination_path):
|
| 20 |
+
print(f"Error: Destination path '{destination_path}' already exists")
|
| 21 |
+
else:
|
| 22 |
+
shutil.move(source_path, destination_path)
|
| 23 |
+
|
| 24 |
+
# Example usage:
|
| 25 |
+
# Path to your ZIP file which is your sentimetn analysis model zip
|
| 26 |
+
zip_file_path = 'bert_model_dir.zip'
|
| 27 |
+
# Destination folder for extraction
|
| 28 |
+
extraction_path = 'bert_model_sentiment_v1'
|
| 29 |
+
|
| 30 |
+
unzip_and_save(zip_file_path, extraction_path)
|
| 31 |
+
|
| 32 |
+
# Load the fine-tuned model and tokenizer
|
| 33 |
+
model_path = "bert_model_sentiment_v1/bert_model_dir"
|
| 34 |
+
tokenizer_path = "bert_model_sentiment_v1/bert_model_dir"
|
| 35 |
+
|
| 36 |
+
@st.cache_resource
|
| 37 |
+
def load_model():
|
| 38 |
+
model = DistilBertForSequenceClassification.from_pretrained(model_path)
|
| 39 |
+
tokenizer = DistilBertTokenizerFast.from_pretrained(tokenizer_path)
|
| 40 |
+
return model, tokenizer
|
| 41 |
+
|
| 42 |
+
model, tokenizer = load_model()
|
| 43 |
+
|
| 44 |
+
def predict_sentiment(text):
|
| 45 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 46 |
+
model.to(device)
|
| 47 |
+
|
| 48 |
+
tokenized = tokenizer(text, truncation=True, padding=True, return_tensors='pt').to(device)
|
| 49 |
+
outputs = model(**tokenized)
|
| 50 |
+
|
| 51 |
+
probs = F.softmax(outputs.logits, dim=-1)
|
| 52 |
+
preds = torch.argmax(outputs.logits, dim=-1).item()
|
| 53 |
+
probs_max = probs.max().detach().cpu().numpy()
|
| 54 |
+
|
| 55 |
+
prediction = "Positive" if preds == 1 else "Negative"
|
| 56 |
+
return prediction, probs_max * 100
|
| 57 |
+
|
| 58 |
+
st.title("Sentiment Analysis App")
|
| 59 |
+
text = st.text_area("Enter your text:")
|
| 60 |
+
|
| 61 |
+
if st.button("Predict Sentiment"):
|
| 62 |
+
if text:
|
| 63 |
+
sentiment, confidence = predict_sentiment(text)
|
| 64 |
+
st.write(f"Sentiment: {sentiment}")
|
| 65 |
+
st.write(f"Confidence: {confidence:.2f}%")
|
| 66 |
+
else:
|
| 67 |
+
st.write("Please enter some text.")
|
bert_model_dir.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ac792554c09eef0f722fc6228e33c09a8b421ae9cad7ca78c76bf7c5285a82e7
|
| 3 |
+
size 247101672
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
streamlit
|