Spaces:
Sleeping
Sleeping
File size: 8,439 Bytes
15975c4 d909077 15975c4 d909077 15975c4 d909077 15975c4 a20b04a 15975c4 d909077 15975c4 d909077 15975c4 d909077 15975c4 d909077 15975c4 d909077 15975c4 d909077 15975c4 35961be 15975c4 d909077 15975c4 d909077 |
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
import React, {
ChangeEvent,
useCallback,
useContext,
useEffect,
useRef,
useState,
} from "react";
import axios from "axios";
import { useNavigate } from "react-router-dom";
import { Delete, UploadFile } from "@mui/icons-material";
import {
Card,
Box,
CardContent,
Typography,
CardActions,
Button,
Modal,
CircularProgress,
} from "@mui/material";
import { ThemeSchemeContext, ThemeModeContext } from "../theme";
const AudioUploader = ({ ...props }) => {
const { themeScheme } = useContext(ThemeSchemeContext);
const { themeMode } = useContext(ThemeModeContext);
const light = themeScheme[themeMode];
const navigate = useNavigate();
const [isVisible, setIsVisible] = useState(false);
const [NextCard, setNextCard] = useState(false);
const [isError, setIsError] = useState(false);
const [audioFile, setAudioFile] = useState<File | null>(null);
const [uploadStatus, setUploadStatus] = useState("");
const inputFile = useRef<HTMLInputElement | null>(null);
const [modalOpen, setModalOpen] = useState(false);
const onDragEnter = useCallback(
(e: { stopPropagation: () => void; preventDefault: () => void }) => {
setIsVisible(true);
e.stopPropagation();
e.preventDefault();
return false;
},
[]
);
const onDragOver = useCallback(
(e: { preventDefault: () => void; stopPropagation: () => void }) => {
e.preventDefault();
e.stopPropagation();
return false;
},
[]
);
const onDragLeave = useCallback(
(e: { stopPropagation: () => void; preventDefault: () => void }) => {
setIsVisible(false);
e.stopPropagation();
e.preventDefault();
return false;
},
[]
);
const onDrop = useCallback(
(e: { preventDefault: () => void; dataTransfer: any }) => {
e.preventDefault();
setAudioFile(e.dataTransfer.files[0]);
console.log("Files dropped: ", audioFile?.name);
setNextCard(true);
setIsVisible(false);
return false;
},
[]
);
useEffect(() => {
window.addEventListener("mouseup", onDragLeave);
window.addEventListener("dragenter", onDragEnter);
window.addEventListener("dragover", onDragOver);
window.addEventListener("drop", onDrop);
return () => {
window.removeEventListener("mouseup", onDragLeave);
window.removeEventListener("dragenter", onDragEnter);
window.removeEventListener("dragover", onDragOver);
window.removeEventListener("drop", onDrop);
};
}, [onDragEnter, onDragLeave, onDragOver, onDrop]);
const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file) {
setAudioFile(file);
setNextCard(true);
}
};
const handleCloseModal = () => {
setModalOpen(false);
};
const onButtonClick = () => {
// `current` points to the mounted file input element
inputFile?.current?.click();
};
const handleUpload = async () => {
if (audioFile) {
try {
const server = import.meta.env.VITE_API_URL;
const form = new FormData();
if (audioFile) {
setModalOpen(true);
const blob = new Blob([audioFile], {
type: "application/octet-stream",
});
const audio_file = new File([blob], audioFile.name, {
type: blob.type,
});
form.append("file", audio_file);
}
const response = await axios.post(
`${server}/upload/`,
form,
{
headers: { Accept: "multipart/form-data" },
responseType: "blob",
}
);
setUploadStatus("Upload successful. Waiting for generation...");
console.log("Upload successful:", response);
// Extract JSON from the blob
const blobText = await response.data.text();
const jsonFromBlob = JSON.parse(blobText);
// Assuming the 'id' property is part of the JSON
const file_path = jsonFromBlob.file_path;
const csv_file_path = jsonFromBlob.csv_path;
const output_file_path = jsonFromBlob.output_file_path;
// Store the ID in localStorage
localStorage.setItem("file_path", file_path);
// Store the ID in localStorage
localStorage.setItem("csv_file_path", csv_file_path);
// Store the ID in localStorage
localStorage.setItem("output_file_path", output_file_path);
// Start uploading
navigate("/Results");
// setUploadStatus('Generation complete!');
} catch (error) {
console.error("Error:", error);
props.handleClose();
setIsError(true);
setModalOpen(false);
setNextCard(false);
setUploadStatus(`Error: ${error}`);
}
}
};
return (
<>
<Box sx={{ display: "grid", gap: "1vh" }}>
<PaletteSwatch
title={uploadStatus}
titleColor={light.errorContainer}
onTitle={uploadStatus}
onTitleColor={light.onErrorContainer}
visible={isError}
/>
<Card variant={isVisible ? "outlined" : "filled"}>
<Box sx={{ display: NextCard ? "none" : "inherit" }}>
<CardContent sx={{ display: "grid", justifyItems: "center" }}>
<Typography gutterBottom variant="h5" sx={{ pb: 3 }}>
{isVisible
? "Drop here to upload"
: "Drag a file to start inference"}
</Typography>
<UploadFile sx={{ fontSize: "35px", color: "inherit" }} />
</CardContent>
<CardActions sx={{ display: "flex", justifyContent: "flex-end" }}>
<input
type="file"
accept="audio/*"
ref={inputFile}
style={{ display: "none" }}
onChange={handleFileChange}
/>
<Button
variant="filled"
size="small"
onClick={() => onButtonClick()}
>
Upload
</Button>
</CardActions>
</Box>
<Box sx={{ display: NextCard ? "inherit" : "none" }}>
<CardContent
sx={{
display: "flex",
justifyItems: "center",
flexDirection: "column",
alignItems: "flex-end",
gap: "1vh",
}}
>
<Box sx={{ mr: "1" }}>
<Button
variant="text"
sx={{fontSize:"10px", marginTop:-2, marginRight:-1.5}}
onClick={() => {
setNextCard(false);
setIsVisible(false);
}}
>
<Delete />
</Button>
</Box>
<Typography fontFamily={"monospace"} fontSize={"15pt"}>
{audioFile?.name}
</Typography>
</CardContent>
<CardActions sx={{ display: "flex", justifyContent: "center" }}>
<Button
variant="filled"
size="medium"
onClick={() => handleUpload()}
>
Confirm
</Button>
</CardActions>
</Box>
</Card>
<Modal
open={modalOpen}
onClose={handleCloseModal}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<Box sx={style_modal}>
<CircularProgress size={60} thickness={4} />
</Box>
</Modal>
</Box>
</>
);
};
export default AudioUploader;
const PaletteSwatch = ({ onTitle, titleColor, onTitleColor, visible }: any) => {
return (
<Box
sx={{
p: 1.5,
borderRadius: "10px",
bgcolor: onTitleColor,
color: titleColor,
visibility: visible ? "visible" : "collapse",
}}
>
<Typography fontSize={12} fontWeight={"bold"}>
{onTitle}
</Typography>
</Box>
);
};
const style_modal = {
position: "absolute" as "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
color: "inherit",
bgcolor: "background.paper",
p: 4,
borderRadius: "10px",
display: "flex",
justifyContent: "center",
alignTtems: "center",
};
|