Spaces:
Runtime error
Runtime error
File size: 5,191 Bytes
273a5e1 |
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 |
import React, { useState, useEffect } from 'react';
import './App.css';
function App() {
const [file, setFile] = useState<File | null>(null);
const [query, setQuery] = useState('');
const [answer, setAnswer] = useState('');
const [context, setContext] = useState<string[]>([]);
const [loading, setLoading] = useState(false);
const [uploadStatus, setUploadStatus] = useState('');
const handleFileUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files && e.target.files[0]) {
setFile(e.target.files[0]);
const formData = new FormData();
formData.append('file', e.target.files[0]);
setLoading(true);
setUploadStatus('Uploading...');
try {
const response = await fetch('http://localhost:8000/upload', {
method: 'POST',
body: formData,
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || 'Failed to upload file');
}
const data = await response.json();
setUploadStatus(`Success! Processed ${data.chunks} chunks`);
} catch (error) {
console.error('Error:', error);
setUploadStatus(error instanceof Error ? error.message : 'Error uploading file');
}
setLoading(false);
}
};
const handleQuery = async (e: React.FormEvent) => {
e.preventDefault();
if (!query.trim()) return;
setLoading(true);
try {
const response = await fetch('http://localhost:8000/query', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: query.trim() }),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || 'Failed to get response');
}
const data = await response.json();
console.log(data);
setAnswer(data.response);
setContext(data.context);
} catch (error) {
console.error('Error:', error);
setAnswer(error instanceof Error ? error.message : 'Error getting response');
}
setLoading(false);
};
const checkStatus = async () => {
try {
const response = await fetch('http://localhost:8000/status');
const data = await response.json();
if (data.ready) {
setUploadStatus('Session initialized. Ready for document upload.');
}
} catch (error) {
console.error('Error checking status:', error);
}
};
useEffect(() => {
checkStatus();
}, []);
return (
<div className="App">
{loading && <div className="loader">
<p>Processing your document...</p>
</div>}
<header className="App-header">
<h1>Custom document Q&A System</h1>
</header>
<main className="App-main">
<section className="upload-section">
<h2>Upload Document</h2>
<input
type="file"
accept=".txt,.pdf"
onChange={handleFileUpload}
disabled={loading}
/>
{uploadStatus && <p className="status">{uploadStatus}</p>}
</section>
<section className="query-section">
<h2>Ask a Question</h2>
<form onSubmit={handleQuery}>
<input
type="text"
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Enter your question..."
disabled={loading || !uploadStatus.includes('Success')}
/>
<button
type="submit"
disabled={loading || !uploadStatus.includes('Success')}
>
Ask
</button>
</form>
</section>
{answer && (
<section className="answer-section">
<h2>Answer</h2>
<p>{answer}</p>
<h3>Relevant Context</h3>
<div className="context">
{context.map((text, index) => (
<div key={index} className="context-item">
{text}
</div>
))}
</div>
</section>
)}
</main>
</div>
);
}
export default App; |