File size: 2,637 Bytes
3383e70
a17cb8e
ff5caa7
a17cb8e
 
3383e70
 
a17cb8e
ff5caa7
a17cb8e
 
 
ff5caa7
647c58d
ff5caa7
 
 
a17cb8e
647c58d
3383e70
 
 
647c58d
a17cb8e
647c58d
 
a17cb8e
 
 
 
647c58d
a17cb8e
 
647c58d
a17cb8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
647c58d
a17cb8e
647c58d
a17cb8e
 
 
 
647c58d
a17cb8e
3383e70
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
document.addEventListener('DOMContentLoaded', () => {
    const askBtn = document.getElementById('askBtn');
    const tokenInput = document.getElementById('tokenInput');
    const contextText = document.getElementById('contextText');
    const questionText = document.getElementById('questionText');
    const outputResult = document.getElementById('outputResult');

    askBtn.addEventListener('click', async () => {
        const HF_TOKEN = tokenInput.value.trim();
        const context = contextText.value.trim();
        const question = questionText.value.trim();

        if (!HF_TOKEN) {
            outputResult.textContent = "Please enter your Hugging Face access token in the configuration field above.";
            return;
        }

        if (!question) {
            outputResult.textContent = "Please provide a query or question to process.";
            return;
        }

        outputResult.textContent = "Processing request, please wait...";

        const prompt = `You are Zuban AI, a professional assistant. Use the provided context to answer the user's query accurately. 
IMPORTANT RULE: You MUST reply entirely in clear, natural Roman Urdu (Hindi/Urdu written in English script). Do not reply in standard Urdu script or English.

Context:
${context}

Query:
${question}

Roman Urdu Response:`;

        try {
            const response = await fetch("https://router.huggingface.co/v1/chat/completions", {
                method: "POST",
                headers: {
                    "Authorization": `Bearer ${HF_TOKEN}`,
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({
                    model: "meta-llama/Llama-3.1-8B-Instruct:fastest",
                    messages: [
                        { role: "user", content: prompt }
                    ],
                    max_tokens: 500,
                    temperature: 0.7
                })
            });

            const data = await response.json();

            if (data.choices && data.choices.length > 0) {
                outputResult.textContent = data.choices[0].message.content.trim();
            } else if (data.error) {
                outputResult.textContent = `API Error: ${data.error.message || JSON.stringify(data.error)}`;
            } else {
                outputResult.textContent = "No valid response received. Please check your token and inputs.";
            }

        } catch (error) {
            console.error(error);
            outputResult.textContent = "A network or transmission error occurred. Please verify your connection.";
        }
    });
});