import React, { useState } from 'react'; function App() { const [file, setFile] = useState(null); const [question, setQuestion] = useState(''); const [answer, setAnswer] = useState(''); const [context, setContext] = useState([]); const [isLoading, setIsLoading] = useState(false); const [uploadStatus, setUploadStatus] = useState(''); const handleFileUpload = async (e: React.ChangeEvent) => { if (!e.target.files?.length) return; const uploadedFile = e.target.files[0]; setFile(uploadedFile); setIsLoading(true); setUploadStatus('Processing...'); const formData = new FormData(); formData.append('file', uploadedFile); try { const response = await fetch('/api/upload', { method: 'POST', body: formData, }); const data = await response.json(); console.log('File uploaded:', data); setUploadStatus(`Processing ${uploadedFile.name} done. You can now ask questions!`); } catch (error) { console.error('Error uploading file:', error); setUploadStatus('Error processing file. Please try again.'); } finally { setIsLoading(false); } }; const handleAskQuestion = async (e: React.FormEvent) => { e.preventDefault(); if (!question.trim()) return; setIsLoading(true); setAnswer(''); setContext([]); try { const response = await fetch('/api/ask', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query: question }), }); const data = await response.json(); setAnswer(data.answer); setContext(data.context || []); } catch (error) { console.error('Error asking question:', error); setAnswer('Error getting answer. Please try again.'); } finally { setIsLoading(false); } }; return (

Document Q&A

{!file && (

Please upload a Text or PDF file to begin!

)}
{uploadStatus && (

{uploadStatus}

)}
setQuestion(e.target.value)} className="w-full p-2 border rounded-lg" placeholder="Type your question here..." disabled={!file || isLoading} />
{answer && (

Answer:

{answer}

)} {context.length > 0 && (

Source Excerpts:

Below are the relevant excerpts from your document that were used to generate the answer:

{context.map((text, index) => (

Excerpt {index + 1}:

{text}

))}
)}
); } export default App;