Spaces:
Sleeping
Sleeping
File size: 5,354 Bytes
56fda74 |
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import React, { useState, useRef, useEffect } from 'react';
import {
Box,
Paper,
TextField,
Button,
Typography,
CircularProgress,
Divider,
List,
ListItem,
ListItemText,
Collapse,
IconButton
} from '@mui/material';
import SendIcon from '@mui/icons-material/Send';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import ExpandLessIcon from '@mui/icons-material/ExpandLess';
import axios from 'axios';
const Message = ({ content, isUser, context }) => {
const [expanded, setExpanded] = useState(false);
return (
<Box
sx={{
display: 'flex',
flexDirection: 'column',
mb: 2,
alignItems: isUser ? 'flex-end' : 'flex-start',
}}
>
<Paper
elevation={1}
sx={{
p: 2,
maxWidth: '80%',
backgroundColor: isUser ? 'primary.light' : 'background.paper',
color: isUser ? 'primary.contrastText' : 'text.primary',
}}
>
<Typography variant="body1">{content}</Typography>
</Paper>
{context && context.length > 0 && (
<Box sx={{ width: '100%', mt: 1 }}>
<Button
size="small"
endIcon={expanded ? <ExpandLessIcon /> : <ExpandMoreIcon />}
onClick={() => setExpanded(!expanded)}
sx={{ color: 'text.secondary' }}
>
{expanded ? 'Hide Context' : 'Show Context'}
</Button>
<Collapse in={expanded}>
<Paper variant="outlined" sx={{ mt: 1, p: 1 }}>
<Typography variant="subtitle2" gutterBottom>
Context used for this answer:
</Typography>
<List dense>
{context.map((item, index) => (
<ListItem key={index} divider={index < context.length - 1}>
<ListItemText
primary={item[0].substring(0, 100) + (item[0].length > 100 ? '...' : '')}
secondary={`Relevance: ${(item[1] * 100).toFixed(2)}%`}
/>
</ListItem>
))}
</List>
</Paper>
</Collapse>
</Box>
)}
</Box>
);
};
const ChatInterface = ({ fileName, isProcessing }) => {
const [messages, setMessages] = useState([]);
const [input, setInput] = useState('');
const [loading, setLoading] = useState(false);
const messagesEndRef = useRef(null);
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
};
useEffect(() => {
scrollToBottom();
}, [messages]);
const handleSend = async () => {
if (!input.trim()) return;
const userMessage = input.trim();
setInput('');
setMessages(prev => [...prev, { content: userMessage, isUser: true }]);
setLoading(true);
try {
const response = await axios.post('http://localhost:8000/ask', {
question: userMessage
});
setMessages(prev => [
...prev,
{
content: response.data.response,
isUser: false,
context: response.data.context
}
]);
} catch (error) {
console.error('Error asking question:', error);
setMessages(prev => [
...prev,
{
content: 'Sorry, there was an error processing your question. Please try again.',
isUser: false
}
]);
} finally {
setLoading(false);
}
};
const handleKeyPress = (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
return (
<Paper elevation={3} sx={{ p: 3, height: '70vh', display: 'flex', flexDirection: 'column' }}>
<Typography variant="h6" gutterBottom>
Chat about {fileName}
</Typography>
<Divider sx={{ mb: 2 }} />
<Box sx={{ flexGrow: 1, overflow: 'auto', mb: 2 }}>
{messages.length === 0 ? (
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%' }}>
<Typography variant="body1" color="text.secondary">
Ask a question about the document to get started
</Typography>
</Box>
) : (
messages.map((message, index) => (
<Message
key={index}
content={message.content}
isUser={message.isUser}
context={message.context}
/>
))
)}
<div ref={messagesEndRef} />
</Box>
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<TextField
fullWidth
variant="outlined"
placeholder="Ask a question about the document..."
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={handleKeyPress}
disabled={loading || isProcessing}
multiline
maxRows={3}
sx={{ mr: 1 }}
/>
<IconButton
color="primary"
onClick={handleSend}
disabled={loading || isProcessing || !input.trim()}
sx={{ p: 1 }}
>
{loading ? <CircularProgress size={24} /> : <SendIcon />}
</IconButton>
</Box>
</Paper>
);
};
export default ChatInterface; |