File size: 5,053 Bytes
b15ca4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486165c
b15ca4d
 
 
 
 
 
 
 
 
486165c
 
 
b15ca4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState } from 'react';
import { 
  Container, 
  Typography, 
  Button, 
  TextField, 
  Box,
  Paper
} from '@mui/material';

function App() {
  const [file, setFile] = useState(null);
  const [question, setQuestion] = useState('');
  const [response, setResponse] = useState('');
  const [sessionId, setSessionId] = useState('');
  const [error, setError] = useState('');
  const [isProcessing, setIsProcessing] = useState(false);

  const handleFileUpload = async (event) => {
    event.preventDefault();
    const formData = new FormData();
    formData.append('file', event.target.files[0]);
    setError('');
    setIsProcessing(true);

    try {
      const response = await fetch('http://localhost:8000/upload', {
        method: 'POST',
        body: formData,
        headers: {
          'Accept': 'application/json'
        }
      });
      const data = await response.json();
      if (data.session_id) {
        setSessionId(data.session_id);
        console.log('Upload successful, session ID:', data.session_id);
      } else {
        setError('Upload failed: No session ID received');
        console.error('Upload response:', data);
      }
    } catch (error) {
      setError('Upload failed: ' + error.message);
      console.error('Upload error:', error);
    } finally {
      setIsProcessing(false);
    }
  };

  const handleQuestionSubmit = async () => {
    if (!sessionId) {
      setError('Please upload a document first');
      return;
    }
    setError('');
    setResponse(''); // Clear previous response

    try {
      console.log('Sending query with session ID:', sessionId);
      const response = await fetch('http://localhost:8000/query', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
        },
        body: JSON.stringify({
          session_id: sessionId,
          query: question
        }),
      });
      
      console.log('Response status:', response.status);
      const responseText = await response.text();
      console.log('Raw response:', responseText);
      
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}, body: ${responseText}`);
      }
      
      const data = JSON.parse(responseText);
      console.log('Parsed response data:', data);
      
      if (data.answer) {
        console.log('Setting response:', data.answer);
        setResponse(data.answer);
        console.log('Response state after setting:', data.answer);
      } else {
        setError('No answer received from the server');
        console.error('Unexpected response format:', data);
      }
    } catch (error) {
      setError('Query failed: ' + error.message);
      console.error('Query error:', error);
    }
  };

  return (
    <Container maxWidth="md">
      <Box sx={{ my: 4 }}>
        <Typography variant="h4" component="h1" gutterBottom>
          RAG Application
        </Typography>
        
        <Paper sx={{ p: 2, mb: 2 }}>
          <Typography variant="h6" gutterBottom>
            Upload Document
          </Typography>
          <Button
            variant="contained"
            component="label"
            disabled={isProcessing}
          >
            {isProcessing ? 'Processing...' : 'Choose File'}
            <input
              type="file"
              hidden
              onChange={handleFileUpload}
              disabled={isProcessing}
            />
          </Button>
          <Typography sx={{ mt: 1, color: isProcessing ? 'info.main' : 'success.main' }}>
            {isProcessing ? 'Processing document...' : 
             sessionId ? 'Document uploaded and processed successfully!' : 
             'No document uploaded yet'}
          </Typography>
        </Paper>

        <Paper sx={{ p: 2, mb: 2 }}>
          <Typography variant="h6" gutterBottom>
            Ask a Question
          </Typography>
          <TextField
            fullWidth
            multiline
            rows={2}
            value={question}
            onChange={(e) => setQuestion(e.target.value)}
            sx={{ mb: 2 }}
            placeholder="Enter your question here..."
          />
          <Button 
            variant="contained" 
            onClick={handleQuestionSubmit}
            disabled={!sessionId || isProcessing}
          >
            Submit Query
          </Button>
        </Paper>

        <Paper sx={{ p: 2 }}>
          <Typography variant="h6" gutterBottom>
            Response
          </Typography>
          
          <Box sx={{ mb: 2 }}>
            {error && (
              <Typography sx={{ color: 'error.main', mb: 2 }}>
                {error}
              </Typography>
            )}
            
            <Typography sx={{ whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>
              {response || "No response yet. Please upload a document and ask a question."}
            </Typography>
          </Box>
        </Paper>
      </Box>
    </Container>
  );
}

export default App;