Spaces:
Running
Running
File size: 5,981 Bytes
803d869 | 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | import React, { useState } from "react";
import api from "../../api";
import "../../assets/css/style.css";
import ErrorPopup from "../Sections/ErrorPopup";
import VoiceInput from "../Sections/VoiceInput"; // 👈 Make sure you have this component
const Summariser = () => {
const [inputText, setInputText] = useState("");
const [summarizedText, setSummarizedText] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [summaryType, setSummaryType] = useState("paragraph");
const [originalText, setOriginalText] = useState("");
const [errorPopupMessage, setErrorPopupMessage] = useState("");
const token = localStorage.getItem("jwtToken");
const handleSummarize = async () => {
if (!inputText.trim()) {
setError("Please enter text to summarize.");
return;
}
setError("");
setLoading(true);
setSummarizedText("");
setOriginalText("");
try {
const response = await api.summarizeText(
{
text: inputText,
type: summaryType,
},
{ headers: { Authorization: `Bearer ${token}` } },
);
if (response && response.summarized_text) {
const cleanedText = response.summarized_text
.replace(/\n\s*\n/g, "\n")
.replace(/^\s+|\s+$/g, "");
setSummarizedText(cleanedText);
setOriginalText(response.original_text || inputText);
} else {
const message = "No summarized text found.";
setError(message);
setErrorPopupMessage(message);
}
} catch (err) {
const message =
err.message || "Failed to summarize text. Please try again.";
setError(message);
setErrorPopupMessage(
message.includes("504")
? "Server timeout. Please try again. (504 Gateway Timeout)"
: message,
);
} finally {
setLoading(false);
}
};
const handleClearText = () => {
setInputText("");
setSummarizedText("");
setOriginalText("");
setError("");
};
const handleVoiceResult = (transcript) => {
setInputText((prevText) => `${prevText} ${transcript}`.trim());
};
const formatSummaryText = (text) => {
if (!text) return null;
const normalizedText = text
.replace(/\n\s*\n/g, "\n")
.replace(/^\s+|\s+$/g, "");
const paragraphs = normalizedText
.split("\n")
.filter((p) => p.trim().length > 0);
return paragraphs.map((paragraph, index) => (
<React.Fragment key={index}>
{paragraph.startsWith("- ") ? (
<li className="bullet-point">{paragraph.substring(2)}</li>
) : (
<p className="summary-paragraph">{paragraph}</p>
)}
{index < paragraphs.length - 1 && <br />}
</React.Fragment>
));
};
return (
<div className="summariser-container">
<h2>Text Summarizer</h2>
<div className="controls">
<div className="summary-type-selector">
<label>
<input
type="radio"
value="paragraph"
checked={summaryType === "paragraph"}
onChange={() => setSummaryType("paragraph")}
/>
Paragraph Summary
</label>
<label>
<input
type="radio"
value="points"
checked={summaryType === "points"}
onChange={() => setSummaryType("points")}
/>
Bullet Points
</label>
</div>
</div>
<div className="text-area-row">
<div className="text-box">
<h3>Original Text:</h3>
<textarea
className="input-textarea-3 summariser-textarea"
placeholder="Type or paste your text here..."
value={inputText}
onChange={(e) => setInputText(e.target.value)}
rows={14}
/>
</div>
<div className="text-box">
<h3>Summarized Text:</h3>
<div className="output-display-3 summariser-output">
{loading ? (
<div className="loading-spinner">Summarizing...</div>
) : summarizedText ? (
<div className="summary-result">
{formatSummaryText(summarizedText)}
</div>
) : (
<div className="placeholder">
Summarized text will appear here...
</div>
)}
</div>
</div>
</div>
{error && <div className="error-message">{error}</div>}
<div className="button-container">
<VoiceInput
onResult={handleVoiceResult}
language="en-US"
buttonStyle={{}}
/>
<button
onClick={handleSummarize}
disabled={loading || !inputText.trim()}
className="summarize-button"
>
{loading ? "Processing..." : "Summarize"}
</button>
<button
onClick={handleClearText}
className="clear-button"
disabled={loading}
>
Clear
</button>
</div>
{originalText && summarizedText && (
<div className="summary-stats">
<p>
Original length: {originalText.split(" ").length} words | Summary
length: {summarizedText.split(" ").length} words | Reduction:{" "}
{Math.round(
100 -
(summarizedText.split(" ").length /
originalText.split(" ").length) *
100,
)}
%
</p>
</div>
)}
<ErrorPopup
message={errorPopupMessage}
onClose={() => setErrorPopupMessage("")}
/>
</div>
);
};
export default Summariser;
|