Delete main.py
Browse files
main.py
DELETED
@@ -1,91 +0,0 @@
|
|
1 |
-
from flask import Flask, request, render_template
|
2 |
-
import pandas as pd
|
3 |
-
import torch
|
4 |
-
from transformers import BertTokenizer, BertForSequenceClassification
|
5 |
-
from collections import Counter
|
6 |
-
import matplotlib
|
7 |
-
matplotlib.use('Agg') # Set the backend before importing pyplot
|
8 |
-
import matplotlib.pyplot as plt
|
9 |
-
import base64
|
10 |
-
from io import BytesIO
|
11 |
-
import os
|
12 |
-
|
13 |
-
app = Flask(__name__)
|
14 |
-
|
15 |
-
# Load Model - Check if local model exists; otherwise, load from Hugging Face
|
16 |
-
MODEL_PATH = "bert_imdb_model.bin"
|
17 |
-
MODEL_HF_REPO = "philipobiorah/bert-imdb-model" # Replace with your Hugging Face model repo
|
18 |
-
|
19 |
-
if os.path.exists(MODEL_PATH):
|
20 |
-
print("Loading model from local file...")
|
21 |
-
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
|
22 |
-
model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device('cpu')))
|
23 |
-
else:
|
24 |
-
print("Loading model from Hugging Face Hub...")
|
25 |
-
model = BertForSequenceClassification.from_pretrained(MODEL_HF_REPO)
|
26 |
-
|
27 |
-
model.eval()
|
28 |
-
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
29 |
-
|
30 |
-
def predict_sentiment(text):
|
31 |
-
# Tokenize and split into chunks
|
32 |
-
tokens = tokenizer.encode(text, add_special_tokens=True)
|
33 |
-
chunks = [tokens[i:i + 512] for i in range(0, len(tokens), 512)]
|
34 |
-
|
35 |
-
# Predict sentiment for each chunk
|
36 |
-
sentiments = []
|
37 |
-
for chunk in chunks:
|
38 |
-
inputs = tokenizer.decode(chunk, skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
39 |
-
inputs = tokenizer(inputs, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
40 |
-
|
41 |
-
with torch.no_grad():
|
42 |
-
outputs = model(**inputs)
|
43 |
-
|
44 |
-
sentiments.append(outputs.logits.argmax(dim=1).item())
|
45 |
-
|
46 |
-
# Aggregate sentiment results (majority voting)
|
47 |
-
majority_sentiment = Counter(sentiments).most_common(1)[0][0]
|
48 |
-
return 'Positive' if majority_sentiment == 1 else 'Negative'
|
49 |
-
|
50 |
-
@app.route('/')
|
51 |
-
def upload_file():
|
52 |
-
return render_template('upload.html')
|
53 |
-
|
54 |
-
@app.route('/analyze_text', methods=['POST'])
|
55 |
-
def analyze_text():
|
56 |
-
text = request.form['text']
|
57 |
-
sentiment = predict_sentiment(text)
|
58 |
-
return render_template('upload.html', sentiment=sentiment)
|
59 |
-
|
60 |
-
@app.route('/uploader', methods=['GET', 'POST'])
|
61 |
-
def upload_file_post():
|
62 |
-
if request.method == 'POST':
|
63 |
-
f = request.files['file']
|
64 |
-
data = pd.read_csv(f)
|
65 |
-
|
66 |
-
# Predict sentiment for each review
|
67 |
-
data['sentiment'] = data['review'].apply(predict_sentiment)
|
68 |
-
|
69 |
-
# Sentiment Analysis Summary
|
70 |
-
sentiment_counts = data['sentiment'].value_counts().to_dict()
|
71 |
-
summary = f"Total Reviews: {len(data)}<br>" \
|
72 |
-
f"Positive: {sentiment_counts.get('Positive', 0)}<br>" \
|
73 |
-
f"Negative: {sentiment_counts.get('Negative', 0)}<br>"
|
74 |
-
|
75 |
-
# Generate bar chart
|
76 |
-
fig, ax = plt.subplots()
|
77 |
-
ax.bar(sentiment_counts.keys(), sentiment_counts.values(), color=['red', 'blue'])
|
78 |
-
ax.set_ylabel('Counts')
|
79 |
-
ax.set_title('Sentiment Analysis Summary')
|
80 |
-
|
81 |
-
# Convert plot to base64 for embedding
|
82 |
-
img = BytesIO()
|
83 |
-
plt.savefig(img, format='png', bbox_inches='tight')
|
84 |
-
img.seek(0)
|
85 |
-
plot_url = base64.b64encode(img.getvalue()).decode('utf8')
|
86 |
-
plt.close(fig)
|
87 |
-
|
88 |
-
return render_template('result.html', tables=[data.to_html(classes='data')], titles=data.columns.values, summary=summary, plot_url=plot_url)
|
89 |
-
|
90 |
-
if __name__ == '__main__':
|
91 |
-
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|