dimoZ commited on
Commit
0ce5847
·
verified ·
1 Parent(s): 67d396f

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +56 -0
  2. bert_model_dir.zip +3 -0
  3. requirements.txt.txt +3 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ shutil.move(source_path, destination_path)
12
+
13
+ # Example usage:
14
+ # Path to your ZIP file which is your sentimetn analysis model zip
15
+ zip_file_path = 'bert_model_dir.zip'
16
+ # Destination folder for extraction
17
+ extraction_path = 'bert_model_sentiment_v1'
18
+
19
+ unzip_and_save(zip_file_path, extraction_path)
20
+
21
+ # Load the fine-tuned model and tokenizer
22
+ model_path = "bert_model_sentiment_v1/bert_model_dir"
23
+ tokenizer_path = "bert_model_sentiment_v1/bert_model_dir"
24
+
25
+ @st.cache_resource
26
+ def load_model():
27
+ model = DistilBertForSequenceClassification.from_pretrained(model_path)
28
+ tokenizer = DistilBertTokenizerFast.from_pretrained(tokenizer_path)
29
+ return model, tokenizer
30
+
31
+ model, tokenizer = load_model()
32
+
33
+ def predict_sentiment(text):
34
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
35
+ model.to(device)
36
+
37
+ tokenized = tokenizer(text, truncation=True, padding=True, return_tensors='pt').to(device)
38
+ outputs = model(**tokenized)
39
+
40
+ probs = F.softmax(outputs.logits, dim=-1)
41
+ preds = torch.argmax(outputs.logits, dim=-1).item()
42
+ probs_max = probs.max().detach().cpu().numpy()
43
+
44
+ prediction = "Positive" if preds == 1 else "Negative"
45
+ return prediction, probs_max * 100
46
+
47
+ st.title("Sentiment Analysis App")
48
+ text = st.text_area("Enter your text:")
49
+
50
+ if st.button("Predict Sentiment"):
51
+ if text:
52
+ sentiment, confidence = predict_sentiment(text)
53
+ st.write(f"Sentiment: {sentiment}")
54
+ st.write(f"Confidence: {confidence:.2f}%")
55
+ else:
56
+ 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:69a9189665594ab76b7d725fdbf5d04a86c7b07290cb0da8e700aff3c4af3b0b
3
+ size 247102214
requirements.txt.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ transformers
3
+ streamlit