Spaces:
Configuration error
Configuration error
Download server.js from mo7hamed1x/AISD: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/spaces/mo7hamed1x/AISD/resolve/main/server.js
- Command line
-
hf download hf://spaces/mo7hamed1x/AISD/server.js
-
curl -L -o server.js https://huggingface.co/spaces/mo7hamed1x/AISD/resolve/main/server.js
1.4 kB
| const express = require('express'); | |
| const app = express(); | |
| app.use(express.json({ limit: '1mb' })); | |
| // تأتي من Settings → Secrets في الـ Space | |
| const API_KEY = process.env.ANTHROPIC_API_KEY; | |
| const APP_TOKEN = process.env.APP_TOKEN; // اختياري: رمز بسيط يرسله تطبيقك | |
| if (!API_KEY) { | |
| console.error('ANTHROPIC_API_KEY is missing'); | |
| process.exit(1); | |
| } | |
| // فحص أن السيرفر يعمل | |
| app.get('/', (_req, res) => res.send('AISD proxy is running')); | |
| app.post('/ai/messages', async (req, res) => { | |
| if (APP_TOKEN && req.get('x-app-token') !== APP_TOKEN) { | |
| return res.status(401).json({ error: 'unauthorized' }); | |
| } | |
| try { | |
| const { model, max_tokens, system, tools, tool_choice, messages } = req.body; | |
| const r = await fetch('https://api.anthropic.com/v1/messages', { | |
| method: 'POST', | |
| headers: { | |
| 'content-type': 'application/json', | |
| 'x-api-key': API_KEY, | |
| 'anthropic-version': '2023-06-01', | |
| }, | |
| body: JSON.stringify({ | |
| model, | |
| max_tokens: Math.min(max_tokens || 1500, 2000), | |
| system, | |
| tools, | |
| tool_choice, | |
| messages, | |
| }), | |
| }); | |
| res.status(r.status).type('json').send(await r.text()); | |
| } catch (e) { | |
| res.status(500).json({ error: String(e) }); | |
| } | |
| }); | |
| app.listen(process.env.PORT || 7860, '0.0.0.0', () => | |
| console.log('Proxy running'), | |
| ); |