-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.tsx
67 lines (59 loc) · 1.83 KB
/
page.tsx
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
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card } from "@/components/ui/card";
export default function HomePage() {
const [prompt, setPrompt] = useState('');
const [image, setImage] = useState('');
const [error, setError] = useState('');
const [isLoading, setIsLoading] = useState(false);
const generateImage = async () => {
setIsLoading(true);
setError('');
setImage('');
try {
const response = await fetch('/api/proxy', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ prompt }),
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || 'Failed to generate image');
}
console.log(data);
setImage(data.output[0]);
} catch (err) {
console.error('Error generating image:', err);
setError(err.message || 'An error occurred while generating the image');
} finally {
setIsLoading(false);
}
};
return (
<div className="container mx-auto p-4">
<h1 className="text-2xl font-bold mb-4">Generate Image</h1>
<div className="flex space-x-2 mb-4">
<Input
type="text"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
placeholder="Enter prompt"
disabled={isLoading}
/>
<Button onClick={generateImage} disabled={isLoading}>
{isLoading ? 'Generating...' : 'Generate'}
</Button>
</div>
{error && <p className="text-red-500 mb-4">{error}</p>}
{image && (
<Card className="p-4">
<img src={image} alt="Generated" className="w-full h-auto" />
</Card>
)}
</div>
);
}