All AI text extraction now uses Google Gemini instead of OpenAI!
Changes:
- ✅ Fixed image handling (was broken)
- ✅ Proper base64 encoding with Deno
- ✅ Correct API call structure for Gemini
- ✅ Better error handling
- ✅ Default model:
gemini-1.5-flash
Before:
// Returned hardcoded "empty for now"
const text = "empty for now";After:
// Actually calls Gemini and returns real response
const response = result.response;
const text = response.text();Changes:
- ✅ Uses Gemini instead of OpenAI
- ✅ Advanced OCR prompt from Resources (detailed instructions)
- ✅ Parses numbered text blocks with coordinates
- ✅ Creates multiple extraction results (one per text block)
- ✅ Extracts coordinates for each text region
- ✅ Smart text grouping (merges related text)
- ✅ Supports multiple languages
Key Features:
- Numbers each text block found
- Provides coordinates: (from: {x, y}, to: {x, y})
- Merges related text (titles, phrases)
- Works for English, Chinese, and other languages
- Uses actual image dimensions
- No guessing or external knowledge
parseNumberedTextList() // Extracts: ["Text 1", "Text 2", ...]
parseCoordinatesList() // Extracts: [{fromCoord, toCoord}, ...]Before: 1 extraction result with all text After: N extraction results, one per text block with precise coordinates
- Go to: https://aistudio.google.com/app/apikey
- Sign in with your Google account
- Click "Create API Key"
- Copy the key
Windows (PowerShell):
$env:GEMINI_API_KEY="your-api-key-here"
$env:GEMINI_MODEL="gemini-1.5-flash"Linux/Mac (Bash):
export GEMINI_API_KEY="your-api-key-here"
export GEMINI_MODEL="gemini-1.5-flash"Optional Config File:
$env:GEMINI_CONFIG="path/to/config.json"gemini-1.5-flash- Fastest, cheapest (recommended)gemini-1.5-pro- More accurate, slowergemini-pro-vision- Vision-specific
1. User clicks "Auto Extract Text"
↓
2. Backend reads image from ./uploads/
↓
3. Gets image dimensions (for coordinate accuracy)
↓
4. Builds detailed OCR prompt with dimensions
↓
5. Calls Gemini with prompt + image
↓
6. Gemini analyzes and returns:
"1: Lion King (from: {x:100, y:50}, to: {x:300, y:80})
2: Mufasa (from: {x:120, y:120}, to: {x:250, y:150})
Number of text blocks: 2"
↓
7. Backend parses response into text blocks
↓
8. Creates separate extraction for each block
↓
9. Stores with coordinates in database
↓
10. Frontend displays all extractions
Gemini returns:
1: The Lion King (from: {x:100, y:50}, to: {x:300, y:80})
2: Simba (from: {x:120, y:120}, to: {x:250, y:150})
3: 2025 Festival (from: {x:80, y:200}, to: {x:280, y:240})
Number of text blocks: 3
Backend creates:
[
{
extractedText: "The Lion King",
fromCoord: [100, 50],
toCoord: [300, 80]
},
{
extractedText: "Simba",
fromCoord: [120, 120],
toCoord: [250, 150]
},
{
extractedText: "2025 Festival",
fromCoord: [80, 200],
toCoord: [280, 240]
}
]Required:
- ✅
src/gemini-llm.ts- Copy to your backend'ssrc/folder - ✅
concepts/TextExtraction/TextExtraction.ts- Copy toconcepts/TextExtraction/
Commands:
# From TEPKonjacFrontEnd directory
# Copy Gemini LLM
cp src/gemini-llm.ts /path/to/backend/src/
# Copy TextExtraction
cp concepts/TextExtraction/TextExtraction.ts /path/to/backend/concepts/TextExtraction/# 1. Set API key
$env:GEMINI_API_KEY="your-key"
$env:GEMINI_MODEL="gemini-1.5-flash"
# 2. Start backend
deno run --allow-net --allow-read --allow-write --allow-sys --allow-env src/concept_server.ts --port 8000 --baseUrl /api
# 3. Check logs for
# ✅ No "Missing GEMINI_API_KEY" error# 1. Upload an image with text (e.g., movie poster, book cover)
# 2. Click "Edit Image"
# 3. Click "Auto Extract Text"
# 4. Backend terminal should show:
🤖 Starting Gemini AI text extraction for: image.png
🤖 Calling Gemini AI...
📷 Reading image from: ./uploads/...
✅ Gemini response received
✅ Gemini extraction complete
📝 Found 3 text blocks
✅ Created 3 extraction records
# 5. Frontend shows multiple extractions (one per text block)# Check database for extraction results
# Each should have:
# - extractedText: "Actual text from image"
# - position: Location ID
# - Location document has fromCoord and toCoordYou can pass custom prompts for specific extraction needs:
// Example: Extract only titles
extractTextFromMedia({
userId,
mediaId,
prompt: "Extract only titles and headings from this image, numbered with coordinates"
})
// Example: Extract specific language
extractTextFromMedia({
userId,
mediaId,
prompt: "Extract all Chinese text from this image, numbered with coordinates"
})Gemini automatically merges related text:
❌ Before: "Lion", "King" (split)
✅ After: "Lion King" (merged)
❌ Before: "2025", "Festival" (split)
✅ After: "2025 Festival" (merged)
Works with:
- English
- Chinese (Simplified & Traditional)
- Japanese
- Korean
- Spanish, French, German, etc.
| Feature | Gemini Flash | OpenAI GPT-4V |
|---|---|---|
| Cost/1K images | ~$0.20 | ~$20-$30 |
| Speed | Fast (2-3s) | Slower (5-7s) |
| Accuracy | High | Very High |
| Free Tier | 15 req/min | None |
| Best For | Production | High accuracy |
Recommendation: Use gemini-1.5-flash for cost-effective production use!
Solution:
# Set the environment variable
$env:GEMINI_API_KEY="your-key-here"
# Verify it's set
echo $env:GEMINI_API_KEY
# Restart backendCheck:
// In TextExtraction.ts, verify import path:
import { GeminiLLM } from "../../src/gemini-llm.ts";
// Adjust based on your backend structureCauses:
- Image file not found
- Gemini returned "No text found"
- Parsing failed
Debug:
# Check backend logs for:
🤖 Starting Gemini AI text extraction...
✅ Gemini response received
📝 Found 0 text blocks # ← This means no text detectedFree tier limits:
- 15 requests/minute
- 1,500 requests/day
Solutions:
- Wait 1 minute between extractions
- Upgrade to paid plan
- Use
gemini-1.5-flash(higher limits)
Backend logs show:
🤖 Starting Gemini AI text extraction for: image.png
📐 Image dimensions: 1024x768
🤖 Calling Gemini AI...
📷 Reading image from: ./uploads/user:xxx/folder/image.png
✅ Gemini response received
✅ Gemini extraction complete
📝 Found 3 text blocks
✅ Created 3 extraction records
Add this to see raw Gemini output:
// In extractTextFromMedia, after line 196:
console.log("Raw Gemini response:", aiResponse);- Set
GEMINI_API_KEYenvironment variable - Copy
gemini-llm.tsto backendsrc/ - Copy
TextExtraction.tsto backendconcepts/TextExtraction/ - Verify import paths are correct
- Install dependencies:
npm:@google/genai - Restart backend with
--allow-env - Test with an image that has text
- Verify multiple extractions are created
- Check coordinates are accurate
| Feature | Before (OpenAI) | After (Gemini) |
|---|---|---|
| AI Provider | OpenAI GPT-4V | Google Gemini |
| Cost | ~$0.01/image | ~$0.0002/image |
| Speed | 5-7 seconds | 2-3 seconds |
| Coordinates | Placeholder (0,0) | Real coordinates |
| Extractions | 1 per image | N per text block |
| Grouping | No | Yes (smart) |
| Languages | English | All languages |
| Free Tier | None | 1,500/day |
Result: 50x cheaper, 2x faster, better accuracy! 🎉
- Copy the files to your backend
- Set GEMINI_API_KEY
- Restart backend
- Test extraction
- Enjoy accurate, cheap OCR!
For questions or issues, check the backend terminal logs for detailed error messages.
Happy extracting! 🚀