Manasi-2 commited on
Commit
2fa735b
·
verified ·
1 Parent(s): 8c3acfd

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirement.txt +0 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")
requirement.txt ADDED
File without changes