| from flask import Flask, request, jsonify
|
| from flask_cors import CORS
|
| import torch
|
| import torch.nn as nn
|
| from transformers import BertModel, BertTokenizer
|
| import os
|
|
|
|
|
| app = Flask(__name__)
|
| CORS(app)
|
|
|
|
|
| device = "cuda" if torch.cuda.is_available() else "cpu"
|
| print(f"Using device: {device}")
|
|
|
|
|
| class YelpReviewClassifier(nn.Module):
|
| def __init__(self):
|
| super(YelpReviewClassifier, self).__init__()
|
| self.bert = BertModel.from_pretrained("bert-base-uncased")
|
|
|
| self.fc_text = nn.Linear(768, 128)
|
| self.fc_stars = nn.Linear(128, 5)
|
| self.fc_useful = nn.Linear(128, 1)
|
|
|
| def forward(self, input_ids, attention_mask):
|
| text_output = self.bert(input_ids=input_ids, attention_mask=attention_mask).pooler_output
|
| text_features = self.fc_text(text_output)
|
|
|
| stars_logits = self.fc_stars(text_features)
|
| usefulness = self.fc_useful(text_features).squeeze()
|
|
|
|
|
| usefulness = torch.round(torch.clamp(usefulness, 1, 5)).int()
|
|
|
| return stars_logits, usefulness
|
|
|
|
|
| model_path = os.path.join(os.path.dirname(__file__), "models", "star_model.pth")
|
| model = YelpReviewClassifier().to(device)
|
|
|
|
|
| try:
|
| model.load_state_dict(torch.load(model_path, map_location=device), strict=False)
|
| model.eval()
|
| print("β
Model loaded successfully!")
|
| except Exception as e:
|
| print(f"β Error loading model: {e}")
|
|
|
|
|
| tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
|
|
|
|
| @app.route("/")
|
| def home():
|
| return jsonify({"message": "Welcome to Yelp Review AI Predictor!"})
|
|
|
| @app.route("/predict", methods=["POST"])
|
| def predict_rating():
|
| try:
|
| data = request.get_json()
|
|
|
| if not data:
|
| return jsonify({"error": "No input data provided"}), 400
|
|
|
| text = data.get("text", "").strip()
|
| if not text:
|
| return jsonify({"error": "Missing 'text' field"}), 400
|
|
|
|
|
| tokens = tokenizer(
|
| text, truncation=True, padding="max_length", max_length=256, return_tensors="pt"
|
| )
|
| input_ids = tokens["input_ids"].to(device)
|
| attention_mask = tokens["attention_mask"].to(device)
|
|
|
|
|
| with torch.no_grad():
|
| stars_logits, usefulness = model(input_ids, attention_mask)
|
| predicted_stars = torch.argmax(stars_logits, dim=1).item() + 1
|
|
|
| return jsonify({
|
| "predicted_stars": predicted_stars,
|
| "usefulness_score": usefulness.item()
|
| })
|
|
|
| except Exception as e:
|
| return jsonify({"error": str(e)}), 500
|
|
|
| if __name__ == "__main__":
|
| app.run(host="0.0.0.0", port=8000, debug=True)
|
|
|