-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
279 lines (242 loc) · 9.65 KB
/
server.ts
File metadata and controls
279 lines (242 loc) · 9.65 KB
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
import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import { GoogleGenAI, Type } from "@google/genai";
import dotenv from "dotenv";
import cors from "cors";
dotenv.config();
const app = express();
const PORT = 3000;
// Middleware
app.use(cors());
app.use(express.json({ limit: '10mb' }));
// Gemini Initialization Helper
const getAI = () => {
let apiKey = process.env.GEMINI_API_KEY || process.env.API_KEY || process.env.GOOGLE_API_KEY;
if (!apiKey || apiKey === "MY_GEMINI_API_KEY" || apiKey === "your-api-key") {
throw new Error("Gemini API Key is missing. Please go to the 'Settings' menu (gear icon) -> 'Secrets' and add a variable named GEMINI_API_KEY with your key from https://aistudio.google.com/app/apikey");
}
// Sanitize the key (remove quotes, whitespace, or accidental prefixes)
apiKey = apiKey.trim().replace(/^['"]|['"]$/g, '');
if (apiKey.includes('=')) {
apiKey = apiKey.split('=')[1].trim().replace(/^['"]|['"]$/g, '');
}
if (!apiKey.startsWith("AIza")) {
throw new Error("The provided GEMINI_API_KEY does not appear to be a valid Google API key (should start with 'AIza'). Please check your key in Settings > Secrets.");
}
console.log(`[AI] Initializing with key length: ${apiKey.length}, prefix: ${apiKey.substring(0, 4)}...`);
return new GoogleGenAI({ apiKey });
};
// Simple Rate Limiting (In-memory for MVP)
const usageStore = new Map<string, { count: number, date: string }>();
const checkRateLimit = (req: express.Request, res: express.Response, next: express.NextFunction) => {
const ip = req.ip || 'anonymous';
const today = new Date().toISOString().split('T')[0];
const usage = usageStore.get(ip) || { count: 0, date: today };
if (usage.date !== today) {
usage.count = 0;
usage.date = today;
}
if (usage.count >= 3) {
return res.status(429).json({ error: "Daily limit of 3 generations reached. Please try again tomorrow." });
}
usageStore.set(ip, usage);
next();
};
// API routes FIRST
app.get("/api/health", (req, res) => {
const keyDetected = !!(process.env.GEMINI_API_KEY || process.env.API_KEY || process.env.GOOGLE_API_KEY);
res.json({
status: "ok",
env: process.env.NODE_ENV,
keyDetected
});
});
// API Routes
app.post(["/api/scrape", "/api/scrape/"], async (req, res) => {
const { url } = req.body;
console.log(`[API] Scrape request for URL: ${url}`);
if (!url) return res.status(400).json({ error: "URL is required" });
// If the URL is already an image, return it directly to avoid unnecessary AI calls
if (url.match(/\.(jpg|jpeg|png|webp|gif|avif)$/i) || url.includes('assets.myntassets.com')) {
console.log(`[API] Direct image URL detected, bypassing AI.`);
const fileName = url.split('/').pop()?.split('?')[0] || "Product";
const name = fileName
.replace(/-/g, ' ')
.replace(/_/g, ' ')
.replace(/\.(jpg|jpeg|png|webp|gif|avif)$/i, '')
.trim();
return res.json({
name: name || "Product Image",
category: "Clothing",
color: "Unknown",
imageUrl: url,
description: "Product image from URL",
brand: "Unknown"
});
}
try {
const ai = getAI();
console.log(`[API] Calling Gemini for scraping...`);
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
contents: `Extract product details from this e-commerce URL: ${url}.
Return the product name, category (e.g., Topwear, Bottomwear, Dress), primary color, a valid image URL for the product, and a brief description.`,
config: {
responseMimeType: "application/json",
responseSchema: {
type: Type.OBJECT,
properties: {
name: { type: Type.STRING },
category: { type: Type.STRING },
color: { type: Type.STRING },
imageUrl: { type: Type.STRING },
description: { type: Type.STRING },
brand: { type: Type.STRING },
},
required: ["name", "category", "color", "imageUrl", "description"],
},
tools: [{ urlContext: {} }]
},
});
const text = response.text;
if (!text) throw new Error("AI returned an empty response");
console.log(`[API] Gemini response received.`);
res.json(JSON.parse(text));
} catch (error: any) {
console.error("[API] Scrape Error:", error);
res.status(500).json({ error: error.message || "Failed to scrape product" });
}
});
app.post(["/api/generate", "/api/generate/"], checkRateLimit, async (req, res) => {
const { userImageBase64, productDetails, profile } = req.body;
const ip = req.ip || 'anonymous';
try {
const ai = getAI();
// Fetch product image as base64
const imgRes = await fetch(productDetails.imageUrl);
const blob = await imgRes.blob();
const buffer = await blob.arrayBuffer();
const productImageBase64 = Buffer.from(buffer).toString('base64');
const systemPrompt = `System Role: You are a professional high-fidelity fashion visualization and virtual try-on engine.
Task: Perform a virtual try-on by REPLACING the current clothing of the person in the FIRST image with the garment shown in the SECOND image.
1. CLOTHING REPLACEMENT (MANDATORY): You MUST remove the existing top/bottom/dress the person is wearing in the first image. Replace it COMPLETELY with the garment from the second image.
2. IDENTITY PRESERVATION: The person in the generated image MUST be the EXACT same individual from the first image.
3. Anatomy & Fit: Adjust the garment's fit to match the user's ${profile.bodyType} build and ${profile.height} height.
4. Garment Fidelity: Maintain the exact ${productDetails.color}, pattern, and texture of the garment from the second image.
5. Pose & Background: Maintain the original pose and background from the first image exactly.`;
const userPrompt = `
Virtual Try-On Request:
- User: ${profile.gender}, ${profile.height}, ${profile.bodyType} build.
- Product: ${productDetails.brand || 'Brand'} ${productDetails.name} (${productDetails.category}).
- Action: Replace the user's current outfit with this ${productDetails.color} ${productDetails.category}.
`;
const parts: any[] = [
{ text: systemPrompt + userPrompt },
{
inlineData: {
data: userImageBase64.split(',')[1],
mimeType: "image/png",
},
},
{
inlineData: {
data: productImageBase64,
mimeType: "image/png",
},
}
];
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash-image',
contents: { parts },
});
let generatedImage = "";
for (const part of response.candidates?.[0]?.content?.parts || []) {
if (part.inlineData) {
generatedImage = `data:image/png;base64,${part.inlineData.data}`;
break;
}
}
if (!generatedImage) throw new Error("No image generated");
// Increment usage count
const usage = usageStore.get(ip)!;
usage.count++;
usageStore.set(ip, usage);
res.json({ image: generatedImage });
} catch (error: any) {
console.error("Generate Error:", error);
res.status(500).json({ error: error.message || "Failed to generate image" });
}
});
app.post(["/api/analyze", "/api/analyze/"], async (req, res) => {
const { generatedImageBase64, productDetails, profile } = req.body;
try {
const ai = getAI();
const prompt = `Compare the generated try-on image with the user's provided measurements (${profile.height}/${profile.weight}).
Identify areas where the garment might feel tight or loose.
Provide a 'Fit Score' out of 100, size recommendation, 3 styling tips, and 2-3 fit badges.
User Profile: ${JSON.stringify(profile)}
Product: ${productDetails.name} (${productDetails.category})`;
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
contents: {
parts: [
{
inlineData: {
data: generatedImageBase64.split(',')[1],
mimeType: "image/png",
},
},
{
text: prompt,
},
],
},
config: {
responseMimeType: "application/json",
responseSchema: {
type: Type.OBJECT,
properties: {
fitScore: { type: Type.NUMBER },
sizeRecommendation: { type: Type.STRING },
stylingTips: {
type: Type.ARRAY,
items: { type: Type.STRING }
},
fitBadges: {
type: Type.ARRAY,
items: { type: Type.STRING }
},
},
required: ["fitScore", "sizeRecommendation", "stylingTips", "fitBadges"],
},
},
});
res.json(JSON.parse(response.text));
} catch (error: any) {
console.error("Analyze Error:", error);
res.status(500).json({ error: error.message || "Failed to analyze fit" });
}
});
// Vite Integration
async function startServer() {
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), 'dist');
app.use(express.static(distPath));
app.get('*', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
const keyDetected = !!(process.env.GEMINI_API_KEY || process.env.API_KEY || process.env.GOOGLE_API_KEY);
console.log(`[AI] Gemini API Key detected: ${keyDetected}`);
});
}
startServer();