Spaces:
Build error
Build error
Refactor TextGeneration.tsx to remove useChat and handle state manually
Browse files
components/app/TextGeneration.tsx
CHANGED
|
@@ -1,23 +1,50 @@
|
|
| 1 |
'use client';
|
| 2 |
|
| 3 |
import { useState } from 'react';
|
| 4 |
-
import {
|
| 5 |
|
| 6 |
export default function TextGeneration() {
|
| 7 |
const [model, setModel] = useState('alias-code');
|
| 8 |
-
const [
|
| 9 |
-
const
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
};
|
| 20 |
-
|
| 21 |
return (
|
| 22 |
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">
|
| 23 |
<div className="flex justify-between items-center mb-4">
|
|
@@ -35,10 +62,10 @@ export default function TextGeneration() {
|
|
| 35 |
</select>
|
| 36 |
</div>
|
| 37 |
<div className="flex-grow overflow-y-auto mb-4">
|
| 38 |
-
{messages.map(m => (
|
| 39 |
-
<div key={
|
| 40 |
<strong>{m.role === 'user' ? 'User: ' : 'AI: '}</strong>
|
| 41 |
-
{m.content}
|
| 42 |
</div>
|
| 43 |
))}
|
| 44 |
</div>
|
|
@@ -46,9 +73,9 @@ export default function TextGeneration() {
|
|
| 46 |
<form onSubmit={handleSubmit} className="flex-shrink-0">
|
| 47 |
<input
|
| 48 |
className="w-full max-w-md p-2 border border-gray-300 rounded shadow-xl"
|
| 49 |
-
value={
|
| 50 |
placeholder="Say something..."
|
| 51 |
-
onChange={
|
| 52 |
/>
|
| 53 |
</form>
|
| 54 |
</div>
|
|
|
|
| 1 |
'use client';
|
| 2 |
|
| 3 |
import { useState } from 'react';
|
| 4 |
+
import { type CoreMessage } from 'ai';
|
| 5 |
|
| 6 |
export default function TextGeneration() {
|
| 7 |
const [model, setModel] = useState('alias-code');
|
| 8 |
+
const [messages, setMessages] = useState<CoreMessage[]>([]);
|
| 9 |
+
const [input, setInput] = useState('');
|
| 10 |
+
|
| 11 |
+
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
| 12 |
+
e.preventDefault();
|
| 13 |
+
if (!input) return;
|
| 14 |
+
|
| 15 |
+
const newMessages: CoreMessage[] = [...messages, { role: 'user', content: input }];
|
| 16 |
+
setMessages(newMessages);
|
| 17 |
+
setInput('');
|
| 18 |
+
|
| 19 |
+
const response = await fetch('/api/text-generation', {
|
| 20 |
+
method: 'POST',
|
| 21 |
+
headers: {
|
| 22 |
+
'Content-Type': 'application/json',
|
| 23 |
+
},
|
| 24 |
+
body: JSON.stringify({
|
| 25 |
+
prompt: input,
|
| 26 |
+
model,
|
| 27 |
+
}),
|
| 28 |
+
});
|
| 29 |
+
|
| 30 |
+
if (!response.body) {
|
| 31 |
+
return;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
const reader = response.body.getReader();
|
| 35 |
+
const decoder = new TextDecoder();
|
| 36 |
+
let done = false;
|
| 37 |
+
|
| 38 |
+
let fullResponse = '';
|
| 39 |
+
while (!done) {
|
| 40 |
+
const { value, done: readerDone } = await reader.read();
|
| 41 |
+
done = readerDone;
|
| 42 |
+
const chunkValue = decoder.decode(value);
|
| 43 |
+
fullResponse += chunkValue;
|
| 44 |
+
setMessages([...newMessages, { role: 'assistant', content: fullResponse }]);
|
| 45 |
+
}
|
| 46 |
};
|
| 47 |
+
|
| 48 |
return (
|
| 49 |
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">
|
| 50 |
<div className="flex justify-between items-center mb-4">
|
|
|
|
| 62 |
</select>
|
| 63 |
</div>
|
| 64 |
<div className="flex-grow overflow-y-auto mb-4">
|
| 65 |
+
{messages.map((m, i) => (
|
| 66 |
+
<div key={i} className="whitespace-pre-wrap p-4 border-b border-gray-200">
|
| 67 |
<strong>{m.role === 'user' ? 'User: ' : 'AI: '}</strong>
|
| 68 |
+
{m.content as string}
|
| 69 |
</div>
|
| 70 |
))}
|
| 71 |
</div>
|
|
|
|
| 73 |
<form onSubmit={handleSubmit} className="flex-shrink-0">
|
| 74 |
<input
|
| 75 |
className="w-full max-w-md p-2 border border-gray-300 rounded shadow-xl"
|
| 76 |
+
value={input}
|
| 77 |
placeholder="Say something..."
|
| 78 |
+
onChange={e => setInput(e.target.value)}
|
| 79 |
/>
|
| 80 |
</form>
|
| 81 |
</div>
|