Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +5 -0
- main.py +37 -0
- templates/index.html +148 -0
Dockerfile
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY . .
|
| 4 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 5 |
+
CMD ["python", "main.py"]
|
main.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
from transformers import pipeline, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
model_name = "sshleifer/distilbart-cnn-12-6"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
summarizer = pipeline("summarization", model=model_name)
|
| 9 |
+
|
| 10 |
+
@app.route("/")
|
| 11 |
+
def index():
|
| 12 |
+
return render_template("index.html")
|
| 13 |
+
|
| 14 |
+
@app.route("/summarize", methods=["POST"])
|
| 15 |
+
def summarize():
|
| 16 |
+
try:
|
| 17 |
+
data = request.get_json()
|
| 18 |
+
text = data.get("text", "").strip()
|
| 19 |
+
if not text:
|
| 20 |
+
return jsonify({"error": "No text provided"}), 400
|
| 21 |
+
|
| 22 |
+
input_tokens = tokenizer.encode(text, return_tensors="pt")
|
| 23 |
+
input_len = input_tokens.shape[1]
|
| 24 |
+
|
| 25 |
+
max_len = max(10, min(100, input_len // 2))
|
| 26 |
+
min_len = max(5, max_len // 2)
|
| 27 |
+
|
| 28 |
+
summary = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]['summary_text']
|
| 29 |
+
return jsonify({"summary": summary})
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return jsonify({"error": str(e)}), 500
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
app.run(debug=True)
|
| 37 |
+
|
templates/index.html
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<title>AI Text Summarizer</title>
|
| 6 |
+
<style>
|
| 7 |
+
body {
|
| 8 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 9 |
+
margin: 40px auto;
|
| 10 |
+
max-width: 600px;
|
| 11 |
+
padding: 0 20px;
|
| 12 |
+
background: #f5f5f5;
|
| 13 |
+
color: #222;
|
| 14 |
+
}
|
| 15 |
+
textarea {
|
| 16 |
+
width: 100%;
|
| 17 |
+
height: 150px;
|
| 18 |
+
padding: 12px;
|
| 19 |
+
border-radius: 6px;
|
| 20 |
+
border: 1px solid #ccc;
|
| 21 |
+
font-size: 1rem;
|
| 22 |
+
resize: vertical;
|
| 23 |
+
box-sizing: border-box;
|
| 24 |
+
}
|
| 25 |
+
button {
|
| 26 |
+
margin-top: 15px;
|
| 27 |
+
padding: 12px 24px;
|
| 28 |
+
font-size: 1.1rem;
|
| 29 |
+
border-radius: 6px;
|
| 30 |
+
border: none;
|
| 31 |
+
background-color: #0078d7;
|
| 32 |
+
color: white;
|
| 33 |
+
cursor: pointer;
|
| 34 |
+
transition: background-color 0.3s ease;
|
| 35 |
+
}
|
| 36 |
+
button:hover {
|
| 37 |
+
background-color: #005ea3;
|
| 38 |
+
}
|
| 39 |
+
.summary-container {
|
| 40 |
+
margin-top: 30px;
|
| 41 |
+
background: white;
|
| 42 |
+
border-radius: 8px;
|
| 43 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
| 44 |
+
padding: 20px;
|
| 45 |
+
font-size: 1.1rem;
|
| 46 |
+
line-height: 1.5;
|
| 47 |
+
white-space: pre-wrap;
|
| 48 |
+
position: relative;
|
| 49 |
+
}
|
| 50 |
+
/* Loader style */
|
| 51 |
+
#loader {
|
| 52 |
+
display: none;
|
| 53 |
+
margin: 20px auto;
|
| 54 |
+
border: 5px solid #f3f3f3;
|
| 55 |
+
border-top: 5px solid #0078d7;
|
| 56 |
+
border-radius: 50%;
|
| 57 |
+
width: 40px;
|
| 58 |
+
height: 40px;
|
| 59 |
+
animation: spin 1s linear infinite;
|
| 60 |
+
}
|
| 61 |
+
@keyframes spin {
|
| 62 |
+
0% { transform: rotate(0deg);}
|
| 63 |
+
100% { transform: rotate(360deg);}
|
| 64 |
+
}
|
| 65 |
+
/* Copy button */
|
| 66 |
+
#copy-btn {
|
| 67 |
+
position: absolute;
|
| 68 |
+
top: 10px;
|
| 69 |
+
right: 10px;
|
| 70 |
+
background: #0078d7;
|
| 71 |
+
color: white;
|
| 72 |
+
border: none;
|
| 73 |
+
padding: 6px 12px;
|
| 74 |
+
border-radius: 4px;
|
| 75 |
+
cursor: pointer;
|
| 76 |
+
font-size: 0.9rem;
|
| 77 |
+
transition: background-color 0.3s ease;
|
| 78 |
+
}
|
| 79 |
+
#copy-btn:hover {
|
| 80 |
+
background-color: #005ea3;
|
| 81 |
+
}
|
| 82 |
+
#copy-btn:active {
|
| 83 |
+
background-color: #004077;
|
| 84 |
+
}
|
| 85 |
+
</style>
|
| 86 |
+
</head>
|
| 87 |
+
<body>
|
| 88 |
+
<h1>AI Text Summarizer</h1>
|
| 89 |
+
<form id="summary-form">
|
| 90 |
+
<textarea name="text" id="text-input" placeholder="Paste your text here..." required></textarea>
|
| 91 |
+
<br />
|
| 92 |
+
<button type="submit">Summarize</button>
|
| 93 |
+
</form>
|
| 94 |
+
|
| 95 |
+
<div id="loader"></div>
|
| 96 |
+
|
| 97 |
+
<div id="summary" class="summary-container" style="display:none;">
|
| 98 |
+
<button id="copy-btn">Copy</button>
|
| 99 |
+
<h2>Summary:</h2>
|
| 100 |
+
<p id="summary-text"></p>
|
| 101 |
+
</div>
|
| 102 |
+
|
| 103 |
+
<script>
|
| 104 |
+
const form = document.getElementById('summary-form');
|
| 105 |
+
const loader = document.getElementById('loader');
|
| 106 |
+
const summaryContainer = document.getElementById('summary');
|
| 107 |
+
const summaryText = document.getElementById('summary-text');
|
| 108 |
+
const copyBtn = document.getElementById('copy-btn');
|
| 109 |
+
const textInput = document.getElementById('text-input');
|
| 110 |
+
|
| 111 |
+
form.addEventListener('submit', async (e) => {
|
| 112 |
+
e.preventDefault();
|
| 113 |
+
const text = textInput.value.trim();
|
| 114 |
+
if (!text) return;
|
| 115 |
+
|
| 116 |
+
loader.style.display = 'block';
|
| 117 |
+
summaryContainer.style.display = 'none';
|
| 118 |
+
|
| 119 |
+
try {
|
| 120 |
+
const response = await fetch('/summarize', {
|
| 121 |
+
method: 'POST',
|
| 122 |
+
headers: { 'Content-Type': 'application/json' },
|
| 123 |
+
body: JSON.stringify({ text }),
|
| 124 |
+
});
|
| 125 |
+
|
| 126 |
+
if (!response.ok) {
|
| 127 |
+
throw new Error('Failed to summarize text');
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
const data = await response.json();
|
| 131 |
+
summaryText.textContent = data.summary;
|
| 132 |
+
summaryContainer.style.display = 'block';
|
| 133 |
+
} catch (err) {
|
| 134 |
+
alert(err.message);
|
| 135 |
+
} finally {
|
| 136 |
+
loader.style.display = 'none';
|
| 137 |
+
}
|
| 138 |
+
});
|
| 139 |
+
|
| 140 |
+
copyBtn.addEventListener('click', () => {
|
| 141 |
+
navigator.clipboard.writeText(summaryText.textContent).then(() => {
|
| 142 |
+
copyBtn.textContent = 'Copied!';
|
| 143 |
+
setTimeout(() => (copyBtn.textContent = 'Copy'), 1500);
|
| 144 |
+
});
|
| 145 |
+
});
|
| 146 |
+
</script>
|
| 147 |
+
</body>
|
| 148 |
+
</html>
|