-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
32 lines (25 loc) · 994 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from flask import Flask, request, jsonify
from flask_cors import CORS
from transformers import pipeline
app = Flask(__name__)
CORS(app)
# Load the summarization pipeline
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
@app.route('/summarize', methods=['POST'])
def summarize():
data = request.json
text = data.get('text')
# Check if text is empty or too short
if not text or len(text.strip()) < 10: # Minimum 10 characters
return jsonify({"error": "Input text is too short or empty. Please provide at least 10 characters."}), 400
try:
# Summarize the text using Hugging Face
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
return jsonify({"summary": summary[0]['summary_text']})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/')
def home():
return "AI Research Helper Backend is running!"
if __name__ == '__main__':
app.run(debug=True)