Spaces:
Running
Running
Download Server.js. from MYSTRO6IX/MYSTROGAD: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/spaces/MYSTRO6IX/MYSTROGAD/resolve/main/Server.js.
- Command line
-
hf download hf://spaces/MYSTRO6IX/MYSTROGAD/Server.js.
-
curl -L -o Server.js. https://huggingface.co/spaces/MYSTRO6IX/MYSTROGAD/resolve/main/Server.js.
1.55 kB
| export default { | |
| async fetch(request, env) { | |
| const corsHeaders = { | |
| "Access-Control-Allow-Origin": "*", | |
| "Access-Control-Allow-Methods": "POST, OPTIONS", | |
| "Access-Control-Allow-Headers": "Content-Type" | |
| }; | |
| if (request.method === "OPTIONS") { | |
| return new Response(null, { headers: corsHeaders }); | |
| } | |
| if (request.method !== "POST") { | |
| return new Response("CreateAI server is running!", { | |
| headers: corsHeaders | |
| }); | |
| } | |
| try { | |
| const { prompt } = await request.json(); | |
| if (!prompt) { | |
| return Response.json( | |
| { error: "Please enter a prompt." }, | |
| { status: 400, headers: corsHeaders } | |
| ); | |
| } | |
| const response = await fetch( | |
| "https://router.huggingface.co/v1/chat/completions", | |
| { | |
| method: "POST", | |
| headers: { | |
| "Authorization": `Bearer ${env.HF_TOKEN}`, | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify({ | |
| model: "Qwen/Qwen2.5-7B-Instruct", | |
| messages: [ | |
| { | |
| role: "user", | |
| content: prompt | |
| } | |
| ], | |
| max_tokens: 800 | |
| }) | |
| } | |
| ); | |
| const data = await response.json(); | |
| return Response.json(data, { | |
| status: response.status, | |
| headers: corsHeaders | |
| }); | |
| } catch (error) { | |
| return Response.json( | |
| { error: error.message }, | |
| { status: 500, headers: corsHeaders } | |
| ); | |
| } | |
| } | |
| }; | |