| from flask import Flask, request, jsonify
|
| from flask_cors import CORS
|
| import torch
|
| import os
|
| from transformers import BertTokenizer, BertModel, AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
| app = Flask(__name__)
|
| CORS(app)
|
|
|
|
|
| device = "cuda" if torch.cuda.is_available() else "cpu"
|
| print(f"Using device: {device}")
|
|
|
|
|
| tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
|
|
|
|
| models = {}
|
|
|
| def load_model(model_name, class_definition):
|
| """Load model if it exists, otherwise return None."""
|
| model_path = f"models/{model_name}.pth"
|
| if os.path.exists(model_path):
|
| model = class_definition().to(device)
|
| model.load_state_dict(torch.load(model_path, map_location=device))
|
| model.eval()
|
| return model
|
| else:
|
| print(f"β οΈ {model_name} model not found!")
|
| return None
|
|
|
|
|
| class StarRatingModel(torch.nn.Module):
|
| def __init__(self):
|
| super(StarRatingModel, self).__init__()
|
| self.bert = BertModel.from_pretrained("bert-base-uncased")
|
| self.fc_stars = torch.nn.Linear(768, 5)
|
|
|
| def forward(self, input_ids, attention_mask):
|
| text_output = self.bert(input_ids=input_ids, attention_mask=attention_mask).pooler_output
|
| return self.fc_stars(text_output)
|
|
|
| models["star_model"] = load_model("star_rating_model", StarRatingModel)
|
|
|
|
|
| class UsefulnessModel(torch.nn.Module):
|
| def __init__(self):
|
| super(UsefulnessModel, self).__init__()
|
| self.bert = BertModel.from_pretrained("bert-base-uncased")
|
| self.fc_useful = torch.nn.Linear(768, 1)
|
|
|
| def forward(self, input_ids, attention_mask):
|
| text_output = self.bert(input_ids=input_ids, attention_mask=attention_mask).pooler_output
|
| return torch.clamp(torch.round(self.fc_useful(text_output)), 1, 5)
|
|
|
| models["usefulness_model"] = load_model("usefulness_model", UsefulnessModel)
|
|
|
|
|
| chatbot_model_name = "facebook/opt-1.3b"
|
| if os.path.exists("models/response_model.pth"):
|
| chatbot_tokenizer = AutoTokenizer.from_pretrained(chatbot_model_name)
|
| chatbot_model = AutoModelForCausalLM.from_pretrained(chatbot_model_name).to(device)
|
| chatbot_model.load_state_dict(torch.load("models/response_model.pth", map_location=device))
|
| chatbot_model.eval()
|
| models["response_model"] = chatbot_model
|
| else:
|
| print("β οΈ Response model not found!")
|
|
|
| @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)
|
|
|
|
|
| results = {}
|
|
|
|
|
| if models["star_model"]:
|
| with torch.no_grad():
|
| stars_logits = models["star_model"](input_ids, attention_mask)
|
| predicted_stars = torch.argmax(stars_logits, dim=1).item() + 1
|
| results["predicted_stars"] = predicted_stars
|
| else:
|
| results["predicted_stars"] = "AI has not been trained and created"
|
|
|
|
|
| if models["usefulness_model"]:
|
| with torch.no_grad():
|
| predicted_usefulness = models["usefulness_model"](input_ids, attention_mask).item()
|
| results["predicted_usefulness"] = int(predicted_usefulness)
|
| else:
|
| results["predicted_usefulness"] = "AI has not been trained and created"
|
|
|
|
|
| if "response_model" in models and results["predicted_usefulness"] != "AI has not been trained and created":
|
| if results["predicted_usefulness"] > 1:
|
| response_input = chatbot_tokenizer(text, return_tensors="pt").to(device)
|
| response_output = models["response_model"].generate(**response_input, max_length=50)
|
| ai_response = chatbot_tokenizer.decode(response_output[0], skip_special_tokens=True)
|
| else:
|
| ai_response = "No response (low usefulness)."
|
| results["ai_response"] = ai_response
|
| else:
|
| results["ai_response"] = "AI has not been trained and created"
|
|
|
| return jsonify(results)
|
|
|
| except Exception as e:
|
| return jsonify({"error": str(e)}), 500
|
|
|
| if __name__ == "__main__":
|
| app.run(host="0.0.0.0", port=8000, debug=True)
|
|
|