CVNSS commited on
Commit
1408074
·
verified ·
1 Parent(s): f7e4d20

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +124 -18
index.html CHANGED
@@ -1,19 +1,125 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
+ <!DOCTYPE html>
2
+ <html lang="vi">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Botchat CVNSS 4.0</title>
6
+
7
+ <!-- Bộ chuyển CVNSS 4.0 -->
8
+ <script src="cvnss4.0-converter.js"></script>
9
+
10
+ <style>
11
+ body{font-family:Arial,Helvetica,sans-serif;background:#f5f7fa;margin:30px auto;max-width:760px;color:#333}
12
+ h2{text-align:center;color:#2c3e50;margin-bottom:20px}
13
+ #chat{background:#fff;border:1px solid #dcdcdc;border-radius:8px;min-height:340px;max-height:540px;
14
+ overflow-y:auto;padding:18px;margin-bottom:16px}
15
+ .msg{margin-bottom:16px;line-height:1.45}
16
+ .user b{color:#2980b9}.bot b{color:#27ae60}
17
+ #bar{display:flex;gap:8px}
18
+ textarea{flex:1;padding:10px 12px;font-size:15px;border:1px solid #ccc;border-radius:6px;resize:vertical;min-height:48px}
19
+ button{padding:11px 22px;font-size:15px;border:none;border-radius:6px;cursor:pointer;background:#2c97de;color:#fff}
20
+ button:hover{filter:brightness(1.1)}button:active{filter:brightness(.93)}
21
+ .spinner{display:inline-block;width:16px;height:16px;border:3px solid #bbb;border-top-color:#2c97de;border-radius:50%;
22
+ animation:spin 1s linear infinite;margin-left:6px;vertical-align:middle}
23
+ @keyframes spin{to{transform:rotate(360deg)}}
24
+ </style>
25
+ </head>
26
+ <body>
27
+ <h2>💬 Botchat - 1 sản phẩm của CVNSS4.0</h2>
28
+
29
+ <div id="chat"></div>
30
+
31
+ <div id="bar">
32
+ <!-- textarea đa dòng; Shift+Enter xuống dòng -->
33
+ <textarea id="input" placeholder="Nhập tin nhắn…"></textarea>
34
+ <button id="send">Gửi</button>
35
+ </div>
36
+
37
+ <script>
38
+ /* ========= CẤU HÌNH ========= */
39
+ const GROQ_KEY = "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"; // chỉ dùng nội bộ!
40
+ const ENDPOINT = "https://api.groq.com/openai/v1/chat/completions";
41
+ const MODEL = "meta-llama/llama-4-scout-17b-16e-instruct";
42
+ /* ============================ */
43
+
44
+ const chat = document.getElementById("chat");
45
+ const input = document.getElementById("input");
46
+ const sendBt = document.getElementById("send");
47
+ const spin = '<span class="spinner"></span>';
48
+ const toCVN = txt => CVNSSConverter.convert(txt,"cqn").cvn;
49
+ const scroll = () => chat.scrollTop = chat.scrollHeight;
50
+
51
+ /* Thêm tin nhắn vào khung chat */
52
+ function add(role, html){
53
+ chat.insertAdjacentHTML("beforeend",
54
+ `<div class="msg ${role}"><b>${role==='user'?'Bạn':'GPT'}:</b> ${html}</div>`);
55
+ scroll();
56
+ }
57
+
58
+ /* Gọi Groq API (retry tối đa 3 lần nếu 429/503) */
59
+ async function askGroq(prompt){
60
+ const body = JSON.stringify({
61
+ model: MODEL,
62
+ messages: [{ role:"user", content: prompt }]
63
+ });
64
+
65
+ for(let i=0;i<3;i++){
66
+ const res = await fetch(ENDPOINT,{
67
+ method:"POST",
68
+ headers:{
69
+ "Content-Type":"application/json",
70
+ "Authorization":`Bearer ${GROQ_KEY}`
71
+ },
72
+ body
73
+ });
74
+
75
+ if(res.ok){
76
+ const j = await res.json();
77
+ return j.choices[0].message.content.trim(); // thành công
78
+ }
79
+
80
+ if([429,503].includes(res.status)){ // hết quota / quá tải → đợi
81
+ const wait = 1500*(i+1); // 1.5s,3s,4.5s
82
+ console.warn(`Groq ${res.status} – đợi ${wait/1000}s rồi thử lại`);
83
+ await new Promise(r=>setTimeout(r,wait));
84
+ continue;
85
+ }
86
+ throw new Error(`HTTP ${res.status} – ${await res.text()}`);
87
+ }
88
+ throw new Error("Groq quá tải – thử lại sau.");
89
+ }
90
+
91
+ /* Gửi tin nhắn */
92
+ async function send(){
93
+ const text = input.value.trim();
94
+ if(!text) return;
95
+ input.value=""; add("user", text); add("bot", spin);
96
+ const last = chat.lastElementChild; sendBt.disabled=true;
97
+
98
+ try{
99
+ const raw = await askGroq(text);
100
+ last.innerHTML = `<b>GPT:</b> ${toCVN(raw)}`;
101
+ }catch(err){
102
+ last.innerHTML = `<b>GPT:</b> ⚠️ ${err.message}`;
103
+ console.error(err);
104
+ }finally{
105
+ sendBt.disabled=false; input.focus();
106
+ }
107
+ }
108
+
109
+ /* Sự kiện UI */
110
+ sendBt.onclick = send;
111
+ input.addEventListener("keydown", e=>{
112
+ if(e.key==="Enter" && !e.shiftKey){ // Enter để gửi
113
+ e.preventDefault(); send();
114
+ }
115
+ });
116
+
117
+ input.focus();
118
+
119
+ /* Khuyến nghị chạy qua localhost khi mở file trực tiếp */
120
+ if(location.protocol==='file:'){
121
+ console.warn("Chạy: python -m http.server 8080 ➔ http://localhost:8080/");
122
+ }
123
+ </script>
124
+ </body>
125
  </html>