Rudrameher45 commited on
Commit
32fea1e
·
verified ·
1 Parent(s): eddb398

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -1,28 +1,23 @@
1
- from flask import Flask, request, jsonify, render_template
2
- from transformers import pipeline
3
 
4
- # Initialize Flask app
5
  app = Flask(__name__)
6
 
7
- # Load the fine-tuned model
8
- model_path = "./finetune_model"
9
- headline_generator = pipeline("text2text-generation", model=model_path)
10
 
11
  @app.route('/')
12
- def index():
13
  return render_template('index.html')
14
 
15
- @app.route('/predict', methods=['POST'])
16
- def predict():
17
- article = request.form.get('article')
18
- if not article:
19
- return jsonify({"error": "No article provided"}), 400
20
- try:
21
- prediction = headline_generator(article, max_length=50, num_return_sequences=1)
22
- headline = prediction[0]['generated_text']
23
- return jsonify({"headline": headline})
24
- except Exception as e:
25
- return jsonify({"error": str(e)}), 500
26
 
27
- if __name__ == "__main__":
28
- app.run(debug=True)
 
1
+ from flask import Flask, request, render_template
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
 
4
  app = Flask(__name__)
5
 
6
+ # Load the fine-tuned model and tokenizer
7
+ tokenizer = AutoTokenizer.from_pretrained("path/to/finetune_model")
8
+ model = AutoModelForSeq2SeqLM.from_pretrained("path/to/finetune_model")
9
 
10
  @app.route('/')
11
+ def home():
12
  return render_template('index.html')
13
 
14
+ @app.route('/summarize', methods=['POST'])
15
+ def summarize():
16
+ article = request.form['article']
17
+ inputs = tokenizer.encode(article, return_tensors="pt", truncation=True)
18
+ summary_ids = model.generate(inputs, max_length=50, min_length=15, length_penalty=2.0, num_beams=4, early_stopping=True)
19
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
20
+ return render_template('index.html', summary=summary)
 
 
 
 
21
 
22
+ if __name__ == '__main__':
23
+ app.run(debug=True)