Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +72 -0
- index.html +131 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from pydantic import BaseModel # validate text input
|
| 3 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 4 |
+
import torch
|
| 5 |
+
import re
|
| 6 |
+
from fastapi.templating import Jinja2Templates # UI
|
| 7 |
+
from fastapi.responses import HTMLResponse
|
| 8 |
+
from fastapi.staticfiles import StaticFiles
|
| 9 |
+
|
| 10 |
+
app = FastAPI(title="Text Summarizer App", description="Text Summarization using T5", version="1.0")
|
| 11 |
+
|
| 12 |
+
model = T5ForConditionalGeneration.from_pretrained("C:\\Users\\india\\Desktop\\Text_Summarizer\\saved_model")
|
| 13 |
+
tokenizer = T5Tokenizer.from_pretrained("C:\\Users\\india\\Desktop\\Text_Summarizer\\saved_model")
|
| 14 |
+
|
| 15 |
+
# device
|
| 16 |
+
if torch.backends.mps.is_available():
|
| 17 |
+
device = torch.device("mps")
|
| 18 |
+
elif torch.cuda.is_available():
|
| 19 |
+
device = torch.device("cuda")
|
| 20 |
+
else:
|
| 21 |
+
device = torch.device("cpu")
|
| 22 |
+
|
| 23 |
+
model.to(device)
|
| 24 |
+
|
| 25 |
+
templates = Jinja2Templates(directory=".")
|
| 26 |
+
|
| 27 |
+
class DialogueInput(BaseModel): # input scheama for dialogue , list exists.
|
| 28 |
+
dialogue: str
|
| 29 |
+
|
| 30 |
+
def clean_data(text):
|
| 31 |
+
text = re.sub(r"\r\n", " ", text) # lines
|
| 32 |
+
text = re.sub(r"\s+", " ", text) # spaces
|
| 33 |
+
text = re.sub(r"<.*?>", " ", text) # html tags <p> <h1>
|
| 34 |
+
text = text.strip().lower()
|
| 35 |
+
return text
|
| 36 |
+
|
| 37 |
+
def summarize_dialogue(dialogue : str) -> str:
|
| 38 |
+
dialogue = clean_data(dialogue) # clean
|
| 39 |
+
|
| 40 |
+
# tokenize
|
| 41 |
+
inputs = tokenizer(
|
| 42 |
+
dialogue,
|
| 43 |
+
padding="max_length",
|
| 44 |
+
max_length=512,
|
| 45 |
+
truncation=True,
|
| 46 |
+
return_tensors="pt"
|
| 47 |
+
).to(device)
|
| 48 |
+
|
| 49 |
+
# generate the summary => token ids
|
| 50 |
+
model.to(device)
|
| 51 |
+
targets = model.generate(
|
| 52 |
+
input_ids=inputs["input_ids"],
|
| 53 |
+
attention_mask=inputs["attention_mask"],
|
| 54 |
+
max_length=150,
|
| 55 |
+
num_beams=4,
|
| 56 |
+
early_stopping=True
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# decoded our output
|
| 60 |
+
summary = tokenizer.decode(targets[0], skip_special_tokens=True) # EOS, SEP
|
| 61 |
+
return summary
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# API endpoints
|
| 65 |
+
@app.post("/summarize/")
|
| 66 |
+
async def summarize(dialogue_input: DialogueInput): # dialogue_input type - DialogueInput
|
| 67 |
+
summary = summarize_dialogue(dialogue_input.dialogue)
|
| 68 |
+
return {"summary": summary}
|
| 69 |
+
|
| 70 |
+
@app.get("/", response_class=HTMLResponse)
|
| 71 |
+
async def home(request: Request):
|
| 72 |
+
return templates.TemplateResponse(request=request, name="index.html")
|
index.html
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Text Summarizer</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: Arial, sans-serif;
|
| 10 |
+
margin: 0;
|
| 11 |
+
padding: 0;
|
| 12 |
+
background-color: #FFF7ED;
|
| 13 |
+
color: #333;
|
| 14 |
+
}
|
| 15 |
+
.container {
|
| 16 |
+
max-width: 600px;
|
| 17 |
+
margin: 50px auto;
|
| 18 |
+
padding: 20px;
|
| 19 |
+
background: #fff;
|
| 20 |
+
border-radius: 10px;
|
| 21 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
h1 {
|
| 25 |
+
text-align: center;
|
| 26 |
+
color: #FB923C;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
h3 {
|
| 30 |
+
text-align: center;
|
| 31 |
+
margin-top: -20px;
|
| 32 |
+
margin-bottom: 40px;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
form {
|
| 36 |
+
display: flex;
|
| 37 |
+
flex-direction: column;
|
| 38 |
+
gap: 15px;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
textarea {
|
| 42 |
+
height: 150px;
|
| 43 |
+
padding: 10px;
|
| 44 |
+
border: 1px solid #ccc;
|
| 45 |
+
border-radius: 5px;
|
| 46 |
+
font-size: 16px;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
button {
|
| 50 |
+
background-color: #FB923C;
|
| 51 |
+
color: white;
|
| 52 |
+
border: none;
|
| 53 |
+
padding: 10px 15px;
|
| 54 |
+
font-size: 16px;
|
| 55 |
+
cursor: pointer;
|
| 56 |
+
border-radius: 5px;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
button:hover {
|
| 60 |
+
background-color: #F97316;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
#summary-output {
|
| 64 |
+
margin-top: 20px;
|
| 65 |
+
background: #FFEDD5;
|
| 66 |
+
padding: 15px;
|
| 67 |
+
border-radius: 5px;
|
| 68 |
+
border: 1px solid #ccc;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
#summary-heading {
|
| 72 |
+
margin-top: 0px;
|
| 73 |
+
margin-bottom: 0px;
|
| 74 |
+
}
|
| 75 |
+
</style>
|
| 76 |
+
</head>
|
| 77 |
+
<body>
|
| 78 |
+
<div class="container">
|
| 79 |
+
<h1>Text Summarizer</h1>
|
| 80 |
+
<h3>Using HuggingFace Transformer</h3>
|
| 81 |
+
<p>Write or Paste your content below for quick summarization.</p>
|
| 82 |
+
|
| 83 |
+
<form id="summarization-form">
|
| 84 |
+
<textarea id="dialogue-input" placeholder="Enter your content here..." required></textarea>
|
| 85 |
+
<button type="submit">Summarize</button>
|
| 86 |
+
</form>
|
| 87 |
+
|
| 88 |
+
<div id="summary-output">
|
| 89 |
+
<h3 id="summary-heading">Content Summary</h3>
|
| 90 |
+
<p id="summary-text"></p>
|
| 91 |
+
</div>
|
| 92 |
+
</div>
|
| 93 |
+
|
| 94 |
+
<script>
|
| 95 |
+
document.getElementById("summarization-form").addEventListener("submit", async (e) => {
|
| 96 |
+
e.preventDefault();
|
| 97 |
+
|
| 98 |
+
const dialogueInput = document.getElementById("dialogue-input");
|
| 99 |
+
const summaryText = document.getElementById("summary-text");
|
| 100 |
+
const submitButton = e.target.querySelector("button");
|
| 101 |
+
|
| 102 |
+
const dialogue = dialogueInput.value.trim();
|
| 103 |
+
if (!dialogue) return;
|
| 104 |
+
|
| 105 |
+
summaryText.innerText = "Processing..."; // Show processing message
|
| 106 |
+
submitButton.disabled = true;
|
| 107 |
+
|
| 108 |
+
try {
|
| 109 |
+
const response = await fetch("/summarize/", {
|
| 110 |
+
method: "POST",
|
| 111 |
+
headers: { "Content-Type": "application/json" },
|
| 112 |
+
body: JSON.stringify({ dialogue }),
|
| 113 |
+
});
|
| 114 |
+
|
| 115 |
+
if (!response.ok) {
|
| 116 |
+
throw new Error(`Server error: ${response.status}`);
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
const data = await response.json();
|
| 120 |
+
summaryText.innerText = data.summary || "No summary returned.";
|
| 121 |
+
} catch (err) {
|
| 122 |
+
|
| 123 |
+
summaryText.innerText = `Error: ${err.message}`;
|
| 124 |
+
} finally {
|
| 125 |
+
|
| 126 |
+
submitButton.disabled = false;
|
| 127 |
+
}
|
| 128 |
+
});
|
| 129 |
+
</script>
|
| 130 |
+
</body>
|
| 131 |
+
</html>
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
sentencepiece
|
| 6 |
+
jinja2
|