|
1 | 1 | import React, { useEffect, useState } from 'react'; |
2 | 2 | import { chatServices } from './services/chat-services'; |
3 | | -import { Grid, CircularProgress, Typography } from '@mui/material'; |
| 3 | +import { Grid, CircularProgress, Typography, Button } from '@mui/material'; |
4 | 4 | import { KeyboardReturn } from '@mui/icons-material'; |
5 | 5 |
|
| 6 | +const styles = { |
| 7 | + grid: { |
| 8 | + alignItems: 'center', |
| 9 | + display: 'flex', |
| 10 | + flexDirection: 'column', |
| 11 | + justifyContent: 'center' |
| 12 | + }, |
| 13 | + input: { |
| 14 | + boxShadow: 24, |
| 15 | + height: '25px', |
| 16 | + width: '300px', |
| 17 | + minWidth: '100px' |
| 18 | + } |
| 19 | +} |
| 20 | + |
6 | 21 | const Chat = () => { |
7 | 22 | const [userInput, setUserInput] = useState(''); |
8 | 23 | const [loading, setLoading] = useState(false); |
9 | 24 | const [answer, setAnswer] = useState(''); |
10 | 25 | const [error, setError] = useState(''); |
| 26 | + const [selectedFile, setSelectedFile] = useState(null); |
11 | 27 |
|
12 | 28 | const handleInputChange = (event) => { |
13 | 29 | setError(''); |
14 | 30 | setUserInput(event.target.value); |
15 | 31 | }; |
16 | 32 |
|
17 | 33 | const handlSendUserInput = async (event) => { |
18 | | - event.persist(); |
19 | | - if (event.key !== "Enter") { |
20 | | - return; |
| 34 | + event.persist(); |
| 35 | + if (event.key !== "Enter") { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + try { |
| 40 | + setLoading(true); |
| 41 | + const { response } = await chatServices.chatWithLLM({ userInput }); |
| 42 | + setAnswer(response); |
| 43 | + } catch (err) { |
| 44 | + setError(err); |
| 45 | + return; |
| 46 | + } finally { |
| 47 | + setLoading(false); |
21 | 48 | } |
| 49 | + }; |
22 | 50 |
|
| 51 | + const handleFileChange = (event) => { |
| 52 | + setSelectedFile(event.target.files[0]); |
| 53 | + } |
| 54 | + |
| 55 | + const handleFileUpload = async () => { |
| 56 | + if (selectedFile) { |
23 | 57 | try { |
24 | | - setLoading(true); |
25 | | - const { response } = await chatServices.create({ userInput }); |
26 | | - setAnswer(response); |
27 | | - } catch (err) { |
28 | | - setError(err); |
29 | | - return; |
30 | | - } finally { |
31 | | - setLoading(false); |
| 58 | + setLoading(true); |
| 59 | + const form = new FormData(); |
| 60 | + form.append('chat-file', selectedFile); |
| 61 | + |
| 62 | + const { success } = await chatServices.ingestFile({ fileInput: form }) |
| 63 | + if (success) { |
| 64 | + setAnswer('Successfully ingested. Ask me anything.'); |
32 | 65 | } |
| 66 | + } catch (err) { |
| 67 | + setSelectedFile(null); |
| 68 | + setError(err); |
| 69 | + } finally { |
| 70 | + setLoading(false); |
| 71 | + } |
| 72 | + } |
33 | 73 | } |
34 | 74 |
|
35 | 75 | useEffect(() => { |
36 | 76 | if (userInput != null && userInput.trim() === "") { |
37 | 77 | setAnswer(''); |
38 | 78 | } |
39 | 79 | }, [userInput]); |
40 | | - |
41 | | - const gridStyle = { |
42 | | - alignItems: 'center', |
43 | | - display: 'flex', |
44 | | - flexDirection: 'column', |
45 | | - justifyContent: 'center', |
46 | | - }; |
47 | | - |
48 | | - const inputStyle = { |
49 | | - boxShadow: 24, |
50 | | - height: '25px', |
51 | | - width: '300px', |
52 | | - minWidth: '100px', |
53 | | - } |
54 | 80 |
|
55 | 81 | return ( |
56 | | - <Grid container spacing={2} style={gridStyle}> |
57 | | - <Grid item xs={8} style={{ display: 'flex', flexDirection: 'row' }}> |
58 | | - <input style={inputStyle} |
| 82 | + <Grid container spacing={2} style={styles.grid}> |
| 83 | + <Grid item xs={8} style={{ display: 'flex', flexDirection: 'column' }}> |
| 84 | + <div style={{ display: 'flex' }}> |
| 85 | + <input style={styles.input} |
59 | 86 | value={userInput} |
60 | 87 | onChange={handleInputChange} |
61 | 88 | onKeyDown={handlSendUserInput} |
62 | 89 | disabled={loading} |
63 | | - > |
64 | | - </input> |
65 | | - <div style={{ marginLeft: '5px', marginTop: '5px' }}> |
66 | | - <KeyboardReturn /> |
| 90 | + /> |
| 91 | + |
| 92 | + <KeyboardReturn style={{ marginLeft: '5px', marginTop: '5px' }} /> |
| 93 | + </div> |
| 94 | + |
| 95 | + <div style={{ marginTop: '2rem', display: 'flex', flexDirection: 'column', alignItems: 'center' }}> |
| 96 | + <input accept=".pdf,.txt,.csv" id="file-input" type="file" onChange={handleFileChange}/> |
| 97 | + {selectedFile && ( |
| 98 | + <Button onClick={handleFileUpload}> |
| 99 | + Upload |
| 100 | + </Button> |
| 101 | + )} |
67 | 102 | </div> |
68 | 103 | </Grid> |
69 | 104 | <Grid item xs={8}> |
|
0 commit comments