from flask import Flask, render_template, request, jsonify from transformers import AutoTokenizer, AutoModelForSeq2SeqLM import os app = Flask(__name__) # Load the fine-tuned model and tokenizer model_dir = "./finetune_model" # Replace with your model's directory tokenizer = AutoTokenizer.from_pretrained(model_dir) model = AutoModelForSeq2SeqLM.from_pretrained(model_dir) def generate_headline(article): """ Generate a headline from the given article using the fine-tuned model. """ inputs = tokenizer.encode(article, return_tensors="pt", max_length=512, truncation=True) outputs = model.generate(inputs, max_length=128, num_beams=5, early_stopping=True) headline = tokenizer.decode(outputs[0], skip_special_tokens=True) return headline @app.route("/", methods=["GET", "POST"]) def home(): headline = None if request.method == "POST": article = request.form.get("article") if article: headline = generate_headline(article) return render_template("index.html", headline=headline) if __name__ == "__main__": # Use dynamic port for compatibility port = int(os.environ.get("PORT", 5000)) # Default to 5000 if no PORT is set app.run(host="0.0.0.0", port=port, debug=False)