-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
43 lines (36 loc) · 1.21 KB
/
Copy pathapp.py
File metadata and controls
43 lines (36 loc) · 1.21 KB
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
33
34
35
36
37
38
39
40
41
42
43
import os
import requests
from flask import Flask, render_template, request
from dotenv import load_dotenv
load_dotenv()
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
SUMMARIZATION_MODEL = "facebook/bart-large-cnn"
app = Flask(__name__)
def summarize_text(text):
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
payload = {"inputs": text, "parameters": {"min_length": 30, "max_length": 130}}
try:
resp = requests.post(
f"https://api-inference.huggingface.co/models/{SUMMARIZATION_MODEL}",
headers=headers,
json=payload,
timeout=30
)
resp.raise_for_status()
out = resp.json()
if isinstance(out, list) and out:
return out[0]["summary_text"]
return str(out)
except Exception as e:
return f"Hugging Face API inference failed: {e}"
@app.route("/", methods=["GET", "POST"])
def index():
summary = ""
text = ""
if request.method == "POST":
text = request.form.get("text")
if text:
summary = summarize_text(text)
return render_template("index.html", text=text, summary=summary)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=False)