Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3399f09
Refactor Gemini Service to use API middleware
google-labs-jules[bot] Sep 26, 2025
bb22753
Merge pull request #1 from Techtutorialshub/feat/api-middleware-integ…
Techtutorialshub Sep 26, 2025
99f047a
feat: Add backend middleware for secure API calls
google-labs-jules[bot] Sep 26, 2025
d8ec064
Merge pull request #2 from Techtutorialshub/feat/api-middleware-integ…
Techtutorialshub Sep 26, 2025
84dd8b6
fix: Add production build process for server
google-labs-jules[bot] Sep 26, 2025
99b00f7
Merge pull request #3 from Techtutorialshub/feat/api-middleware-integ…
Techtutorialshub Sep 26, 2025
383bf83
fix: Convert server to CommonJS to resolve deployment error
google-labs-jules[bot] Sep 26, 2025
f80bb6d
Merge pull request #4 from Techtutorialshub/feat/api-middleware-integ…
Techtutorialshub Sep 26, 2025
6d2057c
fix: Resolve deployment error using .mjs extension
google-labs-jules[bot] Sep 26, 2025
38dee24
Merge pull request #5 from Techtutorialshub/feat/api-middleware-integ…
Techtutorialshub Sep 26, 2025
2f14b72
fix: Configure server to serve the frontend application
google-labs-jules[bot] Sep 26, 2025
9274696
Merge pull request #6 from Techtutorialshub/feat/api-middleware-integ…
Techtutorialshub Sep 26, 2025
0087829
fix: Correct catch-all route to resolve PathError
google-labs-jules[bot] Sep 26, 2025
7328ddf
Merge pull request #7 from Techtutorialshub/feat/api-middleware-integ…
Techtutorialshub Sep 26, 2025
1cbc85d
fix: Implement correct catch-all route syntax for Express v5+
google-labs-jules[bot] Sep 26, 2025
e516c69
Merge pull request #8 from Techtutorialshub/feat/api-middleware-integ…
Techtutorialshub Sep 26, 2025
9ae2a65
fix: Use RegEx for catch-all route to resolve deployment crash
google-labs-jules[bot] Sep 26, 2025
9f5acf5
Merge pull request #9 from Techtutorialshub/feat/api-middleware-integ…
Techtutorialshub Sep 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type": "module"}
118 changes: 118 additions & 0 deletions dist-server/server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import express from 'express';
import { GoogleGenAI } from '@google/genai';
const app = express();
const port = 3001;
app.use(express.json({ limit: '50mb' }));
// Helper to get the Gemini API key from environment variables
function getApiKey() {
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
throw new Error('GEMINI_API_KEY is not set in environment variables.');
}
return apiKey;
}
// Helper to build the edit prompt
function buildEditPrompt(instruction, hasMask) {
const maskInstruction = hasMask
? "\n\nIMPORTANT: Apply changes ONLY where the mask image shows white pixels (value 255). Leave all other areas completely unchanged. Respect the mask boundaries precisely and maintain seamless blending at the edges."
: "";
return `Edit this image according to the following instruction: ${instruction}

Maintain the original image's lighting, perspective, and overall composition. Make the changes look natural and seamlessly integrated.${maskInstruction}

Preserve image quality and ensure the edit looks professional and realistic.`;
}
// Helper to build the segmentation prompt
function buildSegmentationPrompt(query) {
return `Analyze this image and create a segmentation mask for: ${query}

Return a JSON object with this exact structure:
{
"masks": [
{
"label": "description of the segmented object",
"box_2d": [x, y, width, height],
"mask": "base64-encoded binary mask image"
}
]
}

Only segment the specific object or region requested. The mask should be a binary PNG where white pixels (255) indicate the selected region and black pixels (0) indicate the background.`;
}
app.post('/api/generate', async (req, res) => {
try {
const { prompt, referenceImages } = req.body;
const genAI = new GoogleGenAI({ apiKey: getApiKey() });
const contents = [{ text: prompt }];
if (referenceImages && referenceImages.length > 0) {
referenceImages.forEach((image) => {
contents.push({ inlineData: { mimeType: "image/png", data: image } });
});
}
const result = await genAI.models.generateContent({
model: "gemini-2.5-flash-image-preview",
contents,
});
const images = result.candidates[0].content.parts
.filter(part => part.inlineData)
.map(part => part.inlineData.data);
res.json({ images });
}
catch (error) {
console.error('Error in /api/generate:', error);
res.status(500).json({ error: error.message });
}
});
app.post('/api/edit', async (req, res) => {
try {
const { instruction, originalImage, referenceImages, maskImage } = req.body;
const genAI = new GoogleGenAI({ apiKey: getApiKey() });
const contents = [
{ text: buildEditPrompt(instruction, !!maskImage) },
{ inlineData: { mimeType: "image/png", data: originalImage } },
];
if (referenceImages && referenceImages.length > 0) {
referenceImages.forEach((image) => {
contents.push({ inlineData: { mimeType: "image/png", data: image } });
});
}
if (maskImage) {
contents.push({ inlineData: { mimeType: "image/png", data: maskImage } });
}
const result = await genAI.models.generateContent({
model: "gemini-2.5-flash-image-preview",
contents,
});
const images = result.candidates[0].content.parts
.filter(part => part.inlineData)
.map(part => part.inlineData.data);
res.json({ images });
}
catch (error) {
console.error('Error in /api/edit:', error);
res.status(500).json({ error: error.message });
}
});
app.post('/api/segment', async (req, res) => {
try {
const { image, query } = req.body;
const genAI = new GoogleGenAI({ apiKey: getApiKey() });
const contents = [
{ text: buildSegmentationPrompt(query) },
{ inlineData: { mimeType: "image/png", data: image } },
];
const result = await genAI.models.generateContent({
model: "gemini-2.5-flash-image-preview",
contents,
});
const responseText = result.candidates[0].content.parts[0].text;
res.json(JSON.parse(responseText));
}
catch (error) {
console.error('Error in /api/segment:', error);
res.status(500).json({ error: error.message });
}
});
app.listen(port, () => {
console.log(`Backend server listening on http://localhost:${port}`);
});
Loading