-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
63 lines (50 loc) · 1.95 KB
/
app.py
File metadata and controls
63 lines (50 loc) · 1.95 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
from flask import Flask, render_template, request
from bs4 import BeautifulSoup
from dotenv import load_dotenv
import requests
load_dotenv() # Load environment variables from .env file
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def Index():
return render_template("index.html")
@app.route("/Summarize", methods=["GET", "POST"])
def Summarize():
if request.method == "POST":
if 'url' in request.form:
url = request.form['url']
try:
response = requests.get(url)
response.raise_for_status()
html_content = response.text
soup = BeautifulSoup(html_content, 'html.parser')
data = ' '.join([p.get_text() for p in soup.find_all('p')])
except Exception as e:
return render_template("index.html", error=str(e))
else:
data = request.form["data"]
API_URL = "https://api-inference.huggingface.co/models/Falconsai/text_summarization"
headers = {
"Authorization": f"{os.getenv('API_KEY')}"
}
max_length = int(request.form["maxL"])
min_length = max_length // 4
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
output = query({
"inputs": data,
"parameters": {"min_length": min_length, "max_length": max_length},
})
# Check if output is in the expected format
if isinstance(output, list) and output:
summary_text = output[0].get("summary_text", "")
return render_template("index.html", result=summary_text)
else:
error_message = "Failed to generate summary. Please try again."
return render_template("index.html", error=error_message)
else:
return render_template("index.html")
if __name__ == "__main__":
app.debug = True
app.run()