Spaces:
Sleeping
Sleeping
| // Simple test script to verify basic functionality without full dependencies | |
| const express = require('express'); | |
| const multer = require('multer'); | |
| const cors = require('cors'); | |
| const app = express(); | |
| const PORT = process.env.PORT || 7860; | |
| // Middleware | |
| app.use(cors()); | |
| app.use(express.json({ limit: '50mb' })); | |
| app.use(express.urlencoded({ extended: true, limit: '50mb' })); | |
| app.use(express.static('public')); | |
| // Configure multer for file uploads | |
| const upload = multer({ | |
| storage: multer.memoryStorage(), | |
| limits: { | |
| fileSize: 10 * 1024 * 1024, // 10MB limit | |
| }, | |
| fileFilter: (req, file, cb) => { | |
| if (file.mimetype.startsWith('image/')) { | |
| cb(null, true); | |
| } else { | |
| cb(new Error('Only image files are allowed'), false); | |
| } | |
| } | |
| }); | |
| // Health check endpoint | |
| app.get('/', (req, res) => { | |
| res.json({ | |
| status: 'ok', | |
| message: 'AI Image Upscaler API (Test Mode)', | |
| version: '1.0.0', | |
| note: 'This is a test version. Canvas and TensorFlow.js dependencies are required for full functionality.', | |
| endpoints: { | |
| upscale: 'POST /upscale', | |
| health: 'GET /' | |
| } | |
| }); | |
| }); | |
| // Test upscale endpoint | |
| app.post('/upscale', upload.single('image'), async (req, res) => { | |
| try { | |
| if (!req.file) { | |
| return res.status(400).json({ error: 'No image file provided' }); | |
| } | |
| const { scale = 2, modelType = 'esrgan-slim', patchSize = 128, padding = 8 } = req.body; | |
| console.log(`Test upscale request: ${req.file.originalname}, scale: ${scale}x, model: ${modelType}`); | |
| // Return the original image as base64 (test mode) | |
| const originalImageBase64 = `data:${req.file.mimetype};base64,${req.file.buffer.toString('base64')}`; | |
| // Simulate processing time | |
| await new Promise(resolve => setTimeout(resolve, 500)); | |
| res.json({ | |
| success: true, | |
| result: originalImageBase64, | |
| metadata: { | |
| scale: parseInt(scale), | |
| modelType: modelType, | |
| patchSize: parseInt(patchSize), | |
| padding: parseInt(padding), | |
| processingTime: 500, | |
| backend: 'test-mode', | |
| note: 'This is a test response. Original image returned without upscaling.' | |
| } | |
| }); | |
| } catch (error) { | |
| console.error('Test upscaling error:', error); | |
| res.status(500).json({ | |
| error: 'Failed to process image', | |
| message: error.message | |
| }); | |
| } | |
| }); | |
| // Error handling middleware | |
| app.use((error, req, res, next) => { | |
| if (error instanceof multer.MulterError) { | |
| if (error.code === 'LIMIT_FILE_SIZE') { | |
| return res.status(400).json({ error: 'File too large. Maximum size is 10MB' }); | |
| } | |
| } | |
| console.error('Unhandled error:', error); | |
| res.status(500).json({ error: 'Internal server error' }); | |
| }); | |
| // Start server | |
| app.listen(PORT, '0.0.0.0', () => { | |
| console.log(`π§ͺ Test Upscaler API server running on port ${PORT}`); | |
| console.log(`π Note: This is a test version without AI processing`); | |
| console.log(`π Health check: http://localhost:${PORT}/`); | |
| console.log(`π Web interface: http://localhost:${PORT}/index.html`); | |
| }); | |
| // Handle graceful shutdown | |
| process.on('SIGTERM', () => { | |
| console.log('Received SIGTERM, shutting down gracefully...'); | |
| process.exit(0); | |
| }); | |