Spaces:
Sleeping
Sleeping
Vargock
commited on
Commit
·
2691522
0
Parent(s):
Initial commit
Browse files- .gitignore +1 -0
- app.py +43 -0
- requirements.txt +3 -0
- templates/index.html +47 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
/.venv
|
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from flask import Flask, render_template, request
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 9 |
+
SUMMARIZATION_MODEL = "facebook/bart-large-cnn"
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
|
| 13 |
+
def summarize_text(text):
|
| 14 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 15 |
+
payload = {"inputs": text, "parameters": {"min_length": 30, "max_length": 130}}
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
resp = requests.post(
|
| 19 |
+
f"https://api-inference.huggingface.co/models/{SUMMARIZATION_MODEL}",
|
| 20 |
+
headers=headers,
|
| 21 |
+
json=payload,
|
| 22 |
+
timeout=30
|
| 23 |
+
)
|
| 24 |
+
resp.raise_for_status()
|
| 25 |
+
out = resp.json()
|
| 26 |
+
if isinstance(out, list) and out:
|
| 27 |
+
return out[0]["summary_text"]
|
| 28 |
+
return str(out)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Hugging Face API inference failed: {e}"
|
| 31 |
+
|
| 32 |
+
@app.route("/", methods=["GET", "POST"])
|
| 33 |
+
def index():
|
| 34 |
+
summary = ""
|
| 35 |
+
text = ""
|
| 36 |
+
if request.method == "POST":
|
| 37 |
+
text = request.form.get("text")
|
| 38 |
+
if text:
|
| 39 |
+
summary = summarize_text(text)
|
| 40 |
+
return render_template("index.html", text=text, summary=summary)
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
requests
|
| 3 |
+
python-dotenv
|
templates/index.html
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
|
| 4 |
+
<head>
|
| 5 |
+
<meta charset="UTF-8">
|
| 6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 7 |
+
<title>Text Summarizer</title>
|
| 8 |
+
<style>
|
| 9 |
+
body {
|
| 10 |
+
font-family: Arial, sans-serif;
|
| 11 |
+
margin: 50px;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
textarea {
|
| 15 |
+
width: 100%;
|
| 16 |
+
height: 200px;
|
| 17 |
+
margin-bottom: 20px;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
input[type="submit"] {
|
| 21 |
+
padding: 10px 20px;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
.summary {
|
| 25 |
+
background: #f0f0f0;
|
| 26 |
+
padding: 20px;
|
| 27 |
+
margin-top: 20px;
|
| 28 |
+
}
|
| 29 |
+
</style>
|
| 30 |
+
</head>
|
| 31 |
+
|
| 32 |
+
<body>
|
| 33 |
+
<h1>Text Summarizer</h1>
|
| 34 |
+
<form method="POST">
|
| 35 |
+
<textarea name="text" placeholder="Paste your text here">{{ text }}</textarea><br>
|
| 36 |
+
<input type="submit" value="Summarize">
|
| 37 |
+
</form>
|
| 38 |
+
|
| 39 |
+
{% if summary %}
|
| 40 |
+
<div class="summary">
|
| 41 |
+
<h3>Summary:</h3>
|
| 42 |
+
<p>{{ summary }}</p>
|
| 43 |
+
</div>
|
| 44 |
+
{% endif %}
|
| 45 |
+
</body>
|
| 46 |
+
|
| 47 |
+
</html>
|