dimoZ commited on
Commit
c5cf788
·
verified ·
1 Parent(s): c0a125a

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -67
app.py DELETED
@@ -1,67 +0,0 @@
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.")