Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,37 +1,39 @@
|
|
| 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 |
-
return jsonify({"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ['TRANSFORMERS_CACHE'] = '/tmp'
|
| 3 |
+
from flask import Flask, render_template, request, jsonify
|
| 4 |
+
from transformers import pipeline, AutoTokenizer
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
model_name = "sshleifer/distilbart-cnn-12-6"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
summarizer = pipeline("summarization", model=model_name)
|
| 11 |
+
|
| 12 |
+
@app.route("/")
|
| 13 |
+
def index():
|
| 14 |
+
return render_template("index.html")
|
| 15 |
+
|
| 16 |
+
@app.route("/summarize", methods=["POST"])
|
| 17 |
+
def summarize():
|
| 18 |
+
try:
|
| 19 |
+
data = request.get_json()
|
| 20 |
+
text = data.get("text", "").strip()
|
| 21 |
+
if not text:
|
| 22 |
+
return jsonify({"error": "No text provided"}), 400
|
| 23 |
+
|
| 24 |
+
input_tokens = tokenizer.encode(text, return_tensors="pt")
|
| 25 |
+
input_len = input_tokens.shape[1]
|
| 26 |
+
|
| 27 |
+
max_len = max(10, min(100, input_len // 2))
|
| 28 |
+
min_len = max(5, max_len // 2)
|
| 29 |
+
|
| 30 |
+
summary = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]['summary_text']
|
| 31 |
+
return jsonify({"summary": summary})
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return jsonify({"error": str(e)}), 500
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
app.run(debug=True)
|
| 39 |
+
|