Gertie2013 commited on
Commit
4e3ec66
·
verified ·
1 Parent(s): 8c2b219

Create app/page.tsx

Browse files
Files changed (1) hide show
  1. app/page.tsx +75 -0
app/page.tsx ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "use client";
2
+
3
+ import { useState } from "react";
4
+
5
+ export default function Home() {
6
+ const [prompt, setPrompt] = useState("");
7
+ const [image, setImage] = useState<string | null>(null);
8
+ const [loading, setLoading] = useState(false);
9
+ const [error, setError] = useState<string | null>(null);
10
+
11
+ const generate = async () => {
12
+ setLoading(true);
13
+ setError(null);
14
+ setImage(null);
15
+
16
+ try {
17
+ const res = await fetch("/api/image", {
18
+ method: "POST",
19
+ headers: { "Content-Type": "application/json" },
20
+ body: JSON.stringify({ prompt })
21
+ });
22
+
23
+ if (!res.ok) {
24
+ const data = await res.json().catch(() => ({}));
25
+ throw new Error(data.error || `Request failed: ${res.status}`);
26
+ }
27
+
28
+ const data = await res.json();
29
+ setImage(`data:image/png;base64,${data.image}`);
30
+ } catch (e: any) {
31
+ setError(e.message || "Unknown error");
32
+ } finally {
33
+ setLoading(false);
34
+ }
35
+ };
36
+
37
+ return (
38
+ <main style={{ padding: 24, maxWidth: 800, margin: "0 auto" }}>
39
+ <h1>Gemini 2.0 Flash Exp – Image Generation</h1>
40
+ <p>Backend: Python (google-genai) · Frontend: Next.js</p>
41
+
42
+ <textarea
43
+ style={{ width: "100%", height: 100, marginTop: 16 }}
44
+ placeholder="Describe the image you want to generate..."
45
+ value={prompt}
46
+ onChange={(e) => setPrompt(e.target.value)}
47
+ />
48
+
49
+ <button
50
+ style={{ marginTop: 16, padding: "8px 16px" }}
51
+ onClick={generate}
52
+ disabled={loading || !prompt.trim()}
53
+ >
54
+ {loading ? "Generating..." : "Generate Image"}
55
+ </button>
56
+
57
+ {error && (
58
+ <p style={{ color: "red", marginTop: 16 }}>
59
+ Error: {error}
60
+ </p>
61
+ )}
62
+
63
+ {image && (
64
+ <div style={{ marginTop: 24 }}>
65
+ <h2>Result</h2>
66
+ <img
67
+ src={image}
68
+ alt="Generated"
69
+ style={{ maxWidth: "100%", border: "1px solid #ccc" }}
70
+ />
71
+ </div>
72
+ )}
73
+ </main>
74
+ );
75
+ }