File size: 5,864 Bytes
523133d
 
 
 
 
 
 
bfa647c
 
523133d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfa647c
523133d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfa647c
 
 
 
 
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
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