This document provides curl examples for using the image transcription functionality via the REST API.
-
Start the API server:
python api_server.py # or python run_api.pyThe server will run on
http://localhost:8000by default. -
Ensure API keys are configured in your
.envfile:OPENAI_API_KEY=your_openai_key_here GOOGLE_API_KEY=your_google_key_here
-
Have image URLs ready for testing
Endpoint: POST /transcribe-image-url
Description: Provide an image URL and get structured JSON with title, description, and extracted text.
Endpoint: POST /transcribe-image
Description: Upload an image file and get structured JSON with title, description, and extracted text.
curl -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/photo.jpg"}'curl -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/image", "filename": "my-photo.jpg"}'curl -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/document.png"}' \
-o image_result.jsoncurl -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/screenshot.jpg"}' | jq '.'curl -s -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/image.png"}'curl -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-H "User-Agent: MyApp/1.0" \
-H "Accept: application/json" \
-d '{"url": "https://example.com/photo.jpg"}'# Stock photo
curl -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://picsum.photos/800/600"}'
# GitHub raw image
curl -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://raw.githubusercontent.com/user/repo/main/image.jpg"}'
# Direct image URL
curl -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://i.imgur.com/example.png"}'# Upload local file instead of URL
curl -X POST "http://localhost:8000/transcribe-image" \
-F "file=@local-photo.jpg"{
"file_id": "uuid-string",
"file_info": {
"name": "photo.jpg",
"extension": ".jpg",
"size_mb": 2.5,
"mime_type": "image/jpeg"
},
"success": true,
"error": null,
"title": "Modern Office Meeting Room",
"description": "A contemporary meeting room with a large wooden conference table surrounded by black office chairs. The room features floor-to-ceiling windows with natural light, white walls, and a large flat-screen display mounted on the wall.",
"extracted_text": "QUARTERLY REVIEW\nQ3 2024 Results\nMeeting at 2:00 PM",
"processor_used": "OpenAI Vision",
"processing_time": 3.24,
"timestamp": "2024-01-15T10:30:45.123456"
}{
"file_id": "uuid-string",
"file_info": {
"name": "document.pdf",
"extension": ".pdf",
"size_mb": 1.2,
"mime_type": "application/pdf"
},
"success": false,
"error": "Unsupported image format: .pdf. Supported: .bmp, .gif, .jpg, .jpeg, .png, .tif, .tiff, .webp",
"title": null,
"description": null,
"extracted_text": null,
"processor_used": null,
"processing_time": 0.1,
"timestamp": "2024-01-15T10:30:45.123456"
}# Get only the title
curl -s -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/photo.jpg"}' | jq -r '.title'
# Get title and description
curl -s -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/photo.jpg"}' | jq '{title: .title, description: .description}'
# Get extracted text only
curl -s -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/document.jpg"}' | jq -r '.extracted_text'
# Check if processing was successful
curl -s -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/photo.jpg"}' | jq '.success'#!/bin/bash
response=$(curl -s -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d "{\"url\": \"$1\"}")
success=$(echo "$response" | jq -r '.success')
if [ "$success" = "true" ]; then
echo "Success! Title: $(echo "$response" | jq -r '.title')"
echo "Description: $(echo "$response" | jq -r '.description')"
else
echo "Error: $(echo "$response" | jq -r '.error')"
fi#!/bin/bash
# Process multiple image URLs from a file
while IFS= read -r url; do
if [ ! -z "$url" ]; then
echo "Processing: $url"
filename=$(basename "$url" | cut -d'?' -f1)
curl -s -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d "{\"url\": \"$url\"}" \
-o "${filename%.*}_result.json"
echo "Saved result to: ${filename%.*}_result.json"
fi
done < image_urls.txtcurl -s -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/large_image.jpg"}' | jq '.processing_time'curl -X GET "http://localhost:8000/health"curl -X POST "http://localhost:8000/extract" \
-F "file=@photo.jpg"curl -X POST "http://localhost:8000/transcribe-image-url" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/photo.jpg"}'# View interactive API docs
open http://localhost:8000/docs
# View ReDoc documentation
open http://localhost:8000/redoc# Error
curl: (26) Failed to open/read local data from file/application
# Solution: Check file path
ls -la photo.jpg
curl -X POST "http://localhost:8000/transcribe-image" \
-F "file=@./path/to/photo.jpg"# Error response:
{
"success": false,
"error": "Unsupported image format: .pdf"
}
# Solution: Use supported formats
curl -X POST "http://localhost:8000/transcribe-image" \
-F "file=@converted_image.jpg"# Error
curl: (7) Failed to connect to localhost port 8000
# Solution: Start the server
python api_server.py# For large files, increase timeout
curl --max-time 300 -X POST "http://localhost:8000/transcribe-image" \
-F "file=@large_image.jpg"# Error response:
{
"detail": "Image processor not available. Please check API key configuration."
}
# Solution: Check .env file has valid API keys
cat .env | grep -E "(OPENAI_API_KEY|GOOGLE_API_KEY)"- Use appropriate image sizes: Smaller images process faster
- Choose the right format: JPEG and PNG typically work best
- Monitor processing time: Use jq to extract
.processing_time - Batch processing: Process multiple images sequentially to avoid rate limits
- Use HTTPS in production: Replace
http://localhost:8000withhttps://your-domain.com - API Authentication: Consider adding authentication headers if implemented
- File size limits: The API has built-in file size restrictions
- Rate limiting: Be mindful of API rate limits for the underlying services
curl -X POST "http://localhost:8000/transcribe-image" \
-F "file=@test_image.jpg"curl -X POST "https://your-api-domain.com/transcribe-image" \
-H "Authorization: Bearer your-token" \
-F "file=@image.jpg"# If running in Docker
curl -X POST "http://localhost:8000/transcribe-image" \
-F "file=@image.jpg"import requests
# Using image URL (primary method)
data = {"url": "https://example.com/photo.jpg"}
response = requests.post('http://localhost:8000/transcribe-image-url', json=data)
result = response.json()
print(f"Title: {result['title']}")
# Alternative: Upload local file
# with open('photo.jpg', 'rb') as f:
# files = {'file': f}
# response = requests.post('http://localhost:8000/transcribe-image', files=files)
# result = response.json()
# print(f"Title: {result['title']}")// Using image URL (primary method)
const data = { url: "https://example.com/photo.jpg" };
fetch('http://localhost:8000/transcribe-image-url', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log('Title:', data.title));
// Alternative: Upload local file
// const FormData = require('form-data');
// const fs = require('fs');
//
// const form = new FormData();
// form.append('file', fs.createReadStream('photo.jpg'));
//
// fetch('http://localhost:8000/transcribe-image', {
// method: 'POST',
// body: form
// })
// .then(response => response.json())
// .then(data => console.log('Title:', data.title));// Using image URL (primary method)
$data = json_encode(array('url' => 'https://example.com/photo.jpg'));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://localhost:8000/transcribe-image-url',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
),
));
$response = curl_exec($curl);
$result = json_decode($response, true);
echo "Title: " . $result['title'];
// Alternative: Upload local file
// $curl = curl_init();
// curl_setopt_array($curl, array(
// CURLOPT_URL => 'http://localhost:8000/transcribe-image',
// CURLOPT_POST => true,
// CURLOPT_POSTFIELDS => array('file' => new CURLFile('photo.jpg')),
// CURLOPT_RETURNTRANSFER => true,
// ));
//
// $response = curl_exec($curl);
// $result = json_decode($response, true);
// echo "Title: " . $result['title'];curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello, how are you?",
"source_language": "en",
"target_language": "es"
}'curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "Bonjour, comment allez-vous?",
"source_language": "auto-detect",
"target_language": "en"
}'# Using "auto"
curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "Hola, ¿cómo estás?",
"source_language": "auto",
"target_language": "en"
}'
# Using "detect"
curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "Guten Tag, wie geht es Ihnen?",
"source_language": "detect",
"target_language": "en"
}'curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello world",
"source_language": "en",
"target_language": "fr",
"use_openai": true
}'curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "This is a longer text that demonstrates the translation capabilities of the API. It can handle multiple sentences and preserve the meaning and context across the translation.",
"source_language": "auto-detect",
"target_language": "es"
}'curl -X POST "http://localhost:8000/translate-batch" \
-H "Content-Type: application/json" \
-d '{
"texts": [
"Hello world",
"How are you?",
"Thank you very much"
],
"source_language": "auto-detect",
"target_language": "es"
}'curl -X POST "http://localhost:8000/translate-batch" \
-H "Content-Type: application/json" \
-d '{
"texts": [
"Hello world",
"Bonjour le monde",
"Hola mundo",
"Hallo Welt"
],
"source_language": "auto",
"target_language": "en"
}'curl -X POST "http://localhost:8000/translate-batch" \
-H "Content-Type: application/json" \
-d '{
"texts": [
"Buenos días",
"Buenas tardes",
"Buenas noches"
],
"source_language": "es",
"target_language": "en"
}'curl -X POST "http://localhost:8000/detect-language" \
-H "Content-Type: application/json" \
-d '{
"text": "Je suis très heureux de vous rencontrer"
}'curl -X POST "http://localhost:8000/detect-language" \
-H "Content-Type: application/json" \
-d '{
"text": "Ich freue mich sehr, Sie kennenzulernen",
"use_openai": true
}'curl -X GET "http://localhost:8000/supported-languages" \
-H "Accept: application/json"curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "The weather is beautiful today",
"source_language": "en",
"target_language": "es"
}'curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "私は日本人です",
"source_language": "auto-detect",
"target_language": "fr"
}'curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "Wie spät ist es?",
"source_language": "de",
"target_language": "en"
}'curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello",
"source_language": "invalid",
"target_language": "es"
}'curl -X POST "http://localhost:8000/translate" \
-H "Content-Type: application/json" \
-d '{
"text": "",
"source_language": "en",
"target_language": "es"
}'