Download src/App.jsx from mxhulab/ahaha: direct link, hf CLI and curl.
- Browser
- Download file 5.86 kB
-
https://huggingface.co/spaces/mxhulab/ahaha/resolve/main/src/App.jsx
- Command line
-
hf download hf://spaces/mxhulab/ahaha/src/App.jsx
-
curl -L -o App.jsx https://huggingface.co/spaces/mxhulab/ahaha/resolve/main/src/App.jsx
5.86 kB
| import { useState, useRef, useEffect } from 'react' | |
| import UploadForm from './UploadForm'; | |
| import LogsDisplay from './LogsDisplay'; | |
| import FiguresDisplay from './FiguresDisplay'; | |
| import ImageModal from './ImageModal'; | |
| import 'bootstrap/dist/css/bootstrap.min.css'; | |
| import './App.css'; | |
| function App() { | |
| const [selectedFile, setSelectedFile] = useState(null); | |
| const [formData, setFormData] = useState({ | |
| tilt: '', | |
| num_threshold: 10, | |
| entropy_threshold: 0.01, | |
| n_plots: 10 | |
| }); | |
| const [statusText, setStatusText] = useState('Start Analysis'); | |
| const [figures, setFigures] = useState([]); | |
| const [logs, setLogs] = useState([]); | |
| const [modalState, setModalState] = useState({ | |
| show: false, | |
| currentIndex: 0 | |
| }); | |
| const leftFormRef = useRef(null); | |
| const rightPanelRef = useRef(null); | |
| const logPanelRef = useRef(null); | |
| const validateForm = () => { | |
| if (!selectedFile || !selectedFile.name.endsWith('.cs')) { | |
| alert('Please select a .cs file to upload'); | |
| return false; | |
| } | |
| if (!formData.tilt || isNaN(parseFloat(formData.tilt))) { | |
| alert('Micrograph Tilting must be a floating number'); | |
| return false; | |
| } | |
| if (!formData.num_threshold || isNaN(parseInt(formData.num_threshold))) { | |
| alert('Fibril Minimum Particle Count Threshold must be an integer'); | |
| return false; | |
| } else if (parseInt(formData.num_threshold) < 0) { | |
| alert('Fibril Minimum Particle Count Threshold must be a non-negative integer'); | |
| return false; | |
| } | |
| if (!formData.entropy_threshold || isNaN(parseFloat(formData.entropy_threshold))) { | |
| alert('Fibril Maximum Particle Entropy Threshold must be a floating number'); | |
| return false; | |
| } else if (parseFloat(formData.entropy_threshold) < 0 || parseFloat(formData.entropy_threshold) > 1) { | |
| alert('Fibril Maximum Particle Entropy Threshold must be a floating number between 0 and 1'); | |
| return false; | |
| } | |
| if (!formData.n_plots || isNaN(parseInt(formData.n_plots))) { | |
| alert('Number of Fibrils to Analyze must be an integer'); | |
| return false; | |
| } else if (parseInt(formData.n_plots) < 0) { | |
| alert('Number of Fibrils to Analyze must be a non-negative integer'); | |
| return false; | |
| } | |
| return true; | |
| } | |
| const handleSubmit = async (e) => { | |
| e.preventDefault(); | |
| if (!validateForm()) return; | |
| try { | |
| setStatusText('Uploading...'); | |
| setLogs([]); | |
| setFigures([]); | |
| // Create FormData object | |
| const submitData = new FormData(); | |
| submitData.append('file', selectedFile); | |
| submitData.append('tilt', formData.tilt); | |
| submitData.append('num_threshold', formData.num_threshold); | |
| submitData.append('entropy_threshold', formData.entropy_threshold); | |
| submitData.append('n_plots', formData.n_plots); | |
| const response = await fetch('https://zerinf-ahaha-backend.hf.space/api', { | |
| method: 'POST', | |
| body: submitData | |
| }); | |
| if (!response.ok) { | |
| throw new Error('Ahaha backend broke down, contact the administrator!'); | |
| } | |
| setStatusText('Processing...'); | |
| // 处理流式响应,注意read得到的chunk未必是完整的 | |
| const reader = response.body.getReader(); | |
| const decoder = new TextDecoder(); | |
| let buffer = ''; | |
| while (true) { | |
| const { done, value } = await reader.read(); | |
| if (done) break; | |
| const chunk = decoder.decode(value); | |
| buffer += chunk; | |
| const lines = buffer.split('\n'); | |
| buffer = lines.pop() || ''; | |
| for (const line of lines) { | |
| if (line.startsWith('data: ')) { | |
| const data = JSON.parse(line.slice(6)); | |
| if (data.type == 'info' || data.type == 'final') { | |
| setLogs(prev => [...prev, data]); | |
| // console.log(`[${data.type}] ${data.message}`); | |
| } else if (data.type == 'error') { | |
| throw new Error(data.message); | |
| } else if (data.type == 'data') { | |
| setFigures(prev => [...prev, data.data]); | |
| } | |
| } | |
| } | |
| } | |
| } catch (error) { | |
| alert(`Failed to analysis data: ${error}`); | |
| } finally { | |
| setStatusText('Start Analysis'); | |
| } | |
| }; | |
| useEffect(() => { | |
| if (leftFormRef.current && rightPanelRef.current) { | |
| rightPanelRef.current.style.minHeight = `${leftFormRef.current.offsetHeight}px`; | |
| } | |
| }, [logs.length, figures.length, selectedFile, formData, statusText, modalState]); | |
| useEffect(() => { | |
| if (logPanelRef.current) { | |
| logPanelRef.current.scrollTop = logPanelRef.current.scrollHeight; | |
| } | |
| }, [logs]); | |
| return ( | |
| <div id="App"> | |
| <div id="Header"/> | |
| <div id="Container"> | |
| <div id="Title"> | |
| <h1 id="MajorTitle">Ahaha</h1> | |
| <h2 id="MinorTitle"><b>A</b>bsolute <b>Ha</b>nd Determination for <b>H</b>elical <b>A</b>ssembly</h2> | |
| </div> | |
| <div id="MainPanel"> | |
| <div id="LeftPanel" ref={leftFormRef}> | |
| <UploadForm | |
| selectedFile={selectedFile} | |
| setSelectedFile={setSelectedFile} | |
| formData={formData} | |
| setFormData={setFormData} | |
| statusText={statusText} | |
| handleSubmit={handleSubmit} | |
| /> | |
| </div> | |
| <div id="RightPanel" ref={rightPanelRef}> | |
| <div id="LogsPanel" ref={logPanelRef}> | |
| <LogsDisplay | |
| logs={logs} | |
| /> | |
| </div> | |
| <div id="FiguresPanel"> | |
| <FiguresDisplay | |
| figures={figures} | |
| setModalState={setModalState} | |
| /> | |
| </div> | |
| </div> | |
| </div> | |
| <ImageModal | |
| modalState={modalState} | |
| setModalState={setModalState} | |
| figures={figures} | |
| /> | |
| </div> | |
| </div> | |
| ) | |
| } | |
| export default App | |