Spaces:
Sleeping
Sleeping
Update frontend/App.jsx
Browse files- frontend/App.jsx +168 -168
frontend/App.jsx
CHANGED
|
@@ -1,169 +1,169 @@
|
|
| 1 |
-
const { useState, useRef, useEffect } = React;
|
| 2 |
-
|
| 3 |
-
function App() {
|
| 4 |
-
const [file, setFile] = useState(null);
|
| 5 |
-
const [insights, setInsights] = useState(null);
|
| 6 |
-
const [chatHistory, setChatHistory] = useState([]);
|
| 7 |
-
const [inputValue, setInputValue] = useState('');
|
| 8 |
-
const [isProcessing, setIsProcessing] = useState(false);
|
| 9 |
-
const [uploadSuccess, setUploadSuccess] = useState(false);
|
| 10 |
-
const chatEndRef = useRef(null);
|
| 11 |
-
const fileInputRef = useRef(null);
|
| 12 |
-
|
| 13 |
-
useEffect(() => {
|
| 14 |
-
chatEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
| 15 |
-
}, [chatHistory, insights]);
|
| 16 |
-
|
| 17 |
-
const handleFileChange = (e) => {
|
| 18 |
-
if (e.target.files && e.target.files.length > 0) {
|
| 19 |
-
setFile(e.target.files[0]);
|
| 20 |
-
setUploadSuccess(false);
|
| 21 |
-
}
|
| 22 |
-
};
|
| 23 |
-
|
| 24 |
-
const handleUpload = async () => {
|
| 25 |
-
if (!file) return;
|
| 26 |
-
setIsProcessing(true);
|
| 27 |
-
setInsights(null);
|
| 28 |
-
setChatHistory([]);
|
| 29 |
-
setUploadSuccess(false);
|
| 30 |
-
|
| 31 |
-
const formData = new FormData();
|
| 32 |
-
formData.append('file', file);
|
| 33 |
-
|
| 34 |
-
try {
|
| 35 |
-
const response = await fetch('/upload', {
|
| 36 |
-
method: 'POST',
|
| 37 |
-
body: formData,
|
| 38 |
-
});
|
| 39 |
-
const data = await response.json();
|
| 40 |
-
if (data.insights) {
|
| 41 |
-
setInsights(data.insights);
|
| 42 |
-
setUploadSuccess(true);
|
| 43 |
-
} else {
|
| 44 |
-
alert("Error: " + data.error);
|
| 45 |
-
}
|
| 46 |
-
} catch (error) {
|
| 47 |
-
console.error("Upload failed:", error);
|
| 48 |
-
} finally {
|
| 49 |
-
setIsProcessing(false);
|
| 50 |
-
}
|
| 51 |
-
};
|
| 52 |
-
|
| 53 |
-
const handleSendMessage = async (e) => {
|
| 54 |
-
if (e.key === 'Enter' && inputValue.trim() !== '') {
|
| 55 |
-
const userMessage = inputValue;
|
| 56 |
-
setInputValue('');
|
| 57 |
-
setChatHistory(prev => [...prev, { role: 'user', content: userMessage }]);
|
| 58 |
-
|
| 59 |
-
try {
|
| 60 |
-
const response = await fetch('/chat', {
|
| 61 |
-
method: 'POST',
|
| 62 |
-
headers: { 'Content-Type': 'application/json' },
|
| 63 |
-
body: JSON.stringify({ message: userMessage }),
|
| 64 |
-
});
|
| 65 |
-
const data = await response.json();
|
| 66 |
-
|
| 67 |
-
if (data.answer) {
|
| 68 |
-
setChatHistory(prev => [...prev, { role: 'assistant', content: data.answer }]);
|
| 69 |
-
} else {
|
| 70 |
-
alert("Error: " + data.error);
|
| 71 |
-
}
|
| 72 |
-
} catch (error) {
|
| 73 |
-
console.error("Chat failed:", error);
|
| 74 |
-
}
|
| 75 |
-
}
|
| 76 |
-
};
|
| 77 |
-
|
| 78 |
-
return (
|
| 79 |
-
<div className="app-container">
|
| 80 |
-
<div className="sidebar">
|
| 81 |
-
<h2>Upload Paper</h2>
|
| 82 |
-
<p className="subtext">Upload a PDF research paper</p>
|
| 83 |
-
|
| 84 |
-
<div className="upload-box" onClick={() => fileInputRef.current.click()}>
|
| 85 |
-
<span className="upload-label">Drag and drop file here</span>
|
| 86 |
-
<span className="upload-limit">Limit 200MB per file β’ PDF</span>
|
| 87 |
-
<button className="browse-btn">Browse files</button>
|
| 88 |
-
<input
|
| 89 |
-
type="file"
|
| 90 |
-
accept=".pdf"
|
| 91 |
-
ref={fileInputRef}
|
| 92 |
-
onChange={handleFileChange}
|
| 93 |
-
/>
|
| 94 |
-
</div>
|
| 95 |
-
|
| 96 |
-
{file && (
|
| 97 |
-
<div className="file-selected">
|
| 98 |
-
π {file.name.substring(0, 20)}{file.name.length > 20 ? '...' : ''}
|
| 99 |
-
<span style={{cursor: 'pointer'}} onClick={() => setFile(null)}>β</span>
|
| 100 |
-
</div>
|
| 101 |
-
)}
|
| 102 |
-
|
| 103 |
-
<button
|
| 104 |
-
className="process-btn"
|
| 105 |
-
onClick={handleUpload}
|
| 106 |
-
disabled={!file || isProcessing}
|
| 107 |
-
>
|
| 108 |
-
{isProcessing ? "Processing..." : "Process Paper"}
|
| 109 |
-
</button>
|
| 110 |
-
|
| 111 |
-
{uploadSuccess && (
|
| 112 |
-
<div className="success-banner">Paper processed successfully!</div>
|
| 113 |
-
)}
|
| 114 |
-
</div>
|
| 115 |
-
|
| 116 |
-
<div className="main-content">
|
| 117 |
-
<div className="header">
|
| 118 |
-
<h1>π Research Paper Deconstructor</h1>
|
| 119 |
-
</div>
|
| 120 |
-
|
| 121 |
-
{!insights && !isProcessing && (
|
| 122 |
-
<div className="placeholder-info">
|
| 123 |
-
π Please upload a PDF research paper
|
| 124 |
-
</div>
|
| 125 |
-
)}
|
| 126 |
-
|
| 127 |
-
{isProcessing && (
|
| 128 |
-
<div className="spinner">Ingesting and analyzing document...</div>
|
| 129 |
-
)}
|
| 130 |
-
|
| 131 |
-
{insights && (
|
| 132 |
-
<div className="insights-container">
|
| 133 |
-
<h3>π Core Paper Insights</h3>
|
| 134 |
-
<div className="insights-box">
|
| 135 |
-
<pre>{insights}</pre>
|
| 136 |
-
</div>
|
| 137 |
-
</div>
|
| 138 |
-
)}
|
| 139 |
-
|
| 140 |
-
<div className="chat-container">
|
| 141 |
-
{chatHistory.map((msg, index) => (
|
| 142 |
-
<div key={index} className={`chat-message ${msg.role}`}>
|
| 143 |
-
<div className="avatar">{msg.role === 'user' ? 'U' : 'AI'}</div>
|
| 144 |
-
<div className="message-content"><pre>{msg.content}</pre></div>
|
| 145 |
-
</div>
|
| 146 |
-
))}
|
| 147 |
-
<div ref={chatEndRef} />
|
| 148 |
-
</div>
|
| 149 |
-
</div>
|
| 150 |
-
|
| 151 |
-
{insights && (
|
| 152 |
-
<div className="input-wrapper">
|
| 153 |
-
<div className="input-container">
|
| 154 |
-
<input
|
| 155 |
-
type="text"
|
| 156 |
-
placeholder="Ask about hyperparameters, layers, loss functions..."
|
| 157 |
-
value={inputValue}
|
| 158 |
-
onChange={(e) => setInputValue(e.target.value)}
|
| 159 |
-
onKeyDown={handleSendMessage}
|
| 160 |
-
/>
|
| 161 |
-
</div>
|
| 162 |
-
</div>
|
| 163 |
-
)}
|
| 164 |
-
</div>
|
| 165 |
-
);
|
| 166 |
-
}
|
| 167 |
-
|
| 168 |
-
const root = ReactDOM.createRoot(document.getElementById('root'));
|
| 169 |
root.render(<App />);
|
|
|
|
| 1 |
+
const { useState, useRef, useEffect } = React;
|
| 2 |
+
|
| 3 |
+
function App() {
|
| 4 |
+
const [file, setFile] = useState(null);
|
| 5 |
+
const [insights, setInsights] = useState(null);
|
| 6 |
+
const [chatHistory, setChatHistory] = useState([]);
|
| 7 |
+
const [inputValue, setInputValue] = useState('');
|
| 8 |
+
const [isProcessing, setIsProcessing] = useState(false);
|
| 9 |
+
const [uploadSuccess, setUploadSuccess] = useState(false);
|
| 10 |
+
const chatEndRef = useRef(null);
|
| 11 |
+
const fileInputRef = useRef(null);
|
| 12 |
+
|
| 13 |
+
useEffect(() => {
|
| 14 |
+
chatEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
| 15 |
+
}, [chatHistory, insights]);
|
| 16 |
+
|
| 17 |
+
const handleFileChange = (e) => {
|
| 18 |
+
if (e.target.files && e.target.files.length > 0) {
|
| 19 |
+
setFile(e.target.files[0]);
|
| 20 |
+
setUploadSuccess(false);
|
| 21 |
+
}
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
const handleUpload = async () => {
|
| 25 |
+
if (!file) return;
|
| 26 |
+
setIsProcessing(true);
|
| 27 |
+
setInsights(null);
|
| 28 |
+
setChatHistory([]);
|
| 29 |
+
setUploadSuccess(false);
|
| 30 |
+
|
| 31 |
+
const formData = new FormData();
|
| 32 |
+
formData.append('file', file);
|
| 33 |
+
|
| 34 |
+
try {
|
| 35 |
+
const response = await fetch('/upload', {
|
| 36 |
+
method: 'POST',
|
| 37 |
+
body: formData,
|
| 38 |
+
});
|
| 39 |
+
const data = await response.json();
|
| 40 |
+
if (data.insights) {
|
| 41 |
+
setInsights(data.insights);
|
| 42 |
+
setUploadSuccess(true);
|
| 43 |
+
} else {
|
| 44 |
+
alert("Error: " + data.error);
|
| 45 |
+
}
|
| 46 |
+
} catch (error) {
|
| 47 |
+
console.error("Upload failed:", error);
|
| 48 |
+
} finally {
|
| 49 |
+
setIsProcessing(false);
|
| 50 |
+
}
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
const handleSendMessage = async (e) => {
|
| 54 |
+
if (e.key === 'Enter' && inputValue.trim() !== '') {
|
| 55 |
+
const userMessage = inputValue;
|
| 56 |
+
setInputValue('');
|
| 57 |
+
setChatHistory(prev => [...prev, { role: 'user', content: userMessage }]);
|
| 58 |
+
|
| 59 |
+
try {
|
| 60 |
+
const response = await fetch('/chat', {
|
| 61 |
+
method: 'POST',
|
| 62 |
+
headers: { 'Content-Type': 'application/json' },
|
| 63 |
+
body: JSON.stringify({ message: userMessage }),
|
| 64 |
+
});
|
| 65 |
+
const data = await response.json();
|
| 66 |
+
|
| 67 |
+
if (data.answer) {
|
| 68 |
+
setChatHistory(prev => [...prev, { role: 'assistant', content: data.answer }]);
|
| 69 |
+
} else {
|
| 70 |
+
alert("Error: " + data.error);
|
| 71 |
+
}
|
| 72 |
+
} catch (error) {
|
| 73 |
+
console.error("Chat failed:", error);
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
return (
|
| 79 |
+
<div className="app-container">
|
| 80 |
+
<div className="sidebar">
|
| 81 |
+
<h2>Upload Paper</h2>
|
| 82 |
+
<p className="subtext">Upload a PDF research paper</p>
|
| 83 |
+
|
| 84 |
+
<div className="upload-box" onClick={() => fileInputRef.current.click()}>
|
| 85 |
+
<span className="upload-label">Drag and drop file here</span>
|
| 86 |
+
<span className="upload-limit">Limit 200MB per file β’ PDF</span>
|
| 87 |
+
<button className="browse-btn">Browse files</button>
|
| 88 |
+
<input
|
| 89 |
+
type="file"
|
| 90 |
+
accept=".pdf"
|
| 91 |
+
ref={fileInputRef}
|
| 92 |
+
onChange={handleFileChange}
|
| 93 |
+
/>
|
| 94 |
+
</div>
|
| 95 |
+
|
| 96 |
+
{file && (
|
| 97 |
+
<div className="file-selected">
|
| 98 |
+
π {file.name.substring(0, 20)}{file.name.length > 20 ? '...' : ''}
|
| 99 |
+
<span style={{cursor: 'pointer'}} onClick={() => setFile(null)}>β</span>
|
| 100 |
+
</div>
|
| 101 |
+
)}
|
| 102 |
+
|
| 103 |
+
<button
|
| 104 |
+
className="process-btn"
|
| 105 |
+
onClick={handleUpload}
|
| 106 |
+
disabled={!file || isProcessing}
|
| 107 |
+
>
|
| 108 |
+
{isProcessing ? "Processing..." : "Process Paper"}
|
| 109 |
+
</button>
|
| 110 |
+
|
| 111 |
+
{uploadSuccess && (
|
| 112 |
+
<div className="success-banner">Paper processed successfully!</div>
|
| 113 |
+
)}
|
| 114 |
+
</div>
|
| 115 |
+
|
| 116 |
+
<div className="main-content">
|
| 117 |
+
<div className="header">
|
| 118 |
+
<h1>π Research Paper Deconstructor</h1>
|
| 119 |
+
</div>
|
| 120 |
+
|
| 121 |
+
{!insights && !isProcessing && (
|
| 122 |
+
<div className="placeholder-info">
|
| 123 |
+
π Please upload a PDF research paper.
|
| 124 |
+
</div>
|
| 125 |
+
)}
|
| 126 |
+
|
| 127 |
+
{isProcessing && (
|
| 128 |
+
<div className="spinner">Ingesting and analyzing document...</div>
|
| 129 |
+
)}
|
| 130 |
+
|
| 131 |
+
{insights && (
|
| 132 |
+
<div className="insights-container">
|
| 133 |
+
<h3>π Core Paper Insights</h3>
|
| 134 |
+
<div className="insights-box">
|
| 135 |
+
<pre>{insights}</pre>
|
| 136 |
+
</div>
|
| 137 |
+
</div>
|
| 138 |
+
)}
|
| 139 |
+
|
| 140 |
+
<div className="chat-container">
|
| 141 |
+
{chatHistory.map((msg, index) => (
|
| 142 |
+
<div key={index} className={`chat-message ${msg.role}`}>
|
| 143 |
+
<div className="avatar">{msg.role === 'user' ? 'U' : 'AI'}</div>
|
| 144 |
+
<div className="message-content"><pre>{msg.content}</pre></div>
|
| 145 |
+
</div>
|
| 146 |
+
))}
|
| 147 |
+
<div ref={chatEndRef} />
|
| 148 |
+
</div>
|
| 149 |
+
</div>
|
| 150 |
+
|
| 151 |
+
{insights && (
|
| 152 |
+
<div className="input-wrapper">
|
| 153 |
+
<div className="input-container">
|
| 154 |
+
<input
|
| 155 |
+
type="text"
|
| 156 |
+
placeholder="Ask about hyperparameters, layers, loss functions..."
|
| 157 |
+
value={inputValue}
|
| 158 |
+
onChange={(e) => setInputValue(e.target.value)}
|
| 159 |
+
onKeyDown={handleSendMessage}
|
| 160 |
+
/>
|
| 161 |
+
</div>
|
| 162 |
+
</div>
|
| 163 |
+
)}
|
| 164 |
+
</div>
|
| 165 |
+
);
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
const root = ReactDOM.createRoot(document.getElementById('root'));
|
| 169 |
root.render(<App />);
|