Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,12 @@ import torch
|
|
| 4 |
from transformers import BertTokenizerFast, BertForSequenceClassification
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
CORS(app)
|
| 9 |
|
| 10 |
# Set the device
|
| 11 |
-
device = "cpu"
|
| 12 |
-
# But for safety and simplicity on free tier, let's stick to CPU or auto-detect but expect CPU.
|
| 13 |
if torch.cuda.is_available():
|
| 14 |
device = "cuda"
|
| 15 |
elif torch.backends.mps.is_available():
|
|
@@ -20,11 +20,6 @@ else:
|
|
| 20 |
print(f"Using device: {device}")
|
| 21 |
|
| 22 |
# Load the saved model and tokenizer
|
| 23 |
-
# In HF Spaces, we will upload the model files to the root of the repository or a subfolder.
|
| 24 |
-
# Let's assume the user uploads the contents of 'saved_bert_sentiment_model3final' to the root of the Space.
|
| 25 |
-
# So the path is just "." or specific folder if they upload the folder itself.
|
| 26 |
-
# To be safe and organized, let's assume they upload the folder 'model' or just files to root.
|
| 27 |
-
# Best practice for manual upload: Upload files to root.
|
| 28 |
MODEL_PATH = "."
|
| 29 |
|
| 30 |
try:
|
|
@@ -36,11 +31,16 @@ try:
|
|
| 36 |
print("Model loaded successfully!")
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Error loading model: {e}")
|
| 39 |
-
# In a Space, if model fails, the app might crash or just not work.
|
| 40 |
|
| 41 |
@app.route('/')
|
| 42 |
def home():
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
@app.route('/predict', methods=['POST'])
|
| 46 |
def predict():
|
|
@@ -65,10 +65,8 @@ def predict():
|
|
| 65 |
logits = loaded_model(input_ids, attention_mask=attention_mask).logits
|
| 66 |
|
| 67 |
prediction = torch.argmax(logits, dim=1).item()
|
| 68 |
-
# 1 is Positive, 0 is Negative based on the notebook
|
| 69 |
sentiment = "Positive" if prediction == 1 else "Negative"
|
| 70 |
|
| 71 |
-
# Calculate confidence (softmax)
|
| 72 |
probs = torch.nn.functional.softmax(logits, dim=1)
|
| 73 |
confidence = probs[0][prediction].item()
|
| 74 |
|
|
@@ -81,5 +79,4 @@ def predict():
|
|
| 81 |
return jsonify({'error': str(e)}), 500
|
| 82 |
|
| 83 |
if __name__ == '__main__':
|
| 84 |
-
# HF Spaces expects the app to run on port 7860
|
| 85 |
app.run(debug=False, host='0.0.0.0', port=7860)
|
|
|
|
| 4 |
from transformers import BertTokenizerFast, BertForSequenceClassification
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Configure Flask to serve static files from 'dist' folder
|
| 8 |
+
app = Flask(__name__, static_folder='dist', static_url_path='/')
|
| 9 |
CORS(app)
|
| 10 |
|
| 11 |
# Set the device
|
| 12 |
+
device = "cpu"
|
|
|
|
| 13 |
if torch.cuda.is_available():
|
| 14 |
device = "cuda"
|
| 15 |
elif torch.backends.mps.is_available():
|
|
|
|
| 20 |
print(f"Using device: {device}")
|
| 21 |
|
| 22 |
# Load the saved model and tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
MODEL_PATH = "."
|
| 24 |
|
| 25 |
try:
|
|
|
|
| 31 |
print("Model loaded successfully!")
|
| 32 |
except Exception as e:
|
| 33 |
print(f"Error loading model: {e}")
|
|
|
|
| 34 |
|
| 35 |
@app.route('/')
|
| 36 |
def home():
|
| 37 |
+
# Serve the React app
|
| 38 |
+
return app.send_static_file('index.html')
|
| 39 |
+
|
| 40 |
+
@app.route('/<path:path>')
|
| 41 |
+
def static_proxy(path):
|
| 42 |
+
# Serve other static files (JS, CSS, etc.)
|
| 43 |
+
return app.send_static_file(path)
|
| 44 |
|
| 45 |
@app.route('/predict', methods=['POST'])
|
| 46 |
def predict():
|
|
|
|
| 65 |
logits = loaded_model(input_ids, attention_mask=attention_mask).logits
|
| 66 |
|
| 67 |
prediction = torch.argmax(logits, dim=1).item()
|
|
|
|
| 68 |
sentiment = "Positive" if prediction == 1 else "Negative"
|
| 69 |
|
|
|
|
| 70 |
probs = torch.nn.functional.softmax(logits, dim=1)
|
| 71 |
confidence = probs[0][prediction].item()
|
| 72 |
|
|
|
|
| 79 |
return jsonify({'error': str(e)}), 500
|
| 80 |
|
| 81 |
if __name__ == '__main__':
|
|
|
|
| 82 |
app.run(debug=False, host='0.0.0.0', port=7860)
|