| | |
| | import express from 'express'; |
| | import cors from 'cors'; |
| | import multer from 'multer'; |
| | import { uploadFile } from '@huggingface/hub'; |
| | import path from 'path'; |
| | import { fileURLToPath } from 'url'; |
| | import dotenv from 'dotenv'; |
| | dotenv.config() |
| | |
| | const __filename = fileURLToPath(import.meta.url); |
| | const __dirname = path.dirname(__filename); |
| |
|
| | const app = express(); |
| | |
| | const port = process.env.PORT || 7860; |
| |
|
| | |
| | app.use(cors()); |
| | app.use(express.json()); |
| |
|
| | |
| | const storage = multer.memoryStorage(); |
| | const upload = multer({ storage: storage }); |
| |
|
| | |
| | |
| | const SERVER_CONFIG = { |
| | TOKEN: process.env.HF_TOKEN || '', |
| | REPO: process.env.HF_REPO || 'TwanAPI/DataTwan', |
| | TYPE: 'dataset' |
| | }; |
| |
|
| | |
| |
|
| | |
| | app.post('/api/upload', upload.single('file'), async (req, res) => { |
| | try { |
| | const file = req.file; |
| | const filePath = req.body.path; |
| |
|
| | if (!file) { |
| | return res.status(400).json({ error: 'No file provided' }); |
| | } |
| |
|
| | if (!filePath) { |
| | return res.status(400).json({ error: 'No file path provided' }); |
| | } |
| |
|
| | console.log(`[SERVER] Uploading: ${filePath} (${file.size} bytes)`); |
| |
|
| | |
| | const response = await uploadFile({ |
| | credentials: { |
| | accessToken: SERVER_CONFIG.TOKEN, |
| | }, |
| | repo: { |
| | type: SERVER_CONFIG.TYPE, |
| | name: SERVER_CONFIG.REPO |
| | }, |
| | file: { |
| | path: filePath, |
| | content: new Blob([file.buffer]), |
| | }, |
| | }); |
| |
|
| | const commitHash = response.commit.oid; |
| | const urlPrefix = "https://huggingface.co/datasets"; |
| | const publicUrl = `${urlPrefix}/${SERVER_CONFIG.REPO}/blob/${commitHash}/${filePath}`; |
| |
|
| | console.log(`[SERVER] Success: ${publicUrl}`); |
| |
|
| | res.json({ |
| | success: true, |
| | url: publicUrl |
| | }); |
| |
|
| | } catch (error) { |
| | console.error('[SERVER] Upload Error:', error); |
| | res.status(500).json({ |
| | success: false, |
| | error: error.message || 'Internal Server Error' |
| | }); |
| | } |
| | }); |
| |
|
| | |
| |
|
| | |
| | app.use(express.static(path.join(__dirname, 'dist'))); |
| |
|
| | |
| | app.get('*', (req, res) => { |
| | res.sendFile(path.join(__dirname, 'dist', 'index.html')); |
| | }); |
| |
|
| | |
| | app.listen(port, () => { |
| | console.log(`--------------------------------------------------`); |
| | console.log(`✅ Server running on port ${port}`); |
| | console.log(` Target Repo: ${SERVER_CONFIG.REPO}`); |
| | console.log(`--------------------------------------------------`); |
| | }); |