File size: 10,630 Bytes
2f35054 93d1827 1b3b6e1 2f35054 852dc0d 2f35054 852dc0d 2f35054 852dc0d 2f35054 ca67cfa 2f35054 852dc0d 2f35054 852dc0d 2f35054 ca67cfa 2656c1e 2f35054 852dc0d 2f35054 852dc0d 2f35054 852dc0d 2f35054 852dc0d 2f35054 852dc0d 2f35054 852dc0d 2f35054 91cc60b 2f35054 4a70176 2f35054 852dc0d 2f35054 93d1827 2f35054 852dc0d 2f35054 852dc0d 2f35054 852dc0d 2f35054 852dc0d 2f35054 852dc0d 2f35054 5541427 2f35054 5541427 2f35054 5541427 2f35054 852dc0d 2f35054 |
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
import { useState, useRef, useEffect, useCallback } from 'react'
import { Send, Eraser, Loader2, X } from 'lucide-react'
import {
ChatMessage,
TextGenerationWorkerInput,
WorkerMessage
} from '../../types'
import { useModel } from '../../contexts/ModelContext'
import { useTextGeneration } from '../../contexts/TextGenerationContext'
function TextGeneration() {
const { config, messages, setMessages } = useTextGeneration()
const [currentMessage, setCurrentMessage] = useState<string>('')
const [prompt, setPrompt] = useState<string>('')
const [generatedText, setGeneratedText] = useState<string>('')
const [isGenerating, setIsGenerating] = useState<boolean>(false)
const {
activeWorker,
status,
modelInfo,
hasBeenLoaded,
selectedQuantization
} = useModel()
const messagesEndRef = useRef<HTMLDivElement>(null)
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' })
}
useEffect(() => {
scrollToBottom()
}, [messages, generatedText])
const stopGeneration = useCallback(() => {
if (activeWorker && isGenerating) {
activeWorker.postMessage({ type: 'stop' })
setIsGenerating(false)
}
}, [activeWorker, isGenerating])
const handleSendMessage = useCallback(() => {
if (!currentMessage.trim() || !modelInfo || !activeWorker || isGenerating)
return
const userMessage: ChatMessage = {
role: 'user',
content: currentMessage.trim()
}
const updatedMessages = [...messages, userMessage]
setMessages(updatedMessages)
setCurrentMessage('')
setIsGenerating(true)
const message: TextGenerationWorkerInput = {
type: 'generate',
messages: updatedMessages,
hasChatTemplate: modelInfo.hasChatTemplate,
model: modelInfo.id,
dtype: selectedQuantization ?? 'fp32',
config
}
activeWorker.postMessage(message)
}, [
currentMessage,
messages,
setMessages,
modelInfo,
activeWorker,
config,
isGenerating,
selectedQuantization
])
const handleGenerateText = useCallback(() => {
if (!prompt.trim() || !modelInfo || !activeWorker || isGenerating) return
setIsGenerating(true)
const message: TextGenerationWorkerInput = {
type: 'generate',
prompt: prompt.trim(),
hasChatTemplate: modelInfo.hasChatTemplate,
model: modelInfo.id,
config,
dtype: selectedQuantization ?? 'fp32'
}
activeWorker.postMessage(message)
}, [
prompt,
modelInfo,
activeWorker,
config,
isGenerating,
selectedQuantization
])
useEffect(() => {
if (!activeWorker) return
const onMessageReceived = (e: MessageEvent<WorkerMessage>) => {
const { status, output } = e.data
if (status === 'output' && output) {
setIsGenerating(false)
if (modelInfo?.hasChatTemplate) {
const assistantMessage: ChatMessage = {
role: 'assistant',
content: output.content
}
setMessages((prev) => [...prev, assistantMessage])
} else {
setGeneratedText(output.content)
}
} else if (status === 'ready' || status === 'error') {
setIsGenerating(false)
}
}
activeWorker.addEventListener('message', onMessageReceived)
return () => activeWorker.removeEventListener('message', onMessageReceived)
}, [activeWorker, modelInfo?.hasChatTemplate, setMessages])
const handleKeyPress = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault()
if (modelInfo?.hasChatTemplate) {
handleSendMessage()
} else {
handleGenerateText()
}
}
}
const clearChat = () => {
if (modelInfo?.hasChatTemplate) {
setMessages((prev) => prev.filter((msg) => msg.role === 'system'))
} else {
setPrompt('')
setGeneratedText('')
}
}
const busy = status !== 'ready' || isGenerating
const hasChatTemplate = modelInfo?.hasChatTemplate
return (
<div className="flex flex-col min-h-[30dvh] max-h-[calc(100dvh-128px)] w-full p-4">
<div className="flex items-center justify-between mb-4">
<h1 className="text-2xl font-bold">
Text Generation {hasChatTemplate ? '(Chat)' : ''}
</h1>
<div className="flex gap-2">
<button
onClick={clearChat}
className="p-2 bg-red-100 hover:bg-red-200 rounded-lg transition-colors"
title={hasChatTemplate ? 'Clear Chat' : 'Clear Text'}
>
<Eraser className="w-4 h-4" />
</button>
{isGenerating && (
<button
onClick={stopGeneration}
className="p-2 bg-orange-100 hover:bg-orange-200 rounded-lg transition-colors"
title="Stop Generation"
>
<X className="w-4 h-4" />
</button>
)}
</div>
</div>
{hasChatTemplate ? (
<>
<div className="flex-1 overflow-y-auto border border-gray-300 rounded-lg p-4 mb-4 bg-white">
<div className="space-y-4">
{messages
.filter((msg) => msg.role !== 'system')
.map((message, index) => (
<div
key={index}
className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}
>
<div
className={`max-w-[80%] p-3 rounded-lg ${
message.role === 'user'
? 'bg-blue-500 text-white'
: 'bg-gray-100 text-gray-800'
}`}
>
<div className="text-xs font-medium mb-1 opacity-70">
{message.role === 'user' ? 'You' : 'Assistant'}
</div>
<div className="whitespace-pre-wrap">
{message.content}
</div>
</div>
</div>
))}
{isGenerating && (
<div className="flex justify-start">
<div className="bg-gray-100 text-gray-800 p-3 rounded-lg">
<div className="text-xs font-medium mb-1 opacity-70">
Assistant
</div>
<div className="flex items-center space-x-2">
<Loader2 className="w-4 h-4 animate-spin" />
<div>Loading...</div>
</div>
</div>
</div>
)}
</div>
<div ref={messagesEndRef} />
</div>
<div className="flex gap-2">
<textarea
value={currentMessage}
onChange={(e) => setCurrentMessage(e.target.value)}
onKeyPress={handleKeyPress}
placeholder="Type your message... (Press Enter to send, Shift+Enter for new line)"
className="flex-1 p-3 border border-gray-300 rounded-lg resize-none focus:outline-hidden focus:ring-2 focus:ring-blue-500 focus:border-blue-500 disabled:bg-gray-100 disabled:cursor-not-allowed"
rows={2}
disabled={!hasBeenLoaded || isGenerating}
/>
<button
onClick={handleSendMessage}
disabled={!currentMessage.trim() || busy || !hasBeenLoaded}
className="px-4 py-2 bg-blue-500 hover:bg-blue-600 disabled:bg-gray-300 disabled:cursor-not-allowed text-white rounded-lg transition-colors flex items-center justify-center"
>
{isGenerating ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
<Send className="w-4 h-4" />
)}
</button>
</div>
</>
) : (
<>
<div className="mb-4">
<label className="block text-sm font-medium text-gray-700 mb-2">
Enter your prompt:
</label>
<textarea
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
onKeyPress={handleKeyPress}
placeholder="Enter your text prompt here... (Press Enter to generate, Shift+Enter for new line)"
className="w-full p-3 border border-gray-300 rounded-lg resize-none focus:outline-hidden focus:ring-2 focus:ring-blue-500 focus:border-blue-500 disabled:bg-gray-100 disabled:cursor-not-allowed"
rows={4}
disabled={!hasBeenLoaded || isGenerating}
/>
</div>
<div className="mb-4">
<button
onClick={handleGenerateText}
disabled={!prompt.trim() || busy || !hasBeenLoaded}
className="px-6 py-2 bg-green-500 hover:bg-green-600 disabled:bg-gray-300 disabled:cursor-not-allowed text-white rounded-lg transition-colors flex items-center gap-2"
>
{isGenerating ? (
<>
<Loader2 className="w-4 h-4 animate-spin" />
Generating...
</>
) : (
<>
<Send className="w-4 h-4" />
Generate Text
</>
)}
</button>
</div>
<div className="flex-1 overflow-y-auto border border-gray-300 rounded-lg p-4 bg-white">
<div className="mb-2">
<label className="block text-sm font-medium text-gray-700">
Generated Text:
</label>
</div>
{generatedText ? (
<div className="whitespace-pre-wrap text-gray-800 bg-gray-50 p-3 rounded-sm border">
{generatedText}
</div>
) : (
<div className="text-gray-500 italic flex items-center gap-2">
{isGenerating ? (
<>
<Loader2 className="w-4 h-4 animate-spin" />
Generating text...
</>
) : (
'Generated text will appear here'
)}
</div>
)}
<div ref={messagesEndRef} />
</div>
</>
)}
{!hasBeenLoaded && (
<div className="text-center text-gray-500 text-sm mt-2">
Please load a model first to start{' '}
{hasChatTemplate ? 'chatting' : 'generating text'}
</div>
)}
</div>
)
}
export default TextGeneration
|