dimoZ commited on
Commit
f521b90
·
verified ·
1 Parent(s): dc03271

Update app.py

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