Upload backend/app/main copy.py with huggingface_hub
Browse files- backend/app/main copy.py +148 -0
backend/app/main copy.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import BertTokenizer
|
| 5 |
+
from train import YelpReviewClassifier
|
| 6 |
+
|
| 7 |
+
# Initialize Flask app
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
CORS(app) # Enable CORS for cross-origin requests
|
| 10 |
+
|
| 11 |
+
# Load Model
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
print(f"Using device: {device}")
|
| 14 |
+
|
| 15 |
+
model = YelpReviewClassifier().to(device)
|
| 16 |
+
model.load_state_dict(torch.load("models/model.pth", map_location=device))
|
| 17 |
+
model.eval()
|
| 18 |
+
|
| 19 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 20 |
+
|
| 21 |
+
@app.route("/")
|
| 22 |
+
def home():
|
| 23 |
+
return jsonify({"message": "Welcome to Yelp Review AI Predictor!"})
|
| 24 |
+
|
| 25 |
+
@app.route("/predict", methods=["POST"])
|
| 26 |
+
def predict_rating():
|
| 27 |
+
try:
|
| 28 |
+
# Get JSON data from request
|
| 29 |
+
data = request.get_json()
|
| 30 |
+
|
| 31 |
+
if not data:
|
| 32 |
+
return jsonify({"error": "No input data provided"}), 400
|
| 33 |
+
|
| 34 |
+
text = data.get("text", "").strip()
|
| 35 |
+
useful = data.get("useful", 0.0)
|
| 36 |
+
|
| 37 |
+
# Ensure text is provided
|
| 38 |
+
if not text:
|
| 39 |
+
return jsonify({"error": "Missing 'text' field"}), 400
|
| 40 |
+
|
| 41 |
+
# Ensure useful is a number
|
| 42 |
+
try:
|
| 43 |
+
useful = float(useful)
|
| 44 |
+
except ValueError:
|
| 45 |
+
return jsonify({"error": "'useful' must be a numeric value"}), 400
|
| 46 |
+
|
| 47 |
+
# Tokenize input
|
| 48 |
+
tokens = tokenizer(
|
| 49 |
+
text, truncation=True, padding="max_length", max_length=256, return_tensors="pt"
|
| 50 |
+
)
|
| 51 |
+
input_ids = tokens["input_ids"].to(device)
|
| 52 |
+
attention_mask = tokens["attention_mask"].to(device)
|
| 53 |
+
useful_tensor = torch.tensor([useful], dtype=torch.float32).to(device)
|
| 54 |
+
|
| 55 |
+
# Make prediction
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
output = model(input_ids, attention_mask, useful_tensor)
|
| 58 |
+
predicted_stars = torch.argmax(output, dim=1).item() + 1 # Convert to 1-5 stars
|
| 59 |
+
|
| 60 |
+
# **Return predicted stars & usefulness score**
|
| 61 |
+
return jsonify({
|
| 62 |
+
"predicted_stars": predicted_stars,
|
| 63 |
+
"usefulness_score": useful # ✅ Add usefulness score in response
|
| 64 |
+
})
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return jsonify({"error": str(e)}), 500
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
# Get JSON data from request
|
| 71 |
+
data = request.get_json()
|
| 72 |
+
|
| 73 |
+
if not data:
|
| 74 |
+
return jsonify({"error": "No input data provided"}), 400
|
| 75 |
+
|
| 76 |
+
text = data.get("text", "").strip()
|
| 77 |
+
useful = data.get("useful", 0.0)
|
| 78 |
+
|
| 79 |
+
# Ensure text is provided
|
| 80 |
+
if not text:
|
| 81 |
+
return jsonify({"error": "Missing 'text' field"}), 400
|
| 82 |
+
|
| 83 |
+
# Ensure useful is a number
|
| 84 |
+
try:
|
| 85 |
+
useful = float(useful)
|
| 86 |
+
if not (0 <= useful <= 1): # Ensure it's between 0 and 1
|
| 87 |
+
return jsonify({"error": "'useful' must be between 0 and 1"}), 400
|
| 88 |
+
except ValueError:
|
| 89 |
+
return jsonify({"error": "'useful' must be a numeric value"}), 400
|
| 90 |
+
|
| 91 |
+
# Tokenize input
|
| 92 |
+
tokens = tokenizer(
|
| 93 |
+
text, truncation=True, padding="max_length", max_length=256, return_tensors="pt"
|
| 94 |
+
)
|
| 95 |
+
input_ids = tokens["input_ids"].to(device)
|
| 96 |
+
attention_mask = tokens["attention_mask"].to(device)
|
| 97 |
+
useful_tensor = torch.tensor([useful], dtype=torch.float32).to(device)
|
| 98 |
+
|
| 99 |
+
# Make prediction
|
| 100 |
+
with torch.no_grad():
|
| 101 |
+
output = model(input_ids, attention_mask, useful_tensor)
|
| 102 |
+
predicted_stars = torch.argmax(output, dim=1).item() + 1 # Convert to 1-5 stars
|
| 103 |
+
|
| 104 |
+
return jsonify({"predicted_stars": predicted_stars})
|
| 105 |
+
|
| 106 |
+
except Exception as e:
|
| 107 |
+
return jsonify({"error": str(e)}), 500
|
| 108 |
+
|
| 109 |
+
try:
|
| 110 |
+
# Get JSON data from request
|
| 111 |
+
data = request.get_json()
|
| 112 |
+
|
| 113 |
+
if not data:
|
| 114 |
+
return jsonify({"error": "No input data provided"}), 400
|
| 115 |
+
|
| 116 |
+
text = data.get("text", "").strip()
|
| 117 |
+
useful = data.get("useful", 0.0)
|
| 118 |
+
|
| 119 |
+
# Ensure text is provided
|
| 120 |
+
if not text:
|
| 121 |
+
return jsonify({"error": "Missing 'text' field"}), 400
|
| 122 |
+
|
| 123 |
+
# Ensure useful is a number
|
| 124 |
+
try:
|
| 125 |
+
useful = float(useful)
|
| 126 |
+
except ValueError:
|
| 127 |
+
return jsonify({"error": "'useful' must be a numeric value"}), 400
|
| 128 |
+
|
| 129 |
+
# Tokenize input
|
| 130 |
+
tokens = tokenizer(
|
| 131 |
+
text, truncation=True, padding="max_length", max_length=256, return_tensors="pt"
|
| 132 |
+
)
|
| 133 |
+
input_ids = tokens["input_ids"].to(device)
|
| 134 |
+
attention_mask = tokens["attention_mask"].to(device)
|
| 135 |
+
useful_tensor = torch.tensor([useful], dtype=torch.float32).to(device)
|
| 136 |
+
|
| 137 |
+
# Make prediction
|
| 138 |
+
with torch.no_grad():
|
| 139 |
+
output = model(input_ids, attention_mask, useful_tensor)
|
| 140 |
+
predicted_stars = torch.argmax(output, dim=1).item() + 1 # Convert to 1-5 stars
|
| 141 |
+
|
| 142 |
+
return jsonify({"predicted_stars": predicted_stars})
|
| 143 |
+
|
| 144 |
+
except Exception as e:
|
| 145 |
+
return jsonify({"error": str(e)}), 500
|
| 146 |
+
|
| 147 |
+
if __name__ == "__main__":
|
| 148 |
+
app.run(host="0.0.0.0", port=8000, debug=True) # Listen on all interfaces
|